Algorithm for intersection of 2 lines?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding the intersection of two lines is a standard problem in geometry, graphics, and simulation code. Once the two lines are known, the computation is small: you solve a two-equation system and either get one intersection point, no intersection because the lines are parallel, or infinitely many points because the lines are the same line.
The important implementation detail is the representation. In code, a point-direction or two-point form is usually easier to work with than slope-intercept form because it handles vertical lines cleanly.
Represent Each Line with Two Points
Suppose line 1 passes through points (x1, y1) and (x2, y2), and line 2 passes through (x3, y3) and (x4, y4). From those points, you can compute the intersection directly using determinants.
A common formula is:
If denominator is zero, the lines are parallel or coincident. Otherwise, the intersection exists at a unique point.
A Practical Python Implementation
Here is a runnable function:
This returns (2.0, 2.0) for the example above.
Why This Works
Each line can be written in the implicit form:
Ax + By = C
Solving two such equations gives the intersection point. The determinant in the denominator tells you whether the coefficient matrix is invertible.
That is the mathematical reason the zero-denominator case matters. If the determinant is zero, the system does not have one unique solution.
Lines Versus Line Segments
A very common source of confusion is whether you want infinite lines or finite segments.
The formula above computes the intersection of the infinite lines extending through the given points. If your input is line segments, you need an extra check to confirm that the intersection lies within both segment bounds.
Here is a simple segment check once the line intersection has been computed:
For robust production code, use a tolerance instead of exact comparisons with floating-point values.
Time Complexity
If the two lines are already given, the computation is constant time: O(1). You perform a fixed number of arithmetic operations regardless of the coordinates.
That is worth emphasizing because the word “algorithm” sometimes sounds heavier than it is here. The complexity does not grow with input size in the usual array-processing sense.
Handling Special Cases
There are three main outcomes:
- unique intersection point
- parallel lines with no intersection
- coincident lines with infinitely many intersections
The simple None return above groups parallel and coincident lines together. If you need to distinguish them, add another check based on whether the line equations are scalar multiples of each other.
Common Pitfalls
One common mistake is using slope-intercept form and then running into division-by-zero problems for vertical lines. Point-based determinant formulas avoid that issue cleanly.
Another issue is comparing floating-point results with exact equality. In geometry code, tiny rounding errors are common, so tolerance-based comparisons are safer.
It is also easy to forget whether the task is about infinite lines or bounded segments. The formulas are related, but the segment version needs additional range checks.
Finally, do not skip the denominator check. Without it, parallel lines will produce a division-by-zero failure instead of a meaningful geometric result.
Summary
- A standard way to intersect two lines is to represent each line with two points and solve using determinants.
- If the denominator is zero, the lines are parallel or coincident and there is no unique intersection point.
- The calculation for two already-defined lines is
O(1)time. - Infinite line intersection and line-segment intersection are not the same problem.
- Determinant-based formulas are usually easier to implement robustly than slope-intercept formulas.

