line intersection
geometry
algorithm
computational mathematics
mathematics

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:

text
denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)

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:

python
1from typing import Optional, Tuple
2
3
4def intersect_lines(
5    p1: Tuple[float, float],
6    p2: Tuple[float, float],
7    p3: Tuple[float, float],
8    p4: Tuple[float, float],
9) -> Optional[Tuple[float, float]]:
10    x1, y1 = p1
11    x2, y2 = p2
12    x3, y3 = p3
13    x4, y4 = p4
14
15    denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
16    if denominator == 0:
17        return None
18
19    det1 = x1 * y2 - y1 * x2
20    det2 = x3 * y4 - y3 * x4
21
22    x = (det1 * (x3 - x4) - (x1 - x2) * det2) / denominator
23    y = (det1 * (y3 - y4) - (y1 - y2) * det2) / denominator
24    return (x, y)
25
26
27print(intersect_lines((0, 0), (4, 4), (0, 4), (4, 0)))

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:

python
1def within(a: float, b: float, value: float) -> bool:
2    return min(a, b) <= value <= max(a, b)
3
4
5def point_on_segment(p1, p2, point) -> bool:
6    x, y = point
7    return within(p1[0], p2[0], x) and within(p1[1], p2[1], y)

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.

Course illustration
Course illustration

All Rights Reserved.