How to find a projection matrix for a line
Projecting a vector onto a line keeps the part that points along the line. We can calculate that part using a dot product. Then we can write the whole operation as a matrix that works for every input vector.
Use a nonzero vector along the line
If points along the line, the projection of is . The denominator divides by the squared length of , so the formula also works when is longer or shorter than a unit vector.
Project a vector onto the line through one, three
Let and . The two dot products are and .
The projection is . The remaining part is , whose dot product with is .
Build a matrix that gives the same projection
For an input , the projection formula gives . Its components are and , so the projection matrix is
Once a vector lies on the line, projecting it again leaves it in place. In matrix form, .