Introduction
A line in space is often specified by two points rather than by a unit direction vector. To project a point onto that line, first use the two points to construct a direction vector. Then project the displacement from a point on the line to the given point onto that direction.
The resulting parameter tells us where the projected point lies along the line. It can be positive, zero, or negative: a line extends in both directions, so the nearest point need not lie between the two points originally used to specify it.
Representing the Line and the Given Point
Let \(A\) and \(B\) be distinct points on a line, with position vectors \(\mathbf{a}\) and \(\mathbf{b}\). Their difference gives a direction vector for the line:
Since \(A\) and \(B\) are distinct, \(\mathbf{d}\ne\mathbf{0}\). Every point on the line can be represented using a real parameter \(t\):
At \(t=0\), this gives \(A\); at \(t=1\), it gives \(B\). Values of \(t\) outside the interval from 0 to 1 are also allowed because the line continues beyond both points.
Now let \(P\) be the point being projected, with position vector \(\mathbf{p}\). The displacement from \(A\) to \(P\) is
The projection we need is the component of \(\mathbf{v}\) in the direction of \(\mathbf{d}\). Because \(\mathbf{d}\) need not have length 1, its squared length appears in the denominator.
Why This Formula Works
The earlier result for projection onto a unit direction says that if \(\mathbf{u}\) is a unit vector, the projection of \(\mathbf{v}\) onto that direction is \((\mathbf{v}\cdot\mathbf{u})\mathbf{u}\). To use that result, normalize the line direction: \(\mathbf{u}=\mathbf{d}/\|\mathbf{d}\|\). Then the projected displacement from \(A\) is
This projected displacement is \(t_*\mathbf{d}\), so its coefficient is exactly the parameter \(t_*\) in the line equation. Adding that displacement to \(\mathbf{a}\) gives the projected point \(\mathbf{q}\).
The projection is also characterized by a perpendicularity check. The displacement from \(P\) to the projected point is \(\mathbf{q}-\mathbf{p}\). Since \(\mathbf{q}=\mathbf{a}+t_*\mathbf{d}\) and \(\mathbf{p}=\mathbf{a}+\mathbf{v}\),
The last equality follows by substituting the formula for \(t_*\). Thus the displacement from \(P\) to \(\mathbf{q}\) is perpendicular to the line’s direction. The unit-direction projection result identifies \(\mathbf{q}\) as the nearest point on the line; the calculation above gives a convenient check on the result.
Worked Examples
Worked Example: Projecting onto a Line in a Coordinate Plane
Let \(A=(1,2,0)\), \(B=(4,6,0)\), and \(P=(2,5,3)\). Find the nearest point on the line through \(A\) and \(B\) to \(P\).
First find the direction vector:
The displacement from \(A\) to \(P\) is
Calculate the dot products needed for the parameter:
Therefore,
The line point at this parameter is
Check that this is on the line: using \(t=3/5\) in \(\mathbf{a}+t\mathbf{d}\) gives the same point. Check perpendicularity using the displacement from \(P\) to \(Q\):
Thus the projected point is \(Q=(14/5,22/5,0)\).
Worked Example: A Projection Beyond the Defining Points
Let \(A=(-1,1,2)\), \(B=(1,2,2)\), and \(P=(5,0,2)\). Find the nearest point on the line through \(A\) and \(B\).
The line direction and the displacement from \(A\) to \(P\) are
Compute the dot products:
Hence \(t_*=11/5\). The projected point is
Since \(t_*>1\), this point lies beyond \(B\) in the direction from \(A\) to \(B\). That is valid: the specified object is the entire line, not just the segment from \(A\) to \(B\).
For a check, the displacement from \(P\) to \(Q\) is
Its dot product with the line direction is
The point \(Q=(17/5,16/5,2)\) lies on the line at \(t=11/5\) and passes the perpendicularity check.
Worked Example: A Projection Before the First Defining Point
Let \(A=(2,-1,1)\), \(B=(5,1,3)\), and \(P=(0,0,0)\). Find the nearest point on the line through \(A\) and \(B\).
Form the direction and displacement vectors:
Then
Thus \(t_*=-6/17\). Substituting into the line equation gives
The negative parameter places \(Q\) beyond \(A\), opposite the direction from \(A\) to \(B\). To check the point, its displacement from \(P\) is simply \(\mathbf{q}\), because \(\mathbf{p}=\mathbf{0}\). Therefore,
The line parameter and the perpendicularity check confirm \(Q=(16/17,-29/17,5/17)\) as the projected point.
A Common Mistake: Treating the Direction as a Unit Vector
The formula for projection onto a unit direction has no squared-length denominator because a unit vector has squared length 1. When the direction vector comes from two arbitrary points, it is not generally a unit vector. Omitting the denominator can therefore give the wrong parameter.
For example, take \(A=(0,0,0)\), \(B=(2,1,0)\), and \(P=(5,0,2)\). Then \(\mathbf{d}=(2,1,0)\) and \(\mathbf{v}=(5,0,2)\). Their dot product is \(10\), but \(\mathbf{d}\cdot\mathbf{d}=5\). The correct parameter is \(t_*=10/5=2\), giving \(Q=(4,2,0)\).
If we wrongly use \(t=10\), we get the line point \((20,10,0)\). It is on the line, but it is not the projection. In fact, the displacement from \(P\) to this false point is
The perpendicularity check exposes the error. For the correct point, the displacement from \(P\) to \(Q\) is \((4,2,0)-(5,0,2)=(-1,2,-2)\), and
Always calculate both \(\mathbf{v}\cdot\mathbf{d}\) and \(\mathbf{d}\cdot\mathbf{d}\), even when the direction components appear simple.
A Reliable Calculation Sequence
Subtract the position vector of the first line point from that of the second: \(\mathbf{d}=\mathbf{b}-\mathbf{a}\).
Calculate \(\mathbf{v}=\mathbf{p}-\mathbf{a}\), using the same starting point \(A\) as in the line equation.
Use \(t_*=(\mathbf{v}\cdot\mathbf{d})/(\mathbf{d}\cdot\mathbf{d})\). The denominator must be nonzero.
Compute \(\mathbf{q}=\mathbf{a}+t_*\mathbf{d}\), then verify that \((\mathbf{q}-\mathbf{p})\cdot\mathbf{d}=0\).
Why the Parameter Matters
The parameter does more than provide a convenient way to write the answer. It tells how the projected point sits relative to the defining points: \(t_*=0\) gives \(A\), \(t_*=1\) gives \(B\), a value between 0 and 1 gives a point between them, and values outside that range give points beyond one of them.
These interpretations concern projection onto an infinite line. If the target is instead the segment from \(A\) to \(B\), the parameter must be restricted to the interval from 0 to 1; simply using the line projection may then produce a point outside the segment. Keep the geometric object in the question clear before interpreting the result.
Check Your Understanding
Use the projection formula and, where requested, check your answer using a dot product.
- For \(A=(0,1,0)\), \(B=(2,1,0)\), and \(P=(1,3,4)\), find the parameter and projected point.
- Why must the denominator \(\mathbf{d}\cdot\mathbf{d}\) be positive for the formula to apply?
- What does a negative projection parameter indicate about the projected point relative to \(A\) and the direction from \(A\) to \(B\)?
- For \(A=(1,0,0)\), \(B=(1,3,0)\), and \(P=(4,1,2)\), calculate \(\mathbf{q}-\mathbf{p}\) and verify that it is perpendicular to \(\mathbf{d}\).
- Why can a projection onto a line fall outside the segment joining the two points that define the line?