Distance From a Point to a Line, a Segment and a Plane
Compute the shortest distance from a point to a line, segment or plane, with perpendicular feet, formulas and checked examples.
Key takeaways
- Distance to a set of points always means the smallest distance to any point of the set.
- The perpendicular from a point to a line or plane is shorter than every slanted segment, by the Pythagorean theorem.
- The point where the perpendicular lands is called the foot, and it is also the nearest point.
- For a segment, check whether the foot falls between the endpoints before using the perpendicular distance.
- Coordinate formulas for a line in the plane and a plane in space share the same structure.
The distance from a point to a line, segment or plane is the length of the shortest segment from the point to any part of that object. For a whole line or plane, the shortest segment is the perpendicular one. For a segment, it is the perpendicular one only if it lands on the segment. Otherwise the nearest spot is an endpoint.
That one distinction prevents most errors, both on exams and in computer programs.
What "distance" means for a big object
Between two points there is only one distance. A line contains infinitely many points, so there are infinitely many segments from P to the line, with many different lengths. We define the distance as the smallest of them.
The point of the object that achieves it is the nearest point. Every other choice gives a longer segment, or at best an equally long one.
Why the perpendicular wins
Let P be a point off line m, and let F be the foot of the perpendicular from P to m. Take any other point Q on the line. Triangle PFQ has a right angle at F, so
PQ² = PF² + FQ²
FQ² > 0 (Q is not F)
PQ > PF So PF is shorter than every other segment from P to the line, and the nearest point is unique. The formula also shows how distance grows as you walk away from the foot: slowly at first, then almost as fast as you walk. A closer look at this fact is in perpendiculars and shortest distance.
Finding it with an area
If P, A and B form a triangle, the distance from P to line AB is the height of that triangle. Computing the area two ways often gets it without finding the foot.
Take a right triangle with legs 6 and 8 and hypotenuse 10. How far is the right-angle vertex from the hypotenuse?
area = (1/2)(6)(8) = 24
area = (1/2)(10)(h)
h = 4.8 The same idea drives the proof of Viviani's theorem on distances inside an equilateral triangle.
The coordinate formula for a line
Write the line as ax + by + c = 0, where a and b are not both zero. The distance from the point (x₀, y₀) is
distance = |a x₀ + b y₀ + c| / √(a² + b²) For the line 3x + 4y − 12 = 0 and the point (5, 3), the top is |15 + 12 − 12| = 15 and the bottom is √25 = 5. The distance is 3.
The numbers (a, b) form an arrow perpendicular to the line. To find the foot, slide from the point along that arrow by the right amount:
amount = (a x₀ + b y₀ + c) / (a² + b²) = 15/25
foot = (5, 3) − (15/25)(3, 4) = (3.2, 0.6)
check: 3(3.2) + 4(0.6) − 12 = 0 The sign of ax₀ + by₀ + c, before taking the absolute value, tells you which side of the line the point is on. Derivations of the formula are set out in two derivations of the point-to-line distance.
Two parallel lines ax + by + c₁ = 0 and ax + by + c₂ = 0 are everywhere the same distance apart, namely |c₁ − c₂| / √(a² + b²). The lines must be written with identical a and b before you subtract.
Segments need one extra test
A segment AB stops. If the perpendicular foot from P falls beyond an end, it is not part of the segment, and the nearest point is that end.
To test, compute the number t, which says how far along AB the foot lies. With AP and AB read as coordinate steps, t is the sum of the products of matching steps, divided by AB².
- If t < 0, the nearest point is A.
- If t > 1, the nearest point is B.
- If 0 ≤ t ≤ 1, the nearest point is the foot, at fraction t from A to B.
Let A = (1, 1), B = (7, 9) and P = (11, 7).
AB = (6, 8), AB² = 100
AP = (10, 6)
t = (10·6 + 6·8) / 100 = 108/100 = 1.08
t > 1, so the nearest point is B
distance = √(4² + 2²) = √20 ≈ 4.47 The distance from P to the full line through A and B is |6·6 − 8·10| / 10 = 4.4. It is slightly smaller, as it must be, because the line offers more points to choose from.
A ray has one end, so only the t < 0 case applies. A computer routine that skips the test may report that an object is close to a wall when it is really close to the wall's invisible extension.
The shape of a fixed distance
All points exactly r from a full line form two parallel lines. All points exactly r from a segment form a closed curve like a running track: two straight sides parallel to the segment and a half circle around each end. This difference mirrors the endpoint test.
Distance to shapes is the basis of signed distance fields in computer graphics, where each location stores how far it is from a surface and on which side.
Point to plane
A line through a point F of a plane is perpendicular to the plane if it makes a right angle with every line of the plane through F. It is enough to check two different ones.
If PF is perpendicular to the plane and Q is any other point of the plane, triangle PFQ again has a right angle at F, so PQ > PF. The same three-line argument covers planes.
In coordinates, with the plane ax + by + cz + d = 0,
distance = |a x₀ + b y₀ + c z₀ + d| / √(a² + b² + c²) Take the plane 2x − y + 2z − 4 = 0 and the point (3, −1, 4).
top = |6 + 1 + 8 − 4| = 11
bottom = √(4 + 1 + 4) = 3
distance = 11/3 ≈ 3.67
foot = (3, −1, 4) − (11/9)(2, −1, 2) = (5/9, 2/9, 14/9)
check: 10/9 − 2/9 + 28/9 − 4 = 0 Flat pieces and lines in space
A tabletop or a triangle is only part of a plane. As with segments, first find the foot in the full plane. If it lies on the piece, you are done. If not, the nearest point is on the boundary, so compute the distance to each edge segment and keep the smallest.
For the distance from a point to a line in space, the nearest point is again the foot, found with the same number t, now using three coordinate steps. In a box, the distance from a corner to a space diagonal can be found this way, or from the area of a right triangle.
Two lines in space that neither cross nor run parallel have their own problem, covered in the distance between skew lines.
Distance to a circle
The nearest-point idea works for curves too. Take a circle with center O and radius r, and a point P different from O. Draw the line from O through P. The nearest point of the circle is where the ray from O through P crosses it, and the distance is |OP − r|.
The proof is one use of the triangle inequality. For any Q on the circle, PQ is at least the difference between OP and OQ = r, and the point on the ray achieves exactly that difference.
For a circle of radius 5 about the origin and P = (6, 8), OP = 10, so the distance is 5, reached at (3, 4). For P = (1, 2), inside the circle, OP = √5 and the distance is 5 − √5 ≈ 2.76. At the center itself, every point of the circle ties at distance r.
Loci that compare two distances
Once distance to a line is defined, you can ask for points that balance it against another distance.
Points equally far from two crossing lines lie on the angle bisectors. Points equally far from a point and a line that does not pass through it form a parabola.
Take the point (0, 1) and the line y = −1. The distance from (x, y) to the line is |y + 1|. Setting the squares equal gives
x² + (y − 1)² = (y + 1)²
x² = 4y The point (4, 4) is on this curve. Its distance to (0, 1) is √(16 + 9) = 5, and its distance to the line is 4 + 1 = 5.
A site-plan style problem
A straight water main runs from A = (0, 0) to B = (40, 0), in meters, and stops at both ends. Two sheds stand at P = (12, 9) and R = (49, 12). What is the shortest pipe from each shed to the main?
For P, the foot is (12, 0), which lies between the ends, so the length is 9 m. For R, the foot would be (49, 0), beyond B. The nearest point of the main is B, and the length is √(9² + 12²) = 15 m.
Using the perpendicular distance of 12 m for the second shed would order a pipe that ends in empty ground. Real installation depends on surveys, utility records and local rules, but the geometric trap is the same.
A checklist before you answer
- Is the object a line, ray, segment, plane or flat piece?
- Where does the perpendicular foot land?
- Is the foot actually part of the object?
- Are the units consistent, and is the answer no larger than the distance to any obvious point of the object?
The last question is a cheap test. If your distance to a segment exceeds the distance to one of its endpoints, something has gone wrong.