Angle between two vectors

Hello guys!!

I’m trying to find some information in the net about how to calculate the angle between two vectors, but it is coming really dificult, I know that here is not the best place to ask about this, but as the people are helping the others, how can I calculate the angle beetwen two points, and also between two lines…???

ahhhh…before I forget…does anyone know a good site in the net that show math like this stuff ???

Thank you for the moment

Best Regards

Kurt

http://mathworld.wolfram.com/

hi, it’s quite easy.

theta=acos((v1 DOT v2)/(|v1|*|v2|))

v1,v2: vector
DOT : dot product
|v1| : length of v1

OK OK!!

But if it is a vector, (a point), how can I calculate it’s length ?

I forgot everything of my math, how can I calculate the DOT product of the V1 and V2 ?

Thank you very much!

Bets regards

Kurt

The length of the vector is sqtr(v.xv.x + v.yv.y + v.zv.z) where v is your vector, and the dotproduct is (v1.xv2.x)+(v1.yv2.y)+(v1.zv2.z) where v1 and v2 is your two vectors.

Dear Bob,
I will try to implement something in this way.

Thank you very much huys!!!

Best Regards
Kurt

Dear Bob,
I will try to implement something in this way.
Thank you very much guys!!!

Best Regards
Kurt

Hello guys!!

I’m coming crazy…

Can someone see what I’m doing wrong with my code ?

At first of all, I’m using Windows SDK to develope my application.

#include <math.h>
#include

#define NC_PI 3.1415926

typedef struct {
float x,y;
}; VECTOR

float Get_Angle_Between_Points(int X1, int Y1, int X2, int Y2);

float dot_product(VECTOR _V1, VECTOR _V2);
float vector_length(VECTOR _V1);

float Get_Angle_Between_Points(int X1, int Y1, int X2, int Y2)
{
float vf_dot_product, vf_length_V1, vf_length_V2, angle;

VECTOR V1, V2;

V1.x = X1;
V1.y = Y1

V2.x = X2;
V2.y = Y2;

vf_dot_product = dot_product(V1, V2);
vf_length_V1 = vector_length(V1);
vf_length_V2 = vector_length(V2);

angle = acos((vf_dot_product/(vf_length_V1*vf_length_V2)));

// Convert the angle to degress
angle = angle * (180/NC_PI);

return angle;
}

float dot_product(VECTOR _V1, VECTOR _V2)
{
float dot_product;

dot_product = (_V1.x * _V2.x) + (_V1.y * _V2.y);

return dot_product;

}

float vector_length(VECTOR _V1)
{
float vector_length;

vector_length = sqrt(((_V1.x * _V1.x) + (_V1.y * _V1.y)));

return vector_length;

}

What is wrong ???

Thanks

Best Regards

Kurt

Hello guys!!
I’m coming crazy…
Can someone see what I’m doing wrong with my code ?
At first of all, I’m using Windows SDK to develope my application.

#include <math.h>
#include

#define NC_PI 3.1415926
typedef struct {
float x,y;
}; VECTOR

float Get_Angle_Between_Points(int X1, int Y1, int X2, int Y2);
float dot_product(VECTOR _V1, VECTOR _V2);
float vector_length(VECTOR _V1);

float Get_Angle_Between_Points(int X1, int Y1, int X2, int Y2)
{
float vf_dot_product, vf_length_V1, vf_length_V2, angle;
VECTOR V1, V2;

V1.x = X1;
V1.y = Y1
V2.x = X2;
V2.y = Y2;

vf_dot_product = dot_product(V1, V2);
vf_length_V1 = vector_length(V1);
vf_length_V2 = vector_length(V2);
angle = acos((vf_dot_product/(vf_length_V1*vf_length_V2)));
// Convert the angle to degress
angle = angle * (180/NC_PI);
return angle;
}

float dot_product(VECTOR _V1, VECTOR _V2)
{
float dot_product;
dot_product = (_V1.x * _V2.x) + (_V1.y * _V2.y);
return dot_product;
}

float vector_length(VECTOR _V1)
{
float vector_length;
vector_length = sqrt(((_V1.x * _V1.x) + (_V1.y * _V1.y)));
return vector_length;
}

What is wrong ???
Thanks
Best Regards
Kurt

What kind of error are you getting? A division by zero? Is the function returning crap? Get the same result all the time? A general protection fault? Anything?

The fact is, that the returned value is always around 21 maximum 23, I really dont unterstand what is happening.

Thank’s

Best regards

Kurt

First of all it is not considered a good practice to name variables the same as the function, though it should not cause any problems in your example… This is because local variables usualy take up stack space or registers instead of the data segment, if you made those variables global you would get an error like ‘redeclared as different kind of symbol.’

You don’t even need to have a variable to get the value before you return:

return (_V1.x * _V2.x) + (_V1.y * _V2.y);

The compiler will probably use a register this way instead of the stack which is slower.

I like to use macros for this stuff:

#define dot_product(U,V)	\
	((U.x*V.x) + (U.y*V.y))

The reason why your code is screwing up is because you are passing a structure. You should instead pass by reference:

float vector_length( VECTOR *_V1 )
{
  return sqrt((_V1->x + _V1->x) + (_V1->y * _V1->y));
}

/skw|d

I haven’t read through the posts completely, but here’s an offhand suggestion, compute both the cos and sin products of the vectors and then take an atan2() of the sin and cos.
The sin and cos functions vary fast over certain intervals of their values, the atan2() function compensates for that.

Hope this helps,
Dhiraj.

here’s your problem:

you did:
angle = angle * (180/NC_PI);
return angle

you should be doing:
angle2 = angle * (180/NC_PI);
return angle2

i.e, use different variable
hope it works

I don’t see anything immediately wrong with your code. Are you certain that your input is correct?
No offense to The Wolf, but using a different variable shouldn’t make any difference in this situation.

I just compiled and ran the code below, with the output indicated. The only change I made was to your struct definition (I think the change was insignificant). Looks fine to me.

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

#define NC_PI 3.1415926

struct VECTOR{
float x,y;
};

float Get_Angle_Between_Points(int X1, int Y1, int X2, int Y2);

float dot_product(VECTOR _V1, VECTOR _V2);
float vector_length(VECTOR _V1);

float Get_Angle_Between_Points(int X1, int Y1, int X2, int Y2)
{
float vf_dot_product, vf_length_V1, vf_length_V2, angle;

VECTOR V1, V2;

V1.x = X1;
V1.y = Y1;

V2.x = X2;
V2.y = Y2;

vf_dot_product = dot_product(V1, V2);
vf_length_V1 = vector_length(V1);
vf_length_V2 = vector_length(V2);

angle = acos((vf_dot_product/(vf_length_V1*vf_length_V2)));

// Convert the angle to degress
angle = angle * (180/NC_PI);

return angle;

}

float dot_product(VECTOR _V1, VECTOR _V2)
{
float dot_product;

dot_product = (_V1.x * _V2.x) + (_V1.y * _V2.y);

return dot_product;
}

float vector_length(VECTOR _V1)
{
float vector_length;

vector_length = sqrt(((_V1.x * _V1.x) + (_V1.y * _V1.y)));

return vector_length;
}

void main(){

cout &lt;&lt; "angle: " &lt;&lt; Get_Angle_Between_Points(0,1,1,0) &lt;&lt; endl;

cout &lt;&lt; "angle: " &lt;&lt; Get_Angle_Between_Points(0,1,1,1) &lt;&lt; endl;

cout &lt;&lt; "angle: " &lt;&lt; Get_Angle_Between_Points(0,1,8,1) &lt;&lt; endl;

cout &lt;&lt; "angle: " &lt;&lt; Get_Angle_Between_Points(1,1,1,1) &lt;&lt; endl;

}

output:
angle: 90
angle: 45
angle: 82.875
angle: -1.#IND

Dear Rob,

Thank you very much for your reply, I will try to implement the code in my application.

Thanks

Kurt

Hello guys!!

Could the problem be because I’m using gluUnProject, and using it’s to calculate the angle ?

Thanks

Best Regards

Kurt

you might wanna post the code with the openGL calls that your code makes.

Rob- I once had the same problem and believe it or not, changing variables made it work!! any idea why that happens?

Ok Wolf!

I will try to put it in my code.

Thank you for the moment

Best regards

Kurt

Wolf- I really don’t know why it would make a difference. I don’t consider myself a C/C++ expert at all. The best I can do is some wild, baseless speculation that maybe it’s a compiler issue, or a C vs. C++ issue (I’m strictly C++ these days), or some wacky pointer issue… Obviously, I’m clueless .