# Trig Question

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

## Recommended Posts

I hae two objects. One is a blue arrow who's position will never change. Another is a red arrow who's position will change. While the red arrow's position is changing, I want the blue arrow to point towards it. So I need to figure out what angle to rotate the blue arrow to be pointing at the red arrow. How do I do this?

##### Share on other sites
Well if both objects can be represented using points(x,y) you could just measure the angle for the line created between the two points.

This is from DooM.Net:

int Angle = (int)Direct3D.Geometry.RadianToDegree( (float)Math.Atan2( V2.Y - V1.Y, V2.X - V1.X ) );

V1 and V2 are just points.

I make no guarantees that this is correct though, as I've had to make my fair share of math forum help topics.

##### Share on other sites
Hmmm... That works but how could you write the function atan2 for a programming language that doesn't have that function built in? Say like C++, if I were to write it from scratch. Thanks in advance!

##### Share on other sites
Quote:
 Original post by BlackDragon777Hmmm... That works but how could you write the function atan2 for a programming language that doesn't have that function built in? Say like C++, if I were to write it from scratch. Thanks in advance!

Please tell me you're not writing everything(including basic math fucntions) from "scratch"!?

Include the header cmath( #include <cmath> ). You should now have access to a function called atan2 that takes two doubles as arguments.

##### Share on other sites
I've never done this so this might not be the best way to implement it, but if I would take a shot I would approximate atan with a bunch of Taylor series of atan. (I.e. an approximation of atan by an nth degree polynomial.)

##### Share on other sites
Oh ok thanks. I didn't know atan2 was a standard math function such as sin, arctan, etc

##### Share on other sites
So is there a way to do the arctan2 on say a Ti-89?

##### Share on other sites
Basically you have to check the quadrant of the x and y values after you calculate the arctangent. for instance, say you have the point x = 3, y =2. arctan( 2/3) is about 33.69 degrees. The result is in the first quadrant so the answer is correct. If you have x = -3, y = 2 then arctan(2/-3) then you should have the angle -33.69 degrees. The result should be in the second quadrant. The angle should be 146.31. This is 180 degrees move than -33.69. Indeed, if the result is in the 2nd quadrant the angle is 180 + arctan(y,x) If you have x = -3, y = -2 then arctan(-2/-3) is again 33.69. The result should be in the third quadrant and the real angle should again be 213.69 degrees. This is again 180 degrees more than the result given by arctan so if the result should be in quadrant 3 the angle is 180 + arctan(y,x). Finally, if x = 3, y = -2 arctan(-2/3) = -33.69 degrees. the angle Should be 326.31 degrees which is 360 more than the result given by arctan.

Therefore
float atan2(float x, float y) {  if(x < 0) { return arctan(y/x) + PI; }  if(y < 0) { return arctan(y/x) + 2*PI; {  return arctan(y/x);}

I'm about 95% sure that's right, I can't check.

1. 1
2. 2
Rutin
21
3. 3
4. 4
5. 5
frob
12

• 9
• 17
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007453

×