geometry
mathematics
intersection
coordinate geometry
line equations

How to find 4 points next to the intersection of two lines

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

To get four points near the intersection of two lines, first compute the intersection itself, then add a small offset in controlled directions. The best choice of directions depends on what "next to" means in your problem: axis-aligned around the crossing, or aligned with the geometry of the lines.

Compute the intersection first

If the lines are:

  • 'y = m1 * x + c1'
  • 'y = m2 * x + c2'

and the slopes are different, the intersection comes from solving the two equations together.

python
1def line_intersection(m1, c1, m2, c2):
2    if m1 == m2:
3        raise ValueError("Parallel lines do not have a unique intersection")
4
5    x = (c2 - c1) / (m1 - m2)
6    y = m1 * x + c1
7    return x, y
8
9
10point = line_intersection(2.0, 3.0, -1.0, 1.0)
11print(point)

This gives the crossing point. Everything else in the problem is just choosing nearby offsets from that point.

The simplest four nearby points are axis offsets

If you only need four points around the intersection, use a small distance eps along the x-axis and y-axis.

python
1def axis_neighbors(x, y, eps):
2    return [
3        (x + eps, y),
4        (x - eps, y),
5        (x, y + eps),
6        (x, y - eps),
7    ]
8
9
10x, y = line_intersection(2.0, 3.0, -1.0, 1.0)
11points = axis_neighbors(x, y, 0.1)
12for p in points:
13    print(p)

This is usually what people mean when they want four points "next to" the intersection in a coordinate-grid sense. It is easy to compute and works regardless of the line slopes.

If you want geometry-aware points, offset along directions

Sometimes axis-aligned offsets are not ideal. If you want points that respect the line directions, use direction vectors.

For a line with slope m, one direction vector is (1, m). Normalize it before using it as a step direction.

python
1import math
2
3def normalized_direction(m):
4    dx, dy = 1.0, m
5    length = math.hypot(dx, dy)
6    return dx / length, dy / length
7
8
9def directional_neighbors(x, y, m1, m2, eps):
10    d1x, d1y = normalized_direction(m1)
11    d2x, d2y = normalized_direction(m2)
12    return [
13        (x + eps * d1x, y + eps * d1y),
14        (x - eps * d1x, y - eps * d1y),
15        (x + eps * d2x, y + eps * d2y),
16        (x - eps * d2x, y - eps * d2y),
17    ]
18
19
20x, y = line_intersection(2.0, 3.0, -1.0, 1.0)
21for p in directional_neighbors(x, y, 2.0, -1.0, 0.1):
22    print(p)

This version gives two nearby points along one line and two along the other. That is often the better answer in geometry code, CAD tools, or numerical methods where direction matters.

Pick the definition that matches the real task

There is no single universal meaning of "four points next to the intersection." Common meanings include:

  • four axis-aligned neighbors around the crossing
  • two points on each line near the crossing
  • four corner points of a small square centered at the intersection

So the real algorithm starts by clarifying the intended geometry. Once that is clear, the math is simple.

Common Pitfalls

  • Forgetting to handle parallel lines, which have no unique intersection point.
  • Using axis offsets when the problem actually requires points along the line directions.
  • Choosing an eps value that is too large and no longer represents "nearby" points.
  • Offsetting with raw slope vectors without normalizing them first.
  • Solving the intersection correctly but then using a neighboring-point definition that does not match the application.

Summary

  • First solve for the intersection of the two lines.
  • For simple nearby points, offset by a small eps along the x-axis and y-axis.
  • For geometry-aware points, offset along normalized direction vectors of the lines.
  • Parallel lines need special handling because they do not intersect uniquely.
  • The right four points depend on what "next to" means in your actual problem.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.