# 3D Mathematics Projecting One Vector onto Another

I have a Vector function which takes two Vectors and and attempts to "project" these vectors together. The method used is to separate the larger of the two vectors (the larger value calculated via magnitude) and then take the larger vector's magnitude and obtain the cosine of it, while multiplying the lesser of the vectors by the magnitude of the larger times its cosine divided by the lesser's magnitude. I hope this makes sense. For those which are having trouble understanding this, I'll paste some code...

float projectVector(const Vector3f& v, const Vector3f& n) {

float nmagnitude = n.computeMagnitude(),
vmagnitude = v.computeMagnitude(),
II, T;

if (nmagnitude > vmagnitude) {
II = (nmagnitude * cosf(nmagnitude)) / vmagnitude;
T = nmagnitude - II;
} else {
II = (vmagnitude * cosf(vmagnitude)) / nmagnitude;
T = vmagnitude - II;
}

float total = T + II;

}


I'm studying this from the 3D Math Primer for Graphics and Game Development book. The main reason why I'm asking here and not SO is because this is dealing with mathematical theory, and I need to know if my theory is correct, or if I'm on the right track by what I'm doing.

So, am I doing this correctly?

-
What exactly are you trying to do here? I know of no operation that "projects" two vectors "together." There is a method for projecting one vector onto another vector, described here: en.wikipedia.org/wiki/Vector_projection. It looks kind of like you're trying to do that here, with two major errors: 1) you can't take the cosine of a length, only an angle, and 2) II is unitless (it's a ratio of lengths), so you cannot subtract it from a length. –  Scaramouche Jan 9 '12 at 4:57
I see...that makes more sense. Thank you. One last question: The formula on wikipedia states that the absolute value of a is used with cos are used to create c. The question is, what exactly is the value of a calculated from? I know it's a vector, but is it calculating its magnitude as its value, or adding its x, y coordinates together? –  about blank Jan 9 '12 at 16:45
$|a|$ is the magnitude of $\vec a$; it can be calculated as $\| \langle a_x, a_y \rangle \| = \sqrt{ a_x^2 + a_y^2 } \neq a_x + a_y$. –  Scaramouche Jan 13 '12 at 10:10