# can anyone see whats wrong with this angle between vectors function?

This topic is 4725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

this function is supposed to get angle between vectors, but it doesnt. it gets a tiny number. something like 0.0000034221. when i know the angle is 45 degrees. can anyone help
public static float AngleBetweenVectors(Vector point1, Vector point2)
//finds the angle between two vectors
{
float theta = (float)Dot(point1,point2) / (Length(point1) * Length(point2));

theta = (float)Math.Acos((double)theta);
return theta;
}

heres my DOT function, in case its needed:
public static double Dot(Vector point1, Vector point2)
//gets the dot product of two vectors
{
double dot = (point1.Xval * point2.Xval + point1.Yval * point2.Yval);
return dot;
}

and heres the LENGTH function in case thats needed!
public static float Length(Vector point)//gets the length of a vector
{
float length = (float)(Math.Sqrt(point.Xval*point.Xval + point.Yval*point.Yval));
return length;
}

Fruny - Formatting

##### Share on other sites
For an angle of 45 degrees, the function should return about 0.7854, that is, π/4 radians.

##### Share on other sites
needs to be normalised before dotproduct!

##### Share on other sites
Changing your "CODE" tags to lowercase should work (or use "source").

One thing, acos functions often return radian units for an angle, but that won't be your only problem if that 0.0000034221 value is really what you got for 45 deg. Not sure what units your Math.Acos returns. Other than the possible unit issue, everything appears fine to me.

You should be able to run your debugger and just look at each calculation. Is Length returning right value?, Is Dot?, etc. If you don't know how to use debugger, put in some cout statements at appropriate places.

Give us the X and Y values for point1 and point2 you are using in your test.

I'm not sure about all the type casting. Maybe need extra parens here:
theta = (float)(Math.Acos(...));
(That would match what you did in Length for Math.Sqrt.)
What precision are your vector.Xval and Yval stored as?

##### Share on other sites
please post the 2 vectors that you are using for your test.

-me

##### Share on other sites
Quote:
 Original post by Anonymous Posterneeds to be normalised before dotproduct!

float theta = (float)Dot(point1,point2) / (Length(point1) * Length(point2));

##### Share on other sites
Yea either, your vectors are wrong or you are not intepreting your results correctly. Java == BLAH, C++ is my choice [grin]

#include <iostream.h>
#include <math.h>

struct Vector
{
float Xval, Yval;
};

double Dot(Vector point1, Vector point2);
float Length(Vector point);
float AngleBetweenVectors(Vector point1, Vector point2);

float AngleBetweenVectors(Vector point1, Vector point2)
//finds the angle between two vectors
{
float theta = (float)Dot(point1,point2) / (Length(point1) * Length(point2));

theta = (float)acos((double)theta);
return theta;
}

double Dot(Vector point1, Vector point2)
//gets the dot product of two vectors
{
double dot = (point1.Xval * point2.Xval + point1.Yval * point2.Yval);
return dot;
}

float Length(Vector point)//gets the length of a vector
{
float length = (float)(sqrt(point.Xval*point.Xval + point.Yval*point.Yval));
return length;
}

int main(int argc, char* argv[])
{
Vector V1;
V1.Xval = 0;
V1.Yval = 5;

Vector V2;
V2.Xval = 5;
V2.Yval = 0;

cout << (180*AngleBetweenVectors(V1,V2)/3.14159265);

return 0;
}

Displays: 90 so your code is correct! Make sure you convert to degrees from radians.

- Drew

##### Share on other sites
Quote:
 Original post by Anonymous Posterneeds to be normalised before dotproduct!

[EDIT - this is wrong - see other replies below]
While technically his Dot function is only a valid dot product for a unit vector, he is still using it correctly since he normalizes before taking the arccos. However, I would agree that that could be dangerous in the future if you don't realize that the Dot function isn't normalizing internally. But should still work fine in this example - I see Fruny beat me - again. :)

[Edited by - reana1 on March 14, 2005 3:00:57 PM]

##### Share on other sites
Quote:
 Original post by reana1While technically his Dot function is only a valid dot product for a unit vector, he is still using it correctly since he normalizes before taking the arccos.

Computing a dot product never required your vectors to be normalized.

Quote:
 However, I would agree that that could be dangerous in the future if you don't realize that the Dot function isn't normalizing internally.

If it did so, it wouldn't be a dot product anymore... indeed, it would only be one if the vectors were unit vectors. Precisely the opposite of what you assert.

##### Share on other sites
Quote:
Original post by Fruny
Quote:
 Original post by reana1While technically his Dot function is only a valid dot product for a unit vector, he is still using it correctly since he normalizes before taking the arccos.

Computing a dot product never required your vectors to be normalized.

Quote:
 However, I would agree that that could be dangerous in the future if you don't realize that the Dot function isn't normalizing internally.

If it did so, it wouldn't be a dot product anymore.

Strange, I guess it depends on the true definition of dot product, but I had always understood it to include the normalization. And in my reference that's how it's defined. But as long as it's used correctly I guess it's just semantics. I'll have to check more refs later ...

##### Share on other sites
it's not semantics. At all. Of course you could define your DotProduct() to format hard disk, to print dot on screen, or to normalize vectors. (all things is quite similar in spirit)

##### Share on other sites
Quote:
 Original post by fguihenthis function is supposed to get angle between vectors, but it doesnt. it gets a tiny number. something like 0.0000034221. when i know the angle is 45 degrees. can anyone help *** Source Snippet Removed ***heres my DOT function, in case its needed:*** Source Snippet Removed ***and heres the LENGTH function in case thats needed!*** Source Snippet Removed ***Fruny - Formatting

your vectors are points, not directions. You can't get the angle between two points, but you can get the angle between two directions.

Otherwise, the code is fine. It's a conceptual misunderstanding I think.

##### Share on other sites
Quote:
 Original post by reana1Strange, I guess it depends on the true definition of dot product, but I had always understood it to include the normalization. And in my reference that's how it's defined. But as long as it's used correctly I guess it's just semantics. I'll have to check more refs later ...

The dot product is simply the sum of the product of the vector coordinates.

See Dot Product and Inner Product on MathWorld.

The dot product is a special case of the inner product - when your vectors are in an euclidean space.

##### Share on other sites
Quote:
 Original post by reana1Strange, I guess it depends on the true definition of dot product, but I had always understood it to include the normalization.
That would make the dot product infinitely less useful. Certain uses of the dot product, such as finding the closest point on a line to a given point, require the ability to take the dot product of non-unit vectors.

##### Share on other sites
Quote:
 Original post by oliiiyour vectors are points, not directions. You can't get the angle between two points, but you can get the angle between two directions.

I agree as well. In my example the origin was the assumed first point automatically, that's why it did indeed work. With other vectors, *POW*.

##### Share on other sites
Quote:
 Original post by reana1Strange, I guess it depends on the true definition of dot product, but I had always understood it to include the normalization. And in my reference that's how it's defined. But as long as it's used correctly I guess it's just semantics. I'll have to check more refs later ...

http://mathworld.wolfram.com/DotProduct.html

If the dot product were defined including normalization, it would no longer satisfy the requirements for being an inner product:
http://mathworld.wolfram.com/InnerProduct.html

I've never heard of it including normalization myself, and that seems a bit odd to me since it would prevent a lot of the utility of the operator itself... applications where the length of the input vectors IS relivent (ex. projections).

##### Share on other sites
you could be right on the comment about not being able to find the angle between two points. i have a line, and for test purposes its vertical. its points are 300,50, and 300,150. this should return an angle of 0 but i get 20.32432.... . im doing it so that i can find the angle that a line is sloping, if you understand what i mean. il look into other ways to find the angle a line is at. thanks for all the replies. fantastic response everyone. i love theese forums

##### Share on other sites
With two points on the line (x1,y1) and (x2,y2), you can get the angle (in radians) the line makes with the horizontal by calling atan2(y2-y1,x2-x1).

##### Share on other sites
Quote:
 Original post by several... Links and info ...

Well, I stand corrected (several times). :) Thanks all, for links and info. Guess I've just gotten too used to using it where I must normalize. Sorry for any confusion I may have caused.

##### Share on other sites
Quote:
 Original post by fguihenyou could be right on the comment about not being able to find the angle between two points. i have a line, and for test purposes its vertical. its points are 300,50, and 300,150. this should return an angle of 0 but i get 20.32432.... . im doing it so that i can find the angle that a line is sloping, if you understand what i mean. il look into other ways to find the angle a line is at. thanks for all the replies. fantastic response everyone. i love theese forums

but if you do your test as

Vector V0 = Vector(0, 1); // Y axis
Vector V1 = Vector(300, 1500) - Vector(300, 50);

float Angle = AngleBetweenVector(V0, V1);

then Angle should be 0.

Watch out for Acos(). if you have a slight FP innacuracy and you end up doing acos(1.0000000001f), it will probably return an undefined value, or infinite, of something along those line.

atan2() doesn't suffer from this.

##### Share on other sites
sorry for posting again, waste of a post. thanks again all