Tutorials › Calculus III › Projection Onto a Line Given by Two Points

Dot Products and Projections · Tutorial 60 of 999

Projection Onto a Line Given by Two Points

Use the two defining points of a line to find the nearest point on it to a given point.

Beginner 9 min read

What You'll Learn

  • Build a direction vector from two distinct points on a line.
  • Calculate the parameter for the projection onto the line.
  • Locate the projected point using the line’s parametric equation.
  • Check a projection by verifying perpendicularity.
  • Distinguish projection onto an infinite line from projection onto a segment.
  • Recognize why a non-unit direction vector requires a denominator.

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:

$$ \mathbf{d}=\mathbf{b}-\mathbf{a}. $$

Since \(A\) and \(B\) are distinct, \(\mathbf{d}\ne\mathbf{0}\). Every point on the line can be represented using a real parameter \(t\):

$$ \mathbf{x}(t)=\mathbf{a}+t\mathbf{d},\qquad t\in\mathbb{R}. $$

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

$$ \mathbf{v}=\mathbf{p}-\mathbf{a}. $$

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.

Definition: For distinct points \(A\) and \(B\), define \(\mathbf{d}=\mathbf{b}-\mathbf{a}\). The parameter of the projection of \(P\) onto the line through \(A\) and \(B\) is \(t_*=\dfrac{(\mathbf{p}-\mathbf{a})\cdot\mathbf{d}}{\mathbf{d}\cdot\mathbf{d}}\). The projected point has position vector \(\mathbf{q}=\mathbf{a}+t_*\mathbf{d}\).

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

$$ (\mathbf{v}\cdot\mathbf{u})\mathbf{u} = \left(\mathbf{v}\cdot\frac{\mathbf{d}}{\|\mathbf{d}\|}\right) \frac{\mathbf{d}}{\|\mathbf{d}\|} = \frac{\mathbf{v}\cdot\mathbf{d}}{\|\mathbf{d}\|^2}\mathbf{d} = \frac{\mathbf{v}\cdot\mathbf{d}}{\mathbf{d}\cdot\mathbf{d}}\mathbf{d}. $$

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}\),

$$ (\mathbf{q}-\mathbf{p})\cdot\mathbf{d} = (t_*\mathbf{d}-\mathbf{v})\cdot\mathbf{d} = t_*(\mathbf{d}\cdot\mathbf{d})-\mathbf{v}\cdot\mathbf{d} = 0. $$

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.

Key condition: The two defining points must be distinct, so \(\mathbf{d}\ne\mathbf{0}\) and \(\mathbf{d}\cdot\mathbf{d}>0\). If \(A=B\), the direction vector is zero and the formula cannot be used to define a line.

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:

$$ \mathbf{d}=\mathbf{b}-\mathbf{a} =(4-1,6-2,0-0) =(3,4,0). $$

The displacement from \(A\) to \(P\) is

$$ \mathbf{v}=\mathbf{p}-\mathbf{a} =(2-1,5-2,3-0) =(1,3,3). $$

Calculate the dot products needed for the parameter:

$$ \mathbf{v}\cdot\mathbf{d} =(1)(3)+(3)(4)+(3)(0) =3+12+0=15, $$ $$ \mathbf{d}\cdot\mathbf{d} =(3)(3)+(4)(4)+(0)(0) =9+16+0=25. $$

Therefore,

$$ t_*=\frac{15}{25}=\frac35. $$

The line point at this parameter is

$$ \mathbf{q} =\mathbf{a}+\frac35\mathbf{d} =(1,2,0)+\frac35(3,4,0) =(1,2,0)+\left(\frac95,\frac{12}5,0\right) =\left(\frac{14}5,\frac{22}5,0\right). $$

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\):

$$ \mathbf{q}-\mathbf{p} =\left(\frac{14}5-2,\frac{22}5-5,0-3\right) =\left(\frac45,-\frac35,-3\right), $$ $$ (\mathbf{q}-\mathbf{p})\cdot\mathbf{d} =\frac45(3)-\frac35(4)+(-3)(0) =\frac{12}5-\frac{12}5=0. $$

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

$$ \mathbf{d}=(1-(-1),2-1,2-2)=(2,1,0), $$ $$ \mathbf{v}=(5-(-1),0-1,2-2)=(6,-1,0). $$

Compute the dot products:

$$ \mathbf{v}\cdot\mathbf{d} =(6)(2)+(-1)(1)+(0)(0)=12-1=11, $$ $$ \mathbf{d}\cdot\mathbf{d} =(2)(2)+(1)(1)+(0)(0)=4+1=5. $$

Hence \(t_*=11/5\). The projected point is

$$ \mathbf{q} =(-1,1,2)+\frac{11}5(2,1,0) =(-1,1,2)+\left(\frac{22}5,\frac{11}5,0\right) =\left(\frac{17}5,\frac{16}5,2\right). $$

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

$$ \mathbf{q}-\mathbf{p} =\left(\frac{17}5-5,\frac{16}5-0,2-2\right) =\left(-\frac85,\frac{16}5,0\right). $$

Its dot product with the line direction is

$$ (\mathbf{q}-\mathbf{p})\cdot\mathbf{d} =-\frac85(2)+\frac{16}5(1)+0(0) =-\frac{16}5+\frac{16}5=0. $$

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:

$$ \mathbf{d}=(5-2,1-(-1),3-1)=(3,2,2), $$ $$ \mathbf{v}=(0-2,0-(-1),0-1)=(-2,1,-1). $$

Then

$$ \mathbf{v}\cdot\mathbf{d} =(-2)(3)+(1)(2)+(-1)(2) =-6+2-2=-6, $$ $$ \mathbf{d}\cdot\mathbf{d} =(3)(3)+(2)(2)+(2)(2) =9+4+4=17. $$

Thus \(t_*=-6/17\). Substituting into the line equation gives

$$ \mathbf{q} =(2,-1,1)-\frac6{17}(3,2,2) =(2,-1,1)-\left(\frac{18}{17},\frac{12}{17},\frac{12}{17}\right) =\left(\frac{16}{17},-\frac{29}{17},\frac5{17}\right). $$

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,

$$ (\mathbf{q}-\mathbf{p})\cdot\mathbf{d} = \left(\frac{16}{17},-\frac{29}{17},\frac5{17}\right)\cdot(3,2,2) = \frac{48-58+10}{17} =0. $$

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

$$ (20,10,0)-(5,0,2)=(15,10,-2), $$ $$ (15,10,-2)\cdot(2,1,0)=30+10+0=40\ne0. $$

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

$$ (-1,2,-2)\cdot(2,1,0)=-2+2+0=0. $$

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

1
Find the line direction.
Subtract the position vector of the first line point from that of the second: \(\mathbf{d}=\mathbf{b}-\mathbf{a}\).
2
Find the displacement to the given point.
Calculate \(\mathbf{v}=\mathbf{p}-\mathbf{a}\), using the same starting point \(A\) as in the line equation.
3
Calculate the parameter.
Use \(t_*=(\mathbf{v}\cdot\mathbf{d})/(\mathbf{d}\cdot\mathbf{d})\). The denominator must be nonzero.
4
Locate and check the point.
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.

  1. For \(A=(0,1,0)\), \(B=(2,1,0)\), and \(P=(1,3,4)\), find the parameter and projected point.
  2. Why must the denominator \(\mathbf{d}\cdot\mathbf{d}\) be positive for the formula to apply?
  3. What does a negative projection parameter indicate about the projected point relative to \(A\) and the direction from \(A\) to \(B\)?
  4. 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}\).
  5. Why can a projection onto a line fall outside the segment joining the two points that define the line?