Lines
A line in is everything you can reach from one point by moving along one fixed direction, forwards or backwards. The parametric form describes lines in any dimension, and it is the same formula that ray tracers, physics engines and animation code use for rays, segments and straight-line motion.
Parametric form
Given a point on the line and a non-zero direction vector , the line is
Each value of the parameter gives one point: gives , gives , gives . Written component by component in this is three equations, the parametric equations of the line:
Through two points and : use and . Then is , is , and is the segment between them. Restricting to gives a ray that starts at .
The description is not unique: any point of the line can serve as , and any non-zero multiple of as direction. and are the same line.
Lines in the plane: the equation form
In a line can also be written as one linear equation . The vector is a normal vector: it is perpendicular to the line. To see this, take two points on the line; subtracting their equations gives .
In and higher a single linear equation describes a plane, not a line (Planes), so lines there are given parametrically or as the intersection of two planes.
Intersecting two lines
To intersect with , set them equal: , i.e. . That is a linear system in the unknowns and . In it has three equations and two unknowns, so it is often inconsistent: two lines in space usually miss each other (they are skew).
from sympy import Matrix, symbols, linsolve
t, s = symbols("t s")
def intersect(p, d, q, e):
p, d, q, e = map(Matrix, (p, d, q, e))
eqs = list(p + t * d - (q + s * e))
sol = linsolve(eqs, [t, s])
if not sol:
return "no intersection"
(tv, sv), = sol
return f"t = {tv}, s = {sv}, point {list(p + tv * d)}"
print(intersect([1, 0, 2], [1, 1, 0], [0, 3, 2], [2, -1, 0]))
print(intersect([0, 0, 0], [1, 0, 0], [0, 1, 1], [0, 0, 1]))t = 5/3, s = 4/3, point [8/3, 5/3, 2]
no intersection
The second pair is skew: the -axis and the vertical line through never meet. (If the lines were identical, linsolve would return in terms of , and the function would need a third case.)
Distance from a point to a line
The closest point of the line to a point is found with the dot product: project onto .
At the closest point, is perpendicular to , which is exactly what the projection formula guarantees.
import numpy as np
def closest_on_line(p, d, X, segment=False):
t = np.dot(X - p, d) / np.dot(d, d)
if segment:
t = min(1.0, max(0.0, t)) # clamp to the segment P..P+d
C = p + t * d
return t, C, float(np.linalg.norm(X - C))
p, d = np.array([0.0, 0.0, 0.0]), np.array([4.0, 0.0, 3.0])
X = np.array([1.0, 2.0, 7.0])
t, C, dist = closest_on_line(p, d, X)
print("t* =", t, " closest:", C, " distance:", round(dist, 4))
print("perpendicular?", np.dot(X - C, d))
# another point, tested against the segment from p to p + d only
t, C, dist = closest_on_line(p, d, np.array([10.0, 0.0, 0.0]), segment=True)
print("segment: t =", t, " closest:", C, " distance:", round(dist, 4))t* = 1.0 closest: [4. 0. 3.] distance: 5.3852
perpendicular? 0.0
segment: t = 1.0 closest: [4. 0. 3.] distance: 6.7082
In software
- Ray tracing: every pixel shoots a ray with into the scene; the smallest positive at which it hits something is the visible surface (Game Math: Ray–Sphere).
- Picking and selection: clicking in a 3D editor turns the mouse position into a ray; the object closest to that ray is selected (Game Math: Screen to World).
- Animation: constant-velocity motion is , and linear interpolation is the segment formula.
- Collision: capsule colliders and "closest point on segment" queries use the clamped projection above (Game Math: Closest Point).
See also Lines and Planes in Space.
Exercises
Exercise 1: Through two points
Write a parametric form of the line through and . Which point has ?
Answer
; gives .
Solution
Exercise 2: Is the point on the line?
Is on the line from Exercise 1? Is ?
Answer
yes (); yes ().
Solution
Solve each component for ; the point is on the line when all components give the same .
Exercise 3: Equation of a line in the plane
Find an equation for the line .
Answer
.
Solution
Exercise 4: Intersect or skew? (code)
Do the lines and intersect? If so, where?
Answer
No. The system for and is inconsistent, so the lines are skew.
Solution
from sympy import symbols, linsolve
t, s = symbols("t s")
print(linsolve([1 + t - s, 1 - (2 - s), t - 3], [t, s]))EmptySet
Exercise 5: Distance to a line (code)
Find the distance from to the line through with direction , and the closest point.
Answer
Closest point , distance .
Solution
import numpy as np
p, d, X = np.array([0.0, 1.0]), np.array([1.0, 1.0]), np.array([3.0, 3.0])
t = np.dot(X - p, d) / np.dot(d, d)
C = p + t * d
print(C, round(float(np.linalg.norm(X - C)), 4))[2.5 3.5] 0.7071
Test yourself
Which set describes the segment from to ?
Two lines in that are not parallel and do not meet are called…
- orthogonal
- collinear
- coplanar
- skew
In 2D, the line has which normal vector?