Equations of Lines and Planes
A line in space is a point plus a direction, and a plane is a point plus a normal vector. Once you write them that way, the dot and cross products answer the classic questions: where things intersect, at what angle, and how far apart they are. This lesson derives these formulas and then uses them for the ray–plane test behind ray tracing and game picking.
For a gentler first look at vectors and the dot and cross products, see Vectors and Dot and Cross Products in the survey course.
Lines
A line passes through a point (position vector ) parallel to a direction vector . Every point on is plus a multiple of :
The numbers are the direction numbers. If one of them is 0, say , write separately and keep the symmetric form for the other two. The segment from to is for , which is linear interpolation (lerp).
Two lines in can be parallel (their directions are parallel), intersecting, or skew: not parallel and never meeting. Skew lines do not exist in the plane.
Planes
A plane is fixed by a point and a nonzero normal vector perpendicular to it. A point lies on the plane exactly when the vector from to is perpendicular to :
So the coefficients of in a linear equation are always a normal vector. Through three non-collinear points , a normal is .
Two planes are parallel if their normals are parallel. Otherwise they meet in a line, and the angle between the planes is the angle between their normals. The line of intersection has direction , since it lies in both planes and is therefore perpendicular to both normals.
Distances
Point to plane. The distance from to the plane is
Proof. Pick any point on the plane and let . The distance is the length of the projection of onto the normal: . Now , and because lies on the plane. ∎
Without the absolute value, the numerator is a signed distance (times ): positive on the side points to, negative on the other.
Skew lines. Lines and with lie in parallel planes with common normal . Their distance is the distance between those planes:
If , the non-parallel lines intersect.
import numpy as np
r1, v1 = np.array([1.0, 0.0, 2.0]), np.array([1.0, 2.0, -1.0])
r2, v2 = np.array([0.0, 3.0, 0.0]), np.array([2.0, 0.0, 1.0])
n = np.cross(v1, v2)
D = abs((r2 - r1) @ n) / np.linalg.norm(n)
print("n = v1 x v2 =", n, " distance =", round(float(D), 6))
# closest points: solve for t, s so that the connecting vector is ⟂ to both directions
A = np.array([[v1 @ v1, -(v1 @ v2)], [v1 @ v2, -(v2 @ v2)]])
t, s = np.linalg.solve(A, [(r2 - r1) @ v1, (r2 - r1) @ v2])
p, q = r1 + t * v1, r2 + s * v2
print("closest points:", np.round(p, 4), np.round(q, 4), " |pq| =", round(float(np.linalg.norm(q - p)), 6))n = v1 x v2 = [ 2. -3. -4.] distance = 0.557086
closest points: [2.3448 2.6897 0.6552] [2.1379 3. 1.069 ] |pq| = 0.557086
Two different methods (the projection formula, and minimising over and ) agree.
Intersecting a line with a plane: the ray test
Substitute into (writing the plane's constant as ):
- If , the ray is parallel to the plane: there is no hit, or the ray lies in the plane.
- For a ray (a half-line, ) the hit counts only if . A negative is behind the camera.
- The hit point is . With a unit , is the distance travelled.
import numpy as np
def ray_plane(o, d, n, d0, eps=1e-9):
denom = n @ d
if abs(denom) < eps:
return None # parallel
t = -(n @ o + d0) / denom
return (float(t), o + t * d) if t >= 0 else None
n, d0 = np.array([0.0, 1.0, 0.0]), 0.0 # ground plane y = 0
eye = np.array([0.0, 2.0, 0.0])
for d in [np.array([0.0, -1.0, 2.0]), np.array([1.0, 0.0, 0.0]), np.array([0.0, 1.0, 1.0])]:
d = d / np.linalg.norm(d)
hit = ray_plane(eye, d, n, d0)
print(np.round(d, 3), "->", "miss" if hit is None else (round(hit[0], 4), np.round(hit[1], 4)))[ 0. -0.447 0.894] -> (4.4721, array([0., 0., 4.]))
[1. 0. 0.] -> miss
[0. 0.707 0.707] -> miss
In software
- Ray tracing and picking. Every ray tracer intersects rays with planes, and the ray–triangle test starts with the ray–plane test followed by an inside check. Clicking on a 3D ground plane in an RTS or a level editor converts the mouse position to a ray and calls exactly this function (Ray–Plane, Ray–Triangle, Screen to World).
- Half-space tests. The sign of says which side of a plane a point is on. Frustum culling tests an object's bounding volume against six planes. Convex collision shapes are intersections of half-spaces, and BSP trees split space recursively by planes.
- Closest points. The skew-line computation is the core of capsule–capsule collision: two segments plus a radius (Closest Point).
- Machine learning. A linear classifier's decision boundary is a hyperplane, and the signed-distance formula gives each point's margin, which is the quantity an SVM maximises (SVM).
Exercises
Exercise 1: Line through two points
Find parametric and symmetric equations for the line through and . Where does it cross the -plane?
Answer
, , ; . It crosses at .
Solution
Exercise 2: Plane through three points
Find an equation of the plane through , , .
Answer
, i.e. .
Solution
Exercise 3: Angle and line of intersection
Find the angle between the planes and , and a direction vector of their line of intersection.
Answer
, so ; direction .
Solution
Exercise 4: Distance between parallel planes
Find the distance between and .
Answer
3
Solution
The second plane is . Take the point on the first plane and use the point–plane formula with the second: .
Exercise 5: Signed side test (code)
For the plane , classify the points , and as in front (the side points to), behind, or on the plane, with their signed distances.
Answer
: front, . : behind, . : on the plane, 0.
Solution
from fractions import Fraction as Fr
n, d = (1, 2, -2), -4
norm = 3 # √(1 + 4 + 4)
for p in [(4, 1, 0), (0, 0, 0), (2, 1, 0)]:
s = Fr(sum(a * b for a, b in zip(n, p)) + d, norm)
print(p, s, "front" if s > 0 else "behind" if s < 0 else "on plane")(4, 1, 0) 2/3 front
(0, 0, 0) -4/3 behind
(2, 1, 0) 0 on plane
Test yourself
What is a normal vector of the plane ?
Lines in that are not parallel and do not intersect are called…
- orthogonal
- coplanar
- skew
- collinear
A ray hits a plane with . What happens?
- It hits at
- It always hits at
- The formula gives a negative
- The ray is parallel to the plane: no single hit point