# Projecting a point onto a vector (2D)

I'm a bit rusty on my linear algebra, so I'm hoping someone can help. I'm trying to project a point onto a vector.

I've seen the following formula to project a vector (a) onto a vector (b):

proj.x = ( (a.x*b.x + a.y*b.y) / (b.x*b.x + b.y*b.y) ) * b.x;
proj.y = ( (a.x*b.x + a.y*b.y) / (b.x*b.x + b.y*b.y) ) * b.y;


However, I'm looking at another example of projecting a point (p) onto vector (a):

a.x * (p.x + tx) + a.y * (p.y + ty)) / (a.x * a.x + a.y * a.y);


The points which will be projected make up a convex shape, a triangle. Is this a standard formula? What do tx and ty represent?

-

Consider $\vec{a} = (a_x, a_y), \vec{b} = (b_x, b_y) \in \mathbb{R}^2$. We can rewrite the first formula as $$\vec{p} = \left( \vec{a} \cdot \frac{\vec{b}}{\|\vec{b}\|}\right) \frac{\vec{b}}{\|\vec{b}\|} = \frac{\vec{a} \cdot \vec{b}}{\| b \|^2} \vec{b}.$$ (Here $\vec{a} \cdot \vec{b} := a_x b_x + a_y b_y$ is the dot product of vectors in $\mathbb{R}^2$, and $\| \vec{a} \| := \sqrt{a_x^2 + a_y^2}$ denotes the vector norm or length, in case you didn't know).
This is a standard formula, and $\vec{p}$ is called the orthogonal projection $\vec{a}$ onto the line spanned by the vector $\vec{b}$ (the reason for the name is that $\vec{a} - \vec{p}$ is perpendicular to $\vec{b}$, i.e. $(\vec{a} - \vec{p})\cdot \vec{b} = 0$).
I am not sure what the second formula is supposed to be; it looks like you have forgotten a parenthesis, so I guess it is supposed to be $$\frac{a_x (o_x + tx) + a_y(o_y + ty)}{\| a \|^2}$$ (I have replaced $p_x$ and $p_y$ with $o_x$ and $o_y$ in order to avoid notational conflicts). If all quantities involved are numbers, then this is also a number, so it cannot be the complete formula for a projection. Provided you've copied it correctly, I'd say it looks like the scalar part of the formula for the orthogonal projection given above, but where the vector being projected is $(o_x + tx, o_y + ty) = (o_x, o_y) + t(x,y)$ (this would correspond to specifying the vector using a point of origin $(o_x, o_y)$, a direction vector $(x,y)$ and a distance $t$ along the direction vector). But then the formula should read $$\frac{a_x (o_x + tx) + a_y(o_y + ty)}{\| a \|^2} (a_x, a_y).$$ If that is the case, it is equivalent to the previously given formula; the only difference is that the coordinates of the vector being projected is specified differently.
@user987280: Actually, the vector that we're projecting onto is being normalized (so you didn't forget to mention this - it is part of the original formula); see the formula for $\vec{p}$, and note that $\vec{b}/\| \vec{b} \|$ is the normalization of $\vec{b}$. And yes, your calculations are correct; the result can simplified to $\frac{14}{5}(1,2)$. – Martin Wanvik Feb 14 '12 at 10:46
@user987280: I looked at that tutorial, and I think you're right. The mathematics section appears to have been written by someone who doesn't quite understand mathematics (for instance, he writes $\operatorname{proj}(\vec{A},\vec{B})$ for something that is first a vector quantity, and later a scalar quantity). He also starts out using the wrong projection formula, but it looks like it might not matter in the end, because of the assumption that the vectors are normalized, and the particular problem he is using it for (he only cares about whether the projected shapes overlap). – Martin Wanvik Feb 14 '12 at 12:46