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.
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.
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.
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.
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
epsvalue 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
epsalong 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
- How to find all combinations of coins when given some dollar value
- How to find all grid points that correspond to non-reduced fractions in a square?
- How to find all grid squares on a line?
- How to find all partitions of a set
- How to find all permutations of a given word in a given text?
- How to find all possible subsets of a given array?
- How to find all taxicab numbers less than N?
- How to find all vertex-disjoint paths in a graph?

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.