Find if point lies on line segment
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- Find k-th minimum sum of every possible subset
- Find K nearest Points to Point P in 2-dimensional plane
- Find largest rectangle containing only zeros in an N×N binary matrix
- Find maximum possible time HHMM by permuting four given digits
- Find minimal Ai2 Bi2 when A and B are sorted
- Find most efficient groups of pairs
- Find next highest unique number from the given digits
- Find number of permutations of a given sequence of integers which yield the same binary search tree

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.