Points, lines, and planes In what follows are various notes and algorithms dealing with points, lines, and planes.





Minimum Distance between

a Point and a Line Written by Paul Bourke

October 1988 This note describes the technique and gives the solution to finding the shortest distance from a point to a line or line segment. The equation of a line defined through two points P1 (x1,y1) and P2 (x2,y2) is P = P1 + u (P2 - P1) The point P3 (x3,y3) is closest to the line at the tangent to the line which passes through P3, that is, the dot product of the tangent and line is 0, thus (P3 - P) dot (P2 - P1) = 0 Substituting the equation of the line gives [P3 - P1 - u(P2 - P1)] dot (P2 - P1) = 0 Solving this gives the value of u Substituting this into the equation of the line gives the point of intersection (x,y) of the tangent as x = x1 + u (x2 - x1)

y = y1 + u (y2 - y1) The distance therefore between the point P3 and the line is the distance between (x,y) above and P3. Notes The only special testing for a software implementation is to ensure that P1 and P2 are not coincident (denominator in the equation for u is 0)

and are not coincident (denominator in the equation for u is 0) If the distance of the point to a line segment is required then it is only necessary to test that u lies between 0 and 1.

The solution is similar in higher dimensions. Source code contributions

C source from Damian Coventry: C source code

VBA from Brandon Crosby: VBA source code

Dephi from Graham O'Brien: Delphi version

"R" version from Gregoire Thomas: pointline.r

JAVA version from Pieter Iserbyt: DistancePoint.java

LabView implementation from Chris Dancer: Pointlinesegment.vi.zip

Right side distance by Orion Elenzil: rightside

VBA VB6 by Thomas Ludewig: vbavb6.txt





Minimum Distance between

a Point and a Plane Written by Paul Bourke

March 1996 Let P a = (x a , y a , z a ) be the point in question. A plane can be defined by its normal n = (A, B, C) and any point on the plane P b = (x b , y b , z b ) Any point P = (x,y,z) lies on the plane if it satisfies the following A x + B y + C z + D = 0 The minimum distance between P a and the plane is given by the absolute value of (A x a + B y a + C z a + D) / sqrt(A2 + B2 + C2) . . . 1 To derive this result consider the projection of the line (P a - P b ) onto the normal of the plane n, that is just ||P a - P b || cos(theta), where theta is the angle between (P a - P b ) and the normal n. This projection is the minimum distance of P a to the plane. This can be written in terms of the dot product as minimum distance = (P a - P b ) dot n / ||n|| That is minimum distance = (A (x a - x b ) + B (y a - y b ) + C (z a - z b )) / sqrt(A2 + B2 + C2) . . . 2 Since point (x b , y b , z b ) is a point on the plane A x b + B y b + C z b + D = 0 . . . 3 Substituting equation 3 into equation 2 gives the result shown in equation 1.





Intersection point of two line segments in 2 dimensions Written by Paul Bourke

April 1989 This note describes the technique and algorithm for determining the intersection point of two lines (or line segments) in 2 dimensions. The equations of the lines are P a = P1 + u a ( P2 - P1 ) P b = P3 + u b ( P4 - P3 ) Solving for the point where P a = P b gives the following two equations in two unknowns (u a and u b ) x1 + u a (x2 - x1) = x3 + u b (x4 - x3)

and

y1 + u a (y2 - y1) = y3 + u b (y4 - y3) Solving gives the following expressions for u a and u b Substituting either of these into the corresponding equation for the line gives the intersection point. For example the intersection point (x,y) is x = x1 + u a (x2 - x1) y = y1 + u a (y2 - y1) Notes: The denominators for the equations for u a and u b are the same.

and u are the same. If the denominator for the equations for u a and u b is 0 then the two lines are parallel.

and u is 0 then the two lines are parallel. If the denominator and numerator for the equations for u a and u b are 0 then the two lines are coincident.

and u are 0 then the two lines are coincident. The equations apply to lines, if the intersection of line segments is required then it is only necessary to test if u a and u b lie between 0 and 1. Whichever one lies within that range then the corresponding line segment contains the intersection point. If both lie within the range of 0 to 1 then the intersection point is within both line segments. Source code contributions

Original C code by Paul Bourke.

C++ contribution by Damian Coventry.

LISP implementation by Paul Reiners.

C version for Rockbox firmware by Karl Kurbjun.

C# version by Olaf Rabbachin.

VB.net version by Olaf Rabbachin.

VBA implementation by Giuseppe Iaria.

Javascript version by Leo Bottaro.







The shortest line between two lines in 3D Written by Paul Bourke

April 1998 Two lines in 3 dimensions generally don't intersect at a point, they may be parallel (no intersections) or they may be coincident (infinite intersections) but most often only their projection onto a plane intersect.. When they don't exactly intersect at a point they can be connected by a line segment, the shortest line segment is unique and is often considered to be their intersection in 3D. The following will show how to compute this shortest line segment that joins two lines in 3D, it will as a bi-product identify parallel lines. In what follows a line will be defined by two points lying on it, a point on line "a" defined by points P 1 and P 2 has an equation. P a = P 1 + mu a (P 2 - P 1 ) similarly a point on a second line "b" defined by points P 4 and P 4 will be written as P b = P 3 + mu b (P 4 - P 3 ) The values of mu a and mu b range from negative to positive infinity. The line segments between P 1 P 2 and P 3 P 4 have their corresponding mu between 0 and 1. There are two approaches to finding the shortest line segment between lines "a" and "b". The first is to write down the length of the line segment joining the two lines and then find the minimum. That is, minimise the following || P b - P a ||2 Substituting the equations of the lines gives || P 1 - P 3 + mu a (P 2 - P 1 ) - mu b (P 4 - P 3 ) ||2 The above can then be expanded out in the (x,y,z) components. There are conditions to be met at the minimum, the derivative with respect to mu a and mu b must be zero. Note: it is easy to convince oneself that the above function only has one minima and no other minima or maxima. These two equations can then be solved for mu a and mu b , the actual intersection points found by substituting the values of mu into the original equations of the line. An alternative approach but one that gives the exact same equations is to realise that the shortest line segment between the two lines will be perpendicular to the two lines. This allows us to write two equations for the dot product as (P a - P b ) dot (P 2 - P 1 ) = 0 (P a - P b ) dot (P 4 - P 3 ) = 0 Expanding these given the equation of the lines ( P 1 - P 3 + mu a (P 2 - P 1 ) - mu b (P 4 - P 3 ) ) dot (P 2 - P 1 ) = 0 ( P 1 - P 3 + mu a (P 2 - P 1 ) - mu b (P 4 - P 3 ) ) dot (P 4 - P 3 ) = 0 Expanding these in terms of the coordinates (x,y,z) is a nightmare but the result is as follows d 1321 + mu a d 2121 - mu b d 4321 = 0 d 1343 + mu a d 4321 - mu b d 4343 = 0 where d mnop = (x m - x n )(x o - x p ) + (y m - y n )(y o - y p ) + (z m - z n )(z o - z p ) Note that d mnop = d opmn Finally, solving for mu a gives mu a = ( d 1343 d 4321 - d 1321 d 4343 ) / ( d 2121 d 4343 - d 4321 d 4321 ) and back-substituting gives mu b mu b = ( d 1343 + mu a d 4321 ) / d 4343 Source code contributions

Original C source code from the author: lineline.c

Contribution by Dan Wills in MEL (Maya Embedded Language): source.mel.

A Matlab version by Cristian Dima: linelineintersect.m.

A Maxscript function by Chris Johnson: LineLineIntersect.ms

LISP version for AutoCAD (and Intellicad) by Andrew Bennett: int1.lsp and int2.lsp

A contribution by Bruce Vaughan in the form of a Python script for the SDS/2 design software: L3D.py

C# version by Ronald Holthuizen: calclineline.cs

VBA VB6 version by Thomas Ludewig: vbavb6.txt

LabView implementation by John van Schaijk: V01_LineLine3D.vi.zip







Intersection of a plane and a line Written by Paul Bourke

August 1991 Contribution by Bryan Hanson: Implementation in R This note will illustrate the algorithm for finding the intersection of a line and a plane using two possible formulations for a plane. Solution 1 The equation of a plane (points P are on the plane with normal N and point P3 on the plane) can be written as N dot (P - P3) = 0 The equation of the line (points P on the line passing through points P1 and P2) can be written as P = P1 + u (P2 - P1) The intersection of these two occurs when N dot (P1 + u (P2 - P1)) = N dot P3 Solving for u gives Note If the denominator is 0 then the normal to the plane is perpendicular to the line. Thus the line is either parallel to the plane and there are no solutions or the line is on the plane in which case there are an infinite number of solutions

If it is necessary to determine the intersection of the line segment between P1 and P2 then just check that u is between 0 and 1. Solution 2 A plane can also be represented by the equation A x + B y + C z + D = 0 where all points (x,y,z) lie on the plane. Substituting in the equation of the line through points P1 (x1,y1,z1) and P2 (x2,y2,z2) P = P1 + u (P2 - P1) gives A (x1 + u (x2 - x1)) + B (y1 + u (y2 - y1)) + C (z1 + u (z2 - z1)) + D = 0 Solving for u

Note the denominator is 0 then the normal to the plane is perpendicular to the line. Thus the line is either parallel to the plane and there are no solutions or the line is on the plane in which case are infinite solutions

if it is necessary to determine the intersection of the line segment between P1 and P2 then just check that u is between 0 and 1.





Equation of a plane Written by Paul Bourke

March 1989 The standard equation of a plane in 3 space is Ax + By + Cz + D = 0 The normal to the plane is the vector (A,B,C). Given three points in space (x1,y1,z1), (x2,y2,z2), (x3,y3,z3) the equation of the plane through these points is given by the following determinants. Expanding the above gives

A = y1 (z2 - z3) + y2 (z3 - z1) + y3 (z1 - z2)

B = z1 (x2 - x3) + z2 (x3 - x1) + z3 (x1 - x2)

C = x1 (y2 - y3) + x2 (y3 - y1) + x3 (y1 - y2)

- D = x1 (y2 z3 - y3 z2) + x2 (y3 z1 - y1 z3) + x3 (y1 z2 - y2 z1) Note that if the points are collinear then the normal (A,B,C) as calculated above will be (0,0,0). The sign of s = Ax + By + Cz + D determines which side the point (x,y,z) lies with respect to the plane. If s > 0 then the point lies on the same side as the normal (A,B,C). If s < 0 then it lies on the opposite side, if s = 0 then the point (x,y,z) lies on the plane. Alternatively If vector N is the normal to the plane then all points p on the plane satisfy the following N . p = k where . is the dot product between the two vectors. ie: a . b = (a x ,a y ,a z ) . (b x ,b y ,b z ) = a x b x + a y b y + a z b z Given any point a on the plane N . (p - a) = 0





The intersection of two planes Written by Paul Bourke

February 2000 The intersection of two planes (if they are not parallel) is a line. Define the two planes with normals N as N 1 . p = d 1 N 2 . p = d 2 The equation of the line can be written as p = c 1 N 1 + c 2 N 2 + u N 1 * N 2 Where "*" is the cross product, "." is the dot product, and u is the parameter of the line. Taking the dot product of the above with each normal gives two equations with unknowns c 1 and c 2 . N 1 . p = d 1 = c 1 N 1 . N 1 + c 2 N 1 . N 2 N 2 . p = d 2 = c 1 N 1 . N 2 + c 2 N 2 . N 2 Solving for c 1 and c 2

c 1 = ( d 1 N 2 . N 2 - d 2 N 1 . N 2 ) / determinant c 2 = ( d 2 N 1 . N 1 - d 1 N 1 . N 2 ) / determinant determinant = ( N 1 . N 1 ) ( N 2 . N 2 ) - ( N 1 . N 2 )2 Note that a test should first be performed to check that the planes aren't parallel or coincident (also parallel), this is most easily achieved by checking that the cross product of the two normals isn't zero. The planes are parallel if N 1 * N 2 = 0





Intersection of three planes Written by Paul Bourke

October 2001 A contribution by Bruce Vaughan in the form of a Python script for the SDS/2 design software: P3D.py. The intersection of three planes is either a point, a line, or there is no intersection (any two of the planes are parallel). The three planes can be written as N 1 . p = d 1 N 2 . p = d 2 N 3 . p = d 3 In the above and what follows, "." signifies the dot product and "*" is the cross product. The intersection point P is given by: d 1 ( N 2 * N 3 ) + d 2 ( N 3 * N 1 ) + d 3 ( N 1 * N 2 ) P = ------------------------------------------------------------------------- N 1 . ( N 2 * N 3 ) The denominator is zero if N 2 * N 3 = 0, in other words the planes are parallel. Or if N 1 is a linear combination of N 2 and N 3 .





Equation of a line in polar coordinates Written by Paul Bourke

March 2013 How might one represent a line in polar coordinates?

In Cartesian coordinates it can be represented as: The derivation is quite straightforward once one realises that for a point (r, theta) the x axis is simply r cos(theta) and the y axis is r sin(theta). Substituting those into the equation for the line gives the following result. Example from the Graphing Calculator.

Question Given a line defined by two points L1 L2, a point P1 and angle z (bearing from north) find the intersection point between the direction vector from P1 to the line. Short answer: choose a second point P2 along the direction vector from P1, say P2 = (x P1 +sin(z),y P1 +cos(z)). Apply the algorthm here for the intersection of two line segments. Perform the additional test that u b must be greater than 0, the solution where u b is less than 0 is the solution in the direction z+180 degrees.