TExcelTAD

Planes

A plane in R3\mathbb{R}^3 can be described in two ways: by a point and two directions you may move in (parametric form), or by a point and one direction that is perpendicular to the plane, its normal (normal form). Converting between the two uses the cross product, and the normal form leads straight to ray–plane intersection, point–plane distance and the decision boundaries of linear classifiers.

Parametric form

Given a point p\mathbf{p} and two non-parallel direction vectors u\mathbf{u} and v\mathbf{v} lying in the plane:

Π={p+s⋅u+t⋅v : s,t∈R}\Pi = \{ \mathbf{p} + s \cdot \mathbf{u} + t \cdot \mathbf{v} \ :\ s, t \in \mathbb{R} \}

Two parameters, because a plane is two-dimensional. Through three points A,B,CA, B, C that are not on one line, take p=A\mathbf{p} = A, u=B−A\mathbf{u} = B - A, v=C−A\mathbf{v} = C - A.

Normal form

A non-zero vector n\mathbf{n} is normal to the plane if it is perpendicular to every direction in it. A point x\mathbf{x} lies on the plane through p\mathbf{p} exactly when x−p\mathbf{x} - \mathbf{p} is such a direction:

n⋅(x−p)=0  ⟺  a⋅x+b⋅y+c⋅z=dwith (a,b,c)=n,d=n⋅p\mathbf{n} \cdot (\mathbf{x} - \mathbf{p}) = 0 \iff a \cdot x + b \cdot y + c \cdot z = d \qquad \text{with } (a, b, c) = \mathbf{n}, \quad d = \mathbf{n} \cdot \mathbf{p}

So the solution set of one linear equation in three unknowns is a plane, and its coefficients are a normal vector. That links back to the geometric view of systems: solving three equations in three unknowns is intersecting three planes.

From parametric to normal form: n=u×v\mathbf{n} = \mathbf{u} \times \mathbf{v} (cross product). From normal to parametric form: solve the one-equation system; the free variables are ss and tt (Gauss-Jordan elimination).

python · try it yourself
from sympy import Matrix, symbols

def plane_through(A, B, C):
    A, B, C = map(Matrix, (A, B, C))
    n = (B - A).cross(C - A)
    if n == Matrix([0, 0, 0]):
        raise ValueError("points are collinear")
    return n, n.dot(A)

n, d = plane_through([1, 0, 0], [0, 2, 0], [0, 0, 3])
x, y, z = symbols("x y z")
print("normal:", list(n), " equation:", n.dot(Matrix([x, y, z])), "=", d)

for P in ([1, 0, 0], [0, 2, 0], [0, 0, 3], [1, 1, 1]):
    print(P, "on plane:", n.dot(Matrix(P)) == d)
normal: [6, 3, 2]  equation: 6*x + 3*y + 2*z = 6
[1, 0, 0] on plane: True
[0, 2, 0] on plane: True
[0, 0, 3] on plane: True
[1, 1, 1] on plane: False

Distance from a point to a plane

The signed distance of a point XX from the plane n⋅x=d\mathbf{n} \cdot \mathbf{x} = d is

dist⁡(X)=n⋅X−d∥n∥\operatorname{dist}(X) = \frac{\mathbf{n} \cdot X - d}{\lVert \mathbf{n} \rVert}

It is the length of the projection of X−pX - \mathbf{p} onto the normal. Its sign tells you which side of the plane XX is on: positive on the side the normal points to.

Ray–plane intersection

A ray o+t⋅r\mathbf{o} + t \cdot \mathbf{r} hits the plane n⋅x=d\mathbf{n} \cdot \mathbf{x} = d where n⋅(o+t⋅r)=d\mathbf{n} \cdot (\mathbf{o} + t \cdot \mathbf{r}) = d, so

t=d−n⋅on⋅r(if n⋅r≠0)t = \frac{d - \mathbf{n} \cdot \mathbf{o}}{\mathbf{n} \cdot \mathbf{r}} \qquad (\text{if } \mathbf{n} \cdot \mathbf{r} \ne 0)

If n⋅r=0\mathbf{n} \cdot \mathbf{r} = 0 the ray is parallel to the plane (no hit, or it lies inside the plane). A negative tt means the plane is behind the ray's origin.

python · try it yourself
import numpy as np

def ray_plane(o, r, n, d, eps=1e-12):
    denom = np.dot(n, r)
    if abs(denom) < eps:
        return None                       # parallel
    t = (d - np.dot(n, o)) / denom
    return None if t < 0 else (t, o + t * r)

n, d = np.array([0.0, 1.0, 0.0]), 0.0     # the floor y = 0
eye = np.array([0.0, 2.0, 0.0])
for r in ([1.0, -1.0, 0.0], [0.0, -1.0, 2.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]):
    hit = ray_plane(eye, np.array(r), n, d)
    print(r, "->", "miss" if hit is None else f"t = {hit[0]}, point {hit[1]}")

# signed distance of points from the plane x + 2y + 2z = 6
m = np.array([1.0, 2.0, 2.0])
for X in ([0, 0, 0], [2, 2, 0], [4, 4, 4]):
    print(X, "signed distance:", round(float((m @ np.array(X) - 6) / np.linalg.norm(m)), 4))
[1.0, -1.0, 0.0] -> t = 2.0, point [2. 0. 0.]
[0.0, -1.0, 2.0] -> t = 2.0, point [0. 0. 4.]
[1.0, 0.0, 0.0] -> miss
[0.0, 1.0, 0.0] -> miss
[0, 0, 0] signed distance: -2.0
[2, 2, 0] signed distance: 0.0
[4, 4, 4] signed distance: 4.6667

The first two rays hit the floor; the third is parallel to it and the fourth points away from it (tt would be negative). For the plane x+2y+2z=6x + 2y + 2z = 6, the origin lies 2 units on the side opposite to the normal, (2,2,0)(2, 2, 0) lies on the plane, and (4,4,4)(4, 4, 4) is 4234\tfrac{2}{3} units away on the normal's side.

Two planes meet in a line

Two non-parallel planes n1⋅x=d1\mathbf{n}_1 \cdot \mathbf{x} = d_1 and n2⋅x=d2\mathbf{n}_2 \cdot \mathbf{x} = d_2 intersect in a line. Its direction is perpendicular to both normals, so it is n1×n2\mathbf{n}_1 \times \mathbf{n}_2; a point on it comes from solving the 2×32 \times 3 system, e.g. with linsolve.

Hyperplanes in Rn\mathbb{R}^n

In Rn\mathbb{R}^n one linear equation w1x1+⋯+wnxn=dw_1x_1 + \cdots + w_nx_n = d describes a hyperplane, an (n−1)(n - 1)-dimensional flat with normal w\mathbf{w}. Lines in R2\mathbb{R}^2 and planes in R3\mathbb{R}^3 are the cases n=2n = 2 and n=3n = 3.

In software

  • Ray tracing and picking: floors, walls, mirrors and the faces of boxes are planes; the formula for tt above is the core of Game Math: Ray–Plane. Triangles are tested by first hitting their plane.
  • Frustum culling: a camera's view volume is bounded by 6 planes; an object is skipped when its bounding sphere has signed distance<−radius\text{signed distance} < -\text{radius} from any of them.
  • Linear classifiers: a perceptron, logistic regression or linear SVM predicts the class of x\mathbf{x} by the sign of w⋅x+b\mathbf{w} \cdot \mathbf{x} + b, the signed side of a hyperplane. Training moves that hyperplane (SVM, Perceptron).
  • Clipping and CSG: polygons are cut against planes using the signed distances of their vertices.

See also Lines and Planes in Space.

Exercises

Exercise 1: Equation from point and normal

Find the equation of the plane through (2,−1,3)(2, -1, 3) with normal (1,4,−2)(1, 4, -2).

Answer

x+4y−2z=−8x + 4y - 2z = -8.

Solution
d=n⋅p=2−4−6=−8x+4y−2z=−8\begin{aligned} d &= \mathbf{n} \cdot \mathbf{p} = 2 - 4 - 6 = -8 \\ x + 4y - 2z &= -8 \end{aligned}

Exercise 2: Plane through three points

Find an equation of the plane through A=(1,1,1)A = (1, 1, 1), B=(2,1,3)B = (2, 1, 3), C=(1,3,2)C = (1, 3, 2).

Answer

−4x−y+2z=−3-4x - y + 2z = -3 (or equivalently 4x+y−2z=34x + y - 2z = 3).

Solution
B−A=(1,0,2),C−A=(0,2,1)n=(0⋅1−2⋅2, 2⋅0−1⋅1, 1⋅2−0⋅0)=(−4,−1,2)d=n⋅A=−4−1+2=−3Check B: −8−1+6=−3 ✓C: −4−3+4=−3 ✓\begin{aligned} &B - A = (1, 0, 2), \quad C - A = (0, 2, 1) \\ &\mathbf{n} = (0 \cdot 1 - 2 \cdot 2,\ 2 \cdot 0 - 1 \cdot 1,\ 1 \cdot 2 - 0 \cdot 0) = (-4, -1, 2) \\ &d = \mathbf{n} \cdot A = -4 - 1 + 2 = -3 \\ &\text{Check } B\text{: } -8 - 1 + 6 = -3 \ \checkmark \qquad C\text{: } -4 - 3 + 4 = -3 \ \checkmark \end{aligned}

Exercise 3: Distance to a plane

Find the distance from (3,1,4)(3, 1, 4) to the plane 2x−y+2z=12x - y + 2z = 1.

Answer
Solution
n⋅X−d=6−1+8−1=12∥n∥=4+1+4=3distance=12/3=4\begin{aligned} \mathbf{n} \cdot X - d &= 6 - 1 + 8 - 1 = 12 \\ \lVert \mathbf{n} \rVert &= \sqrt{4 + 1 + 4} = 3 \\ \text{distance} &= 12 / 3 = 4 \end{aligned}

Exercise 4: Intersection line of two planes (code)

Find the line where x+y+z=6x + y + z = 6 and x−y+2z=5x - y + 2z = 5 meet.

Answer

Direction n1×n2=(3,−1,−2)\mathbf{n}_1 \times \mathbf{n}_2 = (3, -1, -2); for example the line (112,12,0)+t⋅(3,−1,−2)(\frac{11}{2}, \frac{1}{2}, 0) + t \cdot (3, -1, -2).

Solution
n1×n2=(1⋅2−1⋅(−1), 1⋅1−1⋅2, 1⋅(−1)−1⋅1)=(3,−1,−2)z=0:x+y=6, x−y=5 → x=112, y=12\begin{aligned} &\mathbf{n}_1 \times \mathbf{n}_2 = (1 \cdot 2 - 1 \cdot (-1),\ 1 \cdot 1 - 1 \cdot 2,\ 1 \cdot (-1) - 1 \cdot 1) = (3, -1, -2) \\ &z = 0\text{:} \quad x + y = 6,\ x - y = 5 \ \to\ x = \tfrac{11}{2},\ y = \tfrac{1}{2} \end{aligned}
python · try it yourself
from sympy import Matrix, symbols, linsolve
x, y, z = symbols("x y z")
print(Matrix([1, 1, 1]).cross(Matrix([1, -1, 2])).T)
print(linsolve([x + y + z - 6, x - y + 2*z - 5], [x, y, z]))
Matrix([[3, -1, -2]])
{(11/2 - 3*z/2, z/2 + 1/2, z)}

SymPy uses zz as the parameter: direction (−32,12,1)(-\frac{3}{2}, \frac{1}{2}, 1), which is −12⋅(3,−1,−2)-\frac{1}{2} \cdot (3, -1, -2), the same line.

Exercise 5: Which side? (code)

A linear classifier has w=(2,−1)\mathbf{w} = (2, -1) and b=−1b = -1 and predicts "spam" when w⋅x+b>0\mathbf{w} \cdot \mathbf{x} + b > 0. Classify the points (1,0)(1, 0), (0,1)(0, 1) and (2,3)(2, 3).

Answer

(1,0)(1, 0) spam (score 1), (0,1)(0, 1) not spam (score −2-2), (2,3)(2, 3) not spam (score 0, on the boundary, so not >0> 0).

Solution
python · try it yourself
import numpy as np
w, b = np.array([2, -1]), -1
for x in ([1, 0], [0, 1], [2, 3]):
    score = int(w @ np.array(x) + b)
    print(x, score, "spam" if score > 0 else "not spam")
[1, 0] 1 spam
[0, 1] -2 not spam
[2, 3] 0 not spam

Test yourself

What is a normal vector of the plane 3x−y+5z=23x - y + 5z = 2?

  1. (3,−1,5)(3, -1, 5)
  2. (2,0,0)(2, 0, 0)
  3. (1,3,0)(1, 3, 0)
  4. (3,−1,5,2)(3, -1, 5, 2)

A ray o+t⋅r\mathbf{o} + t \cdot \mathbf{r} with n⋅r=0\mathbf{n} \cdot \mathbf{r} = 0 and the plane n⋅x=d\mathbf{n} \cdot \mathbf{x} = d…

  1. always intersect once
  2. are parallel: no hit, or the ray lies in the plane
  3. intersect at t=0t = 0
  4. intersect at t=dt = d

How do you get a normal of the plane through A,B,CA, B, C?

  1. A+B+CA + B + C
  2. (B−A)⋅(C−A)(B - A) \cdot (C - A)
  3. (B−A)×(C−A)(B - A) \times (C - A)
  4. A×B×CA \times B \times C