Advertisement

Angle between 2 points

Started by March 15, 2002 03:34 AM
2 comments, last by harleyrana 22 years, 11 months ago
Hi i need to find the angle of a line between 2 x,y points. The line represents the path of a projectile. I think it involves a gradient m = (y2-y1)/(x2-x1), tan-1(m). Its probally really simple, but im reminded of my lack of effort at school! Thanks for any help!
Assuming you want the angle between your line and the X-axis in 2d...

tan(theta) = Ystep/Xstep

so theta = tan^-1 (Ystep/Xstep)

hope this helps,

Adam

[edited by - AJFleming on March 15, 2002 6:54:39 AM]
Advertisement
Is that really the fastest way to determine the angle of a 2D line? Wow, I'm impressed because I tried to figure that one out about 4 years ago for my asteroids type game and the atan function was the closest thing I could find to give me an angle...except it only worked up to 90 degs. I'm impressed because back then I knew absolutly nothing about trig so while researching I came up with the atan method.

I'll stop boring you with my story now .

[edited by - WhatEver on March 15, 2002 5:13:56 PM]
Tan(Theta) becomes undefined at 90° and 270°. You might want something that looks like this:

int FindAngle(X, Y){
...if (X <= 0 && Y != 0) {
......if (X != 0) { return aTan(YC/XC) + 180; }
......if (Y > 0 and X == 0) { return 90; }
......if (Y < 0 and X == 0) { return 270; }
...}else {
......if (Y == 0 and X < 0) {
.........return 180;
......}else {
.........return aTan(YC/XC);
......}
...}
}

Where X is the distance between the two points horizontally (negative is the terminal point is to the left, positive for the right) and Y is the verticle distance (negative for up, positive for down).

This works for all angles.

This topic is closed to new replies.

Advertisement