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.

  • $\begingroup$ Thanks for your detailed reply. I forgot to mention a critical point in my original post - the vector that the point will be projected onto will have been normalized. Based on your formula above, it looks as though the formula reduces to (a⃗ ⋅ b⃗) b⃗. That is, the dot product of a and b multiplied by vector b. Is it possible to go through an example? Let's say I have a point (4, 5) and I want to project it onto the vector (1, 2). First, I would normalize the vector: sqrt(1^2 + 2^2) = sqrt (5). The normalized vector would be (1 / sqrt(5), 2 / sqrt(5)). The dot product would be: $\endgroup$ – user987280 Feb 14 '12 at 10:09
  • $\begingroup$ ( (4 * (1 / sqrt(5)) + (5 * (2 / sqrt(5)) ). In the end, the projected point would be equal to that dot product multiplied by the normalized vector, or: ( (4 * (1 / sqrt(5)) + (5 * (2 / sqrt(5)) ) * (1 / sqrt(5), 2 / sqrt(5)). Is this correct? $\endgroup$ – user987280 Feb 14 '12 at 10:10
  • $\begingroup$ @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)$. $\endgroup$ – Martin Wanvik Feb 14 '12 at 10:46
  • $\begingroup$ The final projected point of (4, 5) on vector (1, 2) is (2.8, 5.6). Thanks for your help, I believe that I understand the concept now. I was a little confused by the short "Mathematics" section of this tutorial: content.gpwiki.org/index.php/… . I'm not sure, but it seems like it might have incorrect information. $\endgroup$ – user987280 Feb 14 '12 at 12:19
  • $\begingroup$ @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). $\endgroup$ – Martin Wanvik Feb 14 '12 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.