TExcelTAD
Home › Calculus in Depth › Equations of Lines and Planes

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 LL passes through a point P0P_0 (position vector r0\mathbf{r}_0) parallel to a direction vector v=⟨a,b,c⟩\mathbf{v} = \langle a, b, c \rangle. Every point r\mathbf{r} on LL is r0\mathbf{r}_0 plus a multiple of v\mathbf{v}:

vector formr(t)=r0+tv,t∈Rparametric formx=x0+at,  y=y0+bt,  z=z0+ctsymmetric formx−x0a=y−y0b=z−z0c(if a,b,c≠0)\begin{aligned} &\text{vector form} && \mathbf{r}(t) = \mathbf{r}_0 + t\mathbf{v}, \quad t \in \mathbb{R} \\ &\text{parametric form} && x = x_0 + at,\ \ y = y_0 + bt,\ \ z = z_0 + ct \\ &\text{symmetric form} && \frac{x - x_0}{a} = \frac{y - y_0}{b} = \frac{z - z_0}{c} \quad (\text{if } a, b, c \ne 0) \end{aligned}

The numbers a,b,ca, b, c are the direction numbers. If one of them is 0, say a=0a = 0, write x=x0x = x_0 separately and keep the symmetric form for the other two. The segment from r0\mathbf{r}_0 to r1\mathbf{r}_1 is r(t)=(1−t)r0+tr1\mathbf{r}(t) = (1 - t)\mathbf{r}_0 + t\mathbf{r}_1 for 0≤t≤10 \le t \le 1, which is linear interpolation (lerp).

Two lines in R3\mathbb{R}^3 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 P0P_0 and a nonzero normal vector n=⟨a,b,c⟩\mathbf{n} = \langle a, b, c \rangle perpendicular to it. A point PP lies on the plane exactly when the vector from P0P_0 to PP is perpendicular to n\mathbf{n}:

n⋅(r−r0)=0a(x−x0)+b(y−y0)+c(z−z0)=0→ax+by+cz+d=0\begin{aligned} &\mathbf{n} \cdot (\mathbf{r} - \mathbf{r}_0) = 0 \\ &a(x - x_0) + b(y - y_0) + c(z - z_0) = 0 \quad\to\quad ax + by + cz + d = 0 \end{aligned}

So the coefficients of x,y,zx, y, z in a linear equation are always a normal vector. Through three non-collinear points P,Q,RP, Q, R, a normal is n=PQ→×PR→\mathbf{n} = \overrightarrow{PQ} \times \overrightarrow{PR}.

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 n1×n2\mathbf{n}_1 \times \mathbf{n}_2, since it lies in both planes and is therefore perpendicular to both normals.

Distances

Point to plane. The distance from P1(x1,y1,z1)P_1(x_1, y_1, z_1) to the plane ax+by+cz+d=0ax + by + cz + d = 0 is

D=∣ax1+by1+cz1+d∣a2+b2+c2D = \frac{\lvert ax_1 + by_1 + cz_1 + d \rvert}{\sqrt{a^2 + b^2 + c^2}}

Proof. Pick any point P0P_0 on the plane and let b=P0P1→\mathbf{b} = \overrightarrow{P_0P_1}. The distance is the length of the projection of b\mathbf{b} onto the normal: D=∣comp⁡nb∣=∣n⋅b∣/∣n∣D = \lvert \operatorname{comp}_{\mathbf{n}} \mathbf{b} \rvert = \lvert \mathbf{n} \cdot \mathbf{b} \rvert / \lvert \mathbf{n} \rvert. Now n⋅b=a(x1−x0)+b(y1−y0)+c(z1−z0)=ax1+by1+cz1−(ax0+by0+cz0)\mathbf{n} \cdot \mathbf{b} = a(x_1 - x_0) + b(y_1 - y_0) + c(z_1 - z_0) = ax_1 + by_1 + cz_1 - (ax_0 + by_0 + cz_0), and ax0+by0+cz0=−dax_0 + by_0 + cz_0 = -d because P0P_0 lies on the plane. ∎

Without the absolute value, the numerator is a signed distance (times ∣n∣\lvert \mathbf{n} \rvert): positive on the side n\mathbf{n} points to, negative on the other.

Skew lines. Lines r1+tv1\mathbf{r}_1 + t\mathbf{v}_1 and r2+sv2\mathbf{r}_2 + s\mathbf{v}_2 with v1×v2≠0\mathbf{v}_1 \times \mathbf{v}_2 \ne \mathbf{0} lie in parallel planes with common normal n=v1×v2\mathbf{n} = \mathbf{v}_1 \times \mathbf{v}_2. Their distance is the distance between those planes:

D=∣(r2−r1)⋅(v1×v2)∣∣v1×v2∣D = \frac{\lvert (\mathbf{r}_2 - \mathbf{r}_1) \cdot (\mathbf{v}_1 \times \mathbf{v}_2) \rvert}{\lvert \mathbf{v}_1 \times \mathbf{v}_2 \rvert}

If D=0D = 0, the non-parallel lines intersect.

python · try it yourself
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 tt and ss) agree.

Intersecting a line with a plane: the ray test

Substitute r(t)=o+td\mathbf{r}(t) = \mathbf{o} + t\mathbf{d} into n⋅r+d0=0\mathbf{n} \cdot \mathbf{r} + d_0 = 0 (writing the plane's constant as d0d_0):

n⋅o+t(n⋅d)+d0=0→t=−n⋅o+d0n⋅d\mathbf{n} \cdot \mathbf{o} + t(\mathbf{n} \cdot \mathbf{d}) + d_0 = 0 \quad\to\quad t = -\frac{\mathbf{n} \cdot \mathbf{o} + d_0}{\mathbf{n} \cdot \mathbf{d}}
  • If n⋅d=0\mathbf{n} \cdot \mathbf{d} = 0, the ray is parallel to the plane: there is no hit, or the ray lies in the plane.
  • For a ray (a half-line, t≥0t \ge 0) the hit counts only if t≥0t \ge 0. A negative tt is behind the camera.
  • The hit point is o+td\mathbf{o} + t\mathbf{d}. With a unit d\mathbf{d}, tt is the distance travelled.
python · try it yourself
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 n⋅p+d\mathbf{n} \cdot \mathbf{p} + d says which side of a plane a point p\mathbf{p} 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 w⋅x+b=0\mathbf{w} \cdot \mathbf{x} + b = 0 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 (2,−1,3)(2, -1, 3) and (5,1,−1)(5, 1, -1). Where does it cross the xyxy-plane?

Answer

x=2+3tx = 2 + 3t, y=−1+2ty = -1 + 2t, z=3−4tz = 3 - 4t; (x−2)/3=(y+1)/2=(z−3)/(−4)(x - 2)/3 = (y + 1)/2 = (z - 3)/(-4). It crosses z=0z = 0 at (17/4,1/2,0)(17/4, 1/2, 0).

Solution
v=⟨5−2,1−(−1),−1−3⟩=⟨3,2,−4⟩z=0→3−4t=0→t=3/4x=2+9/4=17/4,y=−1+3/2=1/2\begin{aligned} \mathbf{v} &= \langle 5 - 2, 1 - (-1), -1 - 3 \rangle = \langle 3, 2, -4 \rangle \\ z = 0 &\to 3 - 4t = 0 \to t = 3/4 \\ x &= 2 + 9/4 = 17/4, \qquad y = -1 + 3/2 = 1/2 \end{aligned}

Exercise 2: Plane through three points

Find an equation of the plane through P(1,0,0)P(1, 0, 0), Q(0,2,0)Q(0, 2, 0), R(0,0,3)R(0, 0, 3).

Answer

6x+3y+2z=66x + 3y + 2z = 6, i.e. x/1+y/2+z/3=1x/1 + y/2 + z/3 = 1.

Solution
PQ→=⟨−1,2,0⟩,PR→=⟨−1,0,3⟩n=PQ→×PR→=⟨2⋅3−0⋅0,0⋅(−1)−(−1)⋅3,(−1)⋅0−2⋅(−1)⟩=⟨6,3,2⟩6(x−1)+3y+2z=0→6x+3y+2z=6\begin{aligned} \overrightarrow{PQ} &= \langle -1, 2, 0 \rangle, \qquad \overrightarrow{PR} = \langle -1, 0, 3 \rangle \\ \mathbf{n} &= \overrightarrow{PQ} \times \overrightarrow{PR} = \langle 2 \cdot 3 - 0 \cdot 0, 0 \cdot (-1) - (-1) \cdot 3, (-1) \cdot 0 - 2 \cdot (-1) \rangle = \langle 6, 3, 2 \rangle \\ &6(x - 1) + 3y + 2z = 0 \quad\to\quad 6x + 3y + 2z = 6 \end{aligned}

Exercise 3: Angle and line of intersection

Find the angle between the planes x+y+z=1x + y + z = 1 and x−2y+3z=1x - 2y + 3z = 1, and a direction vector of their line of intersection.

Answer

cos⁡θ=2/42\cos\theta = 2/\sqrt{42}, so θ≈72.02∘\theta \approx 72.02^\circ; direction ⟨5,−2,−3⟩\langle 5, -2, -3 \rangle.

Solution
n1⋅n2=1−2+3=2,∣n1∣=3,∣n2∣=14cos⁡θ=2/42≈0.3086→θ≈72.02∘n1×n2=⟨1⋅3−1⋅(−2),1⋅1−1⋅3,1⋅(−2)−1⋅1⟩=⟨5,−2,−3⟩\begin{aligned} \mathbf{n}_1 \cdot \mathbf{n}_2 &= 1 - 2 + 3 = 2, \qquad \lvert \mathbf{n}_1 \rvert = \sqrt{3}, \qquad \lvert \mathbf{n}_2 \rvert = \sqrt{14} \\ \cos\theta &= 2/\sqrt{42} \approx 0.3086 \quad\to\quad \theta \approx 72.02^\circ \\ \mathbf{n}_1 \times \mathbf{n}_2 &= \langle 1 \cdot 3 - 1 \cdot (-2), 1 \cdot 1 - 1 \cdot 3, 1 \cdot (-2) - 1 \cdot 1 \rangle = \langle 5, -2, -3 \rangle \end{aligned}

Exercise 4: Distance between parallel planes

Find the distance between 2x−y+2z=32x - y + 2z = 3 and 4x−2y+4z=−124x - 2y + 4z = -12.

Answer

3

Solution

The second plane is 2x−y+2z=−62x - y + 2z = -6. Take the point (0,−3,0)(0, -3, 0) on the first plane and use the point–plane formula with the second: ∣0+3+0+6∣/4+1+4=9/3=3\lvert 0 + 3 + 0 + 6 \rvert/\sqrt{4 + 1 + 4} = 9/3 = 3.

Exercise 5: Signed side test (code)

For the plane x+2y−2z−4=0x + 2y - 2z - 4 = 0, classify the points (4,1,0)(4, 1, 0), (0,0,0)(0, 0, 0) and (2,1,0)(2, 1, 0) as in front (the side n\mathbf{n} points to), behind, or on the plane, with their signed distances.

Answer

(4,1,0)(4, 1, 0): front, +2/3+2/3. (0,0,0)(0, 0, 0): behind, −4/3-4/3. (2,1,0)(2, 1, 0): on the plane, 0.

Solution
python · try it yourself
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 3x−y+5z=73x - y + 5z = 7?

  1. ⟨3,−1,5⟩\langle 3, -1, 5 \rangle
  2. ⟨7,7,7⟩\langle 7, 7, 7 \rangle
  3. ⟨3,1,5⟩\langle 3, 1, 5 \rangle
  4. ⟨−1,5,7⟩\langle -1, 5, 7 \rangle

Lines in R3\mathbb{R}^3 that are not parallel and do not intersect are called…

  1. orthogonal
  2. coplanar
  3. skew
  4. collinear

A ray o+td\mathbf{o} + t\mathbf{d} hits a plane with n⋅d=0\mathbf{n} \cdot \mathbf{d} = 0. What happens?

  1. It hits at t=0t = 0
  2. It always hits at t=1t = 1
  3. The formula gives a negative tt
  4. The ray is parallel to the plane: no single hit point