Find if point lies on line segment
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Determining if a point lies on a line segment is a fundamental problem in computational geometry with practical applications in computer graphics, geographic information systems (GIS), and more. This article explores the mathematical principles and algorithms used to determine if a given point resides precisely on a line segment defined by two endpoints.
Mathematical Background
A line segment is a part of a line bounded by two endpoints. If you have a line segment `AB` with endpoints `A(x1, y1)` and `B(x2, y2)`, and a point `P(x, y)`, the question is whether `P` lies on the line segment `AB`.
To determine if `P` lies on `AB`, we need to check two main conditions:
• Collinearity: The point `P` should be collinear with `A` and `B`, meaning all three points lie on the same straight line. • Segment Bounds: The point `P` must be situated between `A` and `B`.
Condition 1: Collinearity
A point is collinear with two other points if the area of the triangle formed by the three points is zero. For three points to lie on the same line, the cross product of vectors $\overrightarrow\{AP\}$ and $\overrightarrow\{AB\}$ should be zero. Mathematically, this is expressed as:
Condition 2: Segment Bounds
Once collinearity is confirmed, check that `P` lies between `A` and `B`. This involves the following checks:
• The x-coordinate of `P` is between the x-coordinates of `A` and `B`. • The y-coordinate of `P` is between the y-coordinates of `A` and `B`.
Mathematically, this is expressed using inequalities:
Algorithm
Here is a step-by-step algorithm to check if a point `P` lies on the line segment `AB`:
- Calculate Collinearity: Use the collinearity condition to verify if `P` is collinear with `A` and `B`.
- Verify Segment Boundaries: Check the bounds using the inequality conditions mentioned above.
Example
Let's examine an example to clarify these concepts.
Suppose `A(1, 1)`, `B(3, 3)`, and `P(2, 2)`. To determine if `P` lies on `AB`:
- Calculate collinearity using the formula:`P` is collinear with `A` and `B`.
- Check the segment bounds:
Thus, `P(2, 2)` lies on the line segment `AB`.
Summary Table
| Condition | Mathematical Expression | Explanation |
| Collinearity | Ensures points A, B, and P are on the same line. | |
| Segment Bound (x) | Verifies that the x-coordinate of P is within the boundary created by A and B. | |
| Segment Bound (y) | Verifies that the y-coordinate of P is within the boundary created by A and B. |
Additional Details
Edge Cases
• Vertical Line Segments: When the line segment is vertical (i.e., `x1 = x2`), the x-bound check simplifies to ensuring `P`'s x-coordinate is equal to the common x-coordinate of `A` and `B`. • Horizontal Line Segments: Similarly, for horizontal segments (`y1 = y2`), only y-coordinates need to be checked. • Point Coinciding with Endpoint: If `P` coincides exactly with either `A` or `B`, it is automatically considered to lie on the segment.
Computational Considerations
Floating point calculations can introduce significant precision errors when checking for collinearity. Using integer arithmetic or rational numbers when possible can help mitigate this. For practical programming language implementations, epsilon values should often be used to account for these precision issues.
Conclusion
Determining if a point lies on a line segment encompasses understanding geometric collinearity and incorporates boundary conditions for the segment's endpoints. With applications ranging from vector graphics rendering to GPS-based applications, understanding and applying these principles accurately and efficiently is crucial in computational geometry fields.

