Point, Line, Plane
Point, Line, Plane
net/geometry/pointlineplane/
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
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
1 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
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)
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.
A plane can be defined by its normal n = (A, B, C) and any point on the plane Pb = (xb, yb, zb)
Ax+By+Cz+D=0
The minimum distance between Pa and the plane is given by the absolute value of
To derive this result consider the projection of the line (Pa - Pb) onto the normal of the plane n, that is just ||Pa - Pb||
2 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
cos(theta), where theta is the angle between (Pa - Pb) and the normal n. This projection is the minimum distance of Pa to
the plane.
That is
minimum distance = (A (xa - xb) + B (ya - yb) + C (za - zb)) / sqrt(A2 + B2 + C2) ...2
A xb + B yb + C zb + D = 0 ...3
This note describes the technique and algorithm for determining the intersection point of two lines (or line segments) in 2
dimensions.
Pa = P1 + ua ( P2 - P1 )
Pb = P3 + ub ( P4 - P3 )
Solving for the point where Pa = Pb gives the following two equations in two unknowns (ua and ub)
3 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
and
y1 + ua (y2 - y1) = y3 + ub (y4 - y3)
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 + ua (x2 - x1)
y = y1 + ua (y2 - y1)
Notes:
The denominators for the equations for ua and ub are the same.
If the denominator for the equations for ua and ub is 0 then the two lines are parallel.
If the denominator and numerator for the equations for ua and ub 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 ua and
ub 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.
Two lines in 3 dimensions generally don't intersect at a point, they may be parallel (no intersections) or they may be
4 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
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.
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
|| Pb - Pa ||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 mua and mub 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 mua and mub, 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
5 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
Expanding these in terms of the coordinates (x,y,z) is a nightmare but the result is as follows
where
dmnop = (xm - xn)(xo - xp) + (ym - yn)(yo - yp) + (zm - zn)(zo - zp)
This note will illustrate the algorithm for finding the intersection of a line and a plane using two possible formulations for a
plane.
6 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
So l ut io n 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)
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.
So l ut io n 2
Ax+By+Cz+D=0
Substituting in the equation of the line through points P1 (x1,y1,z1) and P2 (x2,y2,z2)
P = P1 + u (P2 - P1)
gives
Solving for u
7 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
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
Ax + By + Cz + D = 0
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.
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
8 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
N.p=k
N . (p - a) = 0
The intersection of two planes (if they are not parallel) is a line.
N1 . p = d 1
N2 . p = d 2
p = c1 N1 + c2 N2 + u N1 * N2
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 c1 and c2.
N1 . p = d1 = c1 N1 . N1 + c2 N1 . N2
N2 . p = d2 = c1 N1 . N2 + c2 N2 . N2
c1 = ( d1 N2 . N2 - d2 N1 . N2 ) / determinant
c2 = ( d2 N1 . N1 - d1 N1 . N2) / determinant
determinant = ( N1 . N1 ) ( N2 . N2 ) - ( N1 . N2 )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
9 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
N1 * N 2 = 0
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).
N1 . p = d 1
N2 . p = d 2
N3 . 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:
d1 ( N2 * N3 ) + d2 ( N3 * N1 ) + d3 ( N1 * N2 )
P = -------------------------------------------------------------------------
N 1 . ( N2 * N 3 )
The denominator is zero if N2 * N3 = 0, in other words the planes are parallel. Or if N1 is a linear combination of N2 and
N3.
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.
10 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
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.
11 de 12 17/09/2019 12:31
Point, Line, Plane http://paulbourke.net/geometry/pointlineplane/
Short answer: choose a second point P2 along the direction vector from P1, say P2 = (xP1+sin(z),yP1+cos(z)). Apply the
algorthm here for the intersection of two line segments. Perform the additional test that ub must be greater than 0, the
solution where ub is less than 0 is the solution in the direction z+180 degrees.
12 de 12 17/09/2019 12:31