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

Started by
19 comments, last by fguihen 19 years, 1 month ago
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)
Advertisement
Quote:Original post by fguihen
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

*** 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.

Everything is better with Metal.

Quote:Original post by reana1
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 ...


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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by reana1
Strange, 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.
Quote:Original post by oliii
your 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*.
Quote:Original post by reana1
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 ...

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).
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
I currently only use c# and directX9, in case its relivant and i didnt mention it in the post
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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
Tadd- WarbleWare
Quote:Original post by fguihen
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


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.

Everything is better with Metal.

This topic is closed to new replies.

Advertisement