vector2D class and + operator

Started by
8 comments, last by zip7000 21 years, 8 months ago
hello, I have a vector class defined by: class Vector2D { private: long x,y; int angle; long length; I would like to overload the + operator. How could I calculate the angle? thank you
Advertisement
That depends on what you "angle" means. Is it the angle
between the vector and the X-axis?

If so, then you know from trigonometry that:

tan(theta) = rise / run = slope of line

I''ll leave the answer as an exercise for you.


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Presumably 'x' and 'y' in your structure the origin of the vector?

You have two options for performing '+'.

a) Convert argument vectors of the '+' operator into linear components, add components and then convert back into angle/length form; or,

b) perform the computation directly using the law of sines and law of cosines.

Tangentz has already hinted at the solution for the first method.

The second is performed as follows.

Assume you have two vectors: a =(A,alpha) and b =(B,beta), where A and B are the lengths and alpha and beta are the angles. We seek the vector c =a +b = (C,gamma)

From the Law of Sines we obtain
             A sin(alpha) - B sin(beta)tan(gamma) = --------------------------             A cos(alpha) - B cos(beta)  

and from the Law of cosines we obtain
C2 = A2 + B2 - 2ABcos(PI+alpha-beta)  


I suspect it would be faster to perform the computation by converting into linear component form rather than using the above equations, since it would involve only 2 arctan operations, 6 multiplications, 2 additions, 1 divide, 1 subtraction, 1 tan and 1 sqrt. Compare this with the above, which requires 2 sines, 3 cosines, 1 arctan, 4 subtractions, 2 additions, 1 divide, 1 sqrt and 9 multiplications.

I'll leave it to you to decide which one is better.

Cheers,

Timkin

[edited by - Timkin on August 18, 2002 10:07:25 PM]
so,

If I have two vectors
v1 = (x1,y1,angle1)
v2 = (x2,y2,angle2)

v3 = v1 + v2 = (x1+x2, y1+y2, tan-1(y1+y2/x1+x2) ???

is there an tan-1(inverse tangeante) in c++? what is the prototype?

zip7000
one more question,

I have a vector v. How can I get an vector t so that the angle between v and t is equal to 45 degrees?

thank you for your answers!

zip7000
quote:Original post by zip7000
is there an tan-1(inverse tangeante) in c++? what is the prototype?

zip7000



Yes, actually two of them:

atan(a) gives tan-1 of a, with a in radians. The result is in radians and has a value from -PI/2 to PI/2.

atan(y,x) gives tan-1 of y/x, with y being rise and x being run. The result is in radians and ranges from -PI to PI. This version gives the correct angle in the correct quadrant.


Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
quote:Original post by zip7000
one more question,

I have a vector v. How can I get an vector t so that the angle between v and t is equal to 45 degrees?


There are two possible vectors really, since it could be + or - 45 degrees. Lets assume you want the angle measured from v and to t to be 45 degrees. In this case, you can just rotate v by 45 degrees to get t. I''ll do some math here, for an arbitrary angle, phi, not just 45 deg. Here a matrix formula for t:

t = R * v 


where R is a 2x2 rotation matrix:

R = [cos(phi) -sin(phi)]    [sin(phi)  cos(phi)] 


Which yields this formula for t:

t.x =  v.x*cos(phi) + v.y*sin(phi);t.y = -v.x*sin(phi) + v.y*cos(phi); 


Plug in 45 degrees and you get:

t.x =  v.x*cos(45) + v.y*sin(45);t.y = -v.x*sin(45) + v.y*cos(45); 


The length of t is the same as v.

You can check the angle by dotting t with v:

t.dot(v) = v.x*v.x*cos(45) + v.y*v.x*sin(45) -           v.x*v.y*sin(45) + v.y*v.y*cos(45) 


The two sin(45) terms cancel out and you''re left with

t.dot(v) = v.x*v.x*cos(45) + v.y*v.y*cos(45)         = (v.x*v.x + v.y*v.y)*cos(45) 


Divide by length(t)*length(v) and you''re left with cos(45, proving that this equation for t does give your desired result, with t being 45 degrees rotated from v.


Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
quote:Original post by grhodes_at_work
Original post by zip7000
one more question,

I have a vector v. How can I get an vector t so that the angle between v and t is equal to 45 degrees?

There are two possible vectors really, since it could be + or - 45 degrees.

Actually, there are an infitie number – just think of a cone with the main vector running up through the center.

Edit: Actually, nevermind, I think you're just talking about 2 dimensions .
——————–
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music…"

[edited by - Matt Calabrese on August 21, 2002 6:22:45 PM]
great!!

thank you very much!

zip7000
great!!

thank you very much!

zip7000

This topic is closed to new replies.

Advertisement