TExcelTAD

Lines

A line in Rn\mathbb{R}^n is everything you can reach from one point by moving along one fixed direction, forwards or backwards. The parametric form p+t⋅d\mathbf{p} + t \cdot \mathbf{d} 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 p\mathbf{p} on the line and a non-zero direction vector d\mathbf{d}, the line is

L={p+t⋅d : t∈R}L = \{ \mathbf{p} + t \cdot \mathbf{d} \ :\ t \in \mathbb{R} \}

Each value of the parameter tt gives one point: t=0t = 0 gives p\mathbf{p}, t=1t = 1 gives p+d\mathbf{p} + \mathbf{d}, t=−2t = -2 gives p−2d\mathbf{p} - 2\mathbf{d}. Written component by component in R3\mathbb{R}^3 this is three equations, the parametric equations of the line:

x=p1+t⋅d1,y=p2+t⋅d2,z=p3+t⋅d3x = p_1 + t \cdot d_1, \quad y = p_2 + t \cdot d_2, \quad z = p_3 + t \cdot d_3

Through two points PP and QQ: use p=P\mathbf{p} = P and d=Q−P\mathbf{d} = Q - P. Then t=0t = 0 is PP, t=1t = 1 is QQ, and 0≤t≤10 \le t \le 1 is the segment between them. Restricting to t≥0t \ge 0 gives a ray that starts at PP.

The description is not unique: any point of the line can serve as p\mathbf{p}, and any non-zero multiple of d\mathbf{d} as direction. (1,2)+t⋅(3,1)(1, 2) + t \cdot (3, 1) and (7,4)+s⋅(−6,−2)(7, 4) + s \cdot (-6, -2) are the same line.

Lines in the plane: the equation form

In R2\mathbb{R}^2 a line can also be written as one linear equation a⋅x+b⋅y=ca \cdot x + b \cdot y = c. The vector n=(a,b)\mathbf{n} = (a, b) is a normal vector: it is perpendicular to the line. To see this, take two points P,QP, Q on the line; subtracting their equations gives n⋅(Q−P)=0\mathbf{n} \cdot (Q - P) = 0.

parametric→equation:(1,2)+t⋅(3,1)normal n=(1,−3)(perpendicular to (3,1))n⋅(x,y)=n⋅(1,2) → x−3y=−5\begin{aligned} \text{parametric} \to \text{equation:} \quad & (1, 2) + t \cdot (3, 1) \\ & \text{normal } \mathbf{n} = (1, -3) \quad \text{(perpendicular to } (3, 1)\text{)} \\ & \mathbf{n} \cdot (x, y) = \mathbf{n} \cdot (1, 2) \ \to\ x - 3y = -5 \end{aligned}

In R3\mathbb{R}^3 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 p+t⋅d\mathbf{p} + t \cdot \mathbf{d} with q+s⋅e\mathbf{q} + s \cdot \mathbf{e}, set them equal: p+t⋅d=q+s⋅e\mathbf{p} + t \cdot \mathbf{d} = \mathbf{q} + s \cdot \mathbf{e}, i.e. t⋅d−s⋅e=q−pt \cdot \mathbf{d} - s \cdot \mathbf{e} = \mathbf{q} - \mathbf{p}. That is a linear system in the unknowns tt and ss. In R3\mathbb{R}^3 it has three equations and two unknowns, so it is often inconsistent: two lines in space usually miss each other (they are skew).

python · try it yourself
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 xx-axis and the vertical line through (0,1,0)(0, 1, 0) never meet. (If the lines were identical, linsolve would return tt in terms of ss, and the function would need a third case.)

Distance from a point to a line

The closest point of the line to a point XX is found with the dot product: project X−pX - \mathbf{p} onto d\mathbf{d}.

t∗=(X−p)⋅dd⋅dclosest point C=p+t∗⋅ddistance=∥X−C∥\begin{aligned} t^* &= \frac{(X - \mathbf{p}) \cdot \mathbf{d}}{\mathbf{d} \cdot \mathbf{d}} && \text{closest point } C = \mathbf{p} + t^* \cdot \mathbf{d} \\ \text{distance} &= \lVert X - C \rVert \end{aligned}

At the closest point, X−CX - C is perpendicular to d\mathbf{d}, which is exactly what the projection formula guarantees.

python · try it yourself
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 origin+t⋅direction\text{origin} + t \cdot \text{direction} with t≥0t \ge 0 into the scene; the smallest positive tt 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 position(t)=start+t⋅velocity\text{position}(t) = \text{start} + t \cdot \text{velocity}, and linear interpolation lerp⁡(P,Q,t)=P+t⋅(Q−P)\operatorname{lerp}(P, Q, t) = P + t \cdot (Q - P) 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 P=(2,−1,3)P = (2, -1, 3) and Q=(5,1,2)Q = (5, 1, 2). Which point has t=−1t = -1?

Answer

(2,−1,3)+t⋅(3,2,−1)(2, -1, 3) + t \cdot (3, 2, -1); t=−1t = -1 gives (−1,−3,4)(-1, -3, 4).

Solution
d=Q−P=(3,2,−1)t=−1:(2−3,−1−2,3+1)=(−1,−3,4)\begin{aligned} &\mathbf{d} = Q - P = (3, 2, -1) \\ &t = -1\text{:} \quad (2 - 3, -1 - 2, 3 + 1) = (-1, -3, 4) \end{aligned}

Exercise 2: Is the point on the line?

Is (8,3,1)(8, 3, 1) on the line from Exercise 1? Is (11,5,0)(11, 5, 0)?

Answer

(8,3,1)(8, 3, 1) yes (t=2t = 2); (11,5,0)(11, 5, 0) yes (t=3t = 3).

Solution

Solve each component for tt; the point is on the line when all components give the same tt.

(8,3,1):2+3t=8→t=2;−1+2t=3→t=2;3−t=1→t=2 ✓(11,5,0):t=3, t=3, t=3 ✓\begin{aligned} &(8, 3, 1)\text{:} && 2 + 3t = 8 \to t = 2; \quad -1 + 2t = 3 \to t = 2; \quad 3 - t = 1 \to t = 2 \ \checkmark \\ &(11, 5, 0)\text{:} && t = 3,\ t = 3,\ t = 3 \ \checkmark \end{aligned}

Exercise 3: Equation of a line in the plane

Find an equation a⋅x+b⋅y=ca \cdot x + b \cdot y = c for the line (3,−2)+t⋅(2,5)(3, -2) + t \cdot (2, 5).

Answer

5x−2y=195x - 2y = 19.

Solution
normal perpendicular to (2,5): n=(5,−2)(check: 10−10=0)c=n⋅(3,−2)=15+4=19→5x−2y=19\begin{aligned} &\text{normal perpendicular to } (2, 5)\text{: } \mathbf{n} = (5, -2) && \text{(check: } 10 - 10 = 0\text{)} \\ &c = \mathbf{n} \cdot (3, -2) = 15 + 4 = 19 \\ &\to 5x - 2y = 19 \end{aligned}

Exercise 4: Intersect or skew? (code)

Do the lines (1,1,0)+t⋅(1,0,1)(1, 1, 0) + t \cdot (1, 0, 1) and (0,2,3)+s⋅(1,−1,0)(0, 2, 3) + s \cdot (1, -1, 0) intersect? If so, where?

Answer

No. The system for tt and ss is inconsistent, so the lines are skew.

Solution
x:1+t=s→t−s=−1y:1=2−s→s=1z:t=3→t=3t−s=2≠−1→inconsistent→skew lines\begin{aligned} x\text{:} \quad 1 + t &= s && \to t - s = -1 \\ y\text{:} \quad 1 &= 2 - s && \to s = 1 \\ z\text{:} \quad t &= 3 && \to t = 3 \\ t - s &= 2 \ne -1 && \to \text{inconsistent} \to \text{skew lines} \end{aligned}
python · try it yourself
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 X=(3,3)X = (3, 3) to the line through (0,1)(0, 1) with direction (1,1)(1, 1), and the closest point.

Answer

Closest point (2.5,3.5)(2.5, 3.5), distance 0.5≈0.7071\sqrt{0.5} \approx 0.7071.

Solution
X−p=(3,2),t∗=(3+2)/2=2.5C=(0,1)+2.5⋅(1,1)=(2.5,3.5)X−C=(0.5,−0.5),length 0.5\begin{aligned} X - \mathbf{p} &= (3, 2), \quad t^* = (3 + 2)/2 = 2.5 \\ C &= (0, 1) + 2.5 \cdot (1, 1) = (2.5, 3.5) \\ X - C &= (0.5, -0.5), \quad \text{length } \sqrt{0.5} \end{aligned}
python · try it yourself
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 PP to QQ?

  1. P+t(Q−P), t∈RP + t(Q - P),\ t \in \mathbb{R}
  2. P+t(Q−P), 0≤t≤1P + t(Q - P),\ 0 \le t \le 1
  3. P+t(Q−P), t≥0P + t(Q - P),\ t \ge 0
  4. t⋅P+t⋅Q, 0≤t≤1t \cdot P + t \cdot Q,\ 0 \le t \le 1

Two lines in R3\mathbb{R}^3 that are not parallel and do not meet are called…

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

In 2D, the line 3x−4y=73x - 4y = 7 has which normal vector?

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