Planes
A plane in 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 and two non-parallel direction vectors and lying in the plane:
Two parameters, because a plane is two-dimensional. Through three points that are not on one line, take , , .
Normal form
A non-zero vector is normal to the plane if it is perpendicular to every direction in it. A point lies on the plane through exactly when is such a direction:
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: (cross product). From normal to parametric form: solve the one-equation system; the free variables are and (Gauss-Jordan elimination).
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 from the plane is
It is the length of the projection of onto the normal. Its sign tells you which side of the plane is on: positive on the side the normal points to.
Ray–plane intersection
A ray hits the plane where , so
If the ray is parallel to the plane (no hit, or it lies inside the plane). A negative means the plane is behind the ray's origin.
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 ( would be negative). For the plane , the origin lies 2 units on the side opposite to the normal, lies on the plane, and is units away on the normal's side.
Two planes meet in a line
Two non-parallel planes and intersect in a line. Its direction is perpendicular to both normals, so it is ; a point on it comes from solving the system, e.g. with linsolve.
Hyperplanes in
In one linear equation describes a hyperplane, an -dimensional flat with normal . Lines in and planes in are the cases and .
In software
- Ray tracing and picking: floors, walls, mirrors and the faces of boxes are planes; the formula for 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 from any of them.
- Linear classifiers: a perceptron, logistic regression or linear SVM predicts the class of by the sign of , 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 with normal .
Answer
.
Solution
Exercise 2: Plane through three points
Find an equation of the plane through , , .
Answer
(or equivalently ).
Solution
Exercise 3: Distance to a plane
Find the distance from to the plane .
Answer
Solution
Exercise 4: Intersection line of two planes (code)
Find the line where and meet.
Answer
Direction ; for example the line .
Solution
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 as the parameter: direction , which is , the same line.
Exercise 5: Which side? (code)
A linear classifier has and and predicts "spam" when . Classify the points , and .
Answer
spam (score 1), not spam (score ), not spam (score 0, on the boundary, so not ).
Solution
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 ?
A ray with and the plane …
- always intersect once
- are parallel: no hit, or the ray lies in the plane
- intersect at
- intersect at
How do you get a normal of the plane through ?