My Function Doesn't Work

closed account (zb0S216C)
I'm working with vectors (not the STL vector), and I'm trying to get the angle between two vectors. I've created a function that returns the result of a dot operation (·) (Which is required). Strangely enough, when I call std::acos( ), I get the IND output (I believe that it's a result of diving by zero). Here's my function:

1
2
3
4
float GetDotProduct( VECTOR &A, VECTOR &B )
{
    return( ( A.X * B.X ) + ( A.Y * B.Y ) );
}

VECTOR is a simple structure. X and Y are members of VECTOR that're of type float.

Here're the vectors I've used:

1
2
3
4
5
6
7
VECTOR VectorA;
VectorA.X = 3.0f;
VectorA.Y = 1.5f;

VECTOR VectorB;
VectorB.X = 1.5f;
VectorB.Y = 3.0f;

Here's the function call to std::acos( ):

 
std::cout << std::acos( GetDotProduct( VectorA, VectorB ) ) << std::endl;

Any ideas?

Additional Information:
- I've tried normalizing the vectors but it gives me the same result.

Wazzak
Last edited on
closed account (zwA4jE8b)
std::acos() only takes values between 1 and -1

or so it seems.
Last edited on
closed account (zb0S216C)
Thanks for the reply, CreativeMFS. That explains why I get the IND result. One more thing, though: When I normalize my vector, my vector doesn't equal the value of 1. All of the websites say that the vector should equal to one after normalization. Any thoughts on this?

Wazzak
To normalize, you basically divide each dimension (or coordinate or whatever you call it) by the vector's magnitude. If that doesn't yield a normalized vector, remember that PC's have rounding issues. Personally, I would work with doubles by default.
closed account (zb0S216C)
Thanks for that, WebJose. I'll look it up :)

Thanks again.

Wazzak
this is only the dot product... the angle is something like this
cos(∂)=A.B/|A||B|...
A.B is dot product....
|A||B| is the product of their magnitudes.....
closed account (zb0S216C)
Rambo, your equation doesn't work. I broken down your equation (so C++ can understand it) and I was left with this:

 
Angle( GetDotProduct( VectorA, VectorB ) / ( GetVectorMagnitude( VectorA ) * GetVectorMagnitude( VectorB ) ) );

The output was (without normalization): 0.97308
The output was (with normalization): 0.993353

Rambo, if possible, can you convert your computation into C++ code? That way, I'll have better understanding.

Wazzak
Topic archived. No new replies allowed.