Angle between 2 Points
Hi together !
I am working on an adventure in the old Lucas-Arts-Style. To the question :
What is the fastest way to determine the angle between a line (between 2 points) and the y-axis (meaning that 0° "is on top" of the y-axis)?
In the moment I use vectors for that :
Result := round(radtodeg(arccos((-Vector1.y)) / sqrt(sqr(Vector1.x)+sqr(Vector1.y)))));
(thats Delphi-Code ...;-)
I need the angle to determine the "facing" of the main character !
thx
-crent-
Here's how I do it:
float
GetAngle(float x, float y)
{
float rslt;
1) handle special cases (0,PI/2,PI,3PI/2)
2) switch(quadrant)
2a) Q0: rslt = atanf(x/y);
2b) Q1: rslt = TWO_PI_F - atanf(-x/y);
2c) Q2: rslt = atanf(x/y) + PI_F;
2d) Q3: rslt = PI_F - atanf(-x/y);
return rslt; // (rslt in rads)
}
(I dunno Delphi but:
atanf is inverse tan (float version)
PI_F, TWO_PI_F are my float pi constants)
Also, this is assuming x,y are in rads already.
If anyone knows a faster way I'd surely like to hear about it!
-ns-
Edited by - NightShade on 1/10/00 3:16:40 PM
Edited by - NightShade on 1/11/00 8:54:51 AM
float
GetAngle(float x, float y)
{
float rslt;
1) handle special cases (0,PI/2,PI,3PI/2)
2) switch(quadrant)
2a) Q0: rslt = atanf(x/y);
2b) Q1: rslt = TWO_PI_F - atanf(-x/y);
2c) Q2: rslt = atanf(x/y) + PI_F;
2d) Q3: rslt = PI_F - atanf(-x/y);
return rslt; // (rslt in rads)
}
(I dunno Delphi but:
atanf is inverse tan (float version)
PI_F, TWO_PI_F are my float pi constants)
Also, this is assuming x,y are in rads already.
If anyone knows a faster way I'd surely like to hear about it!
-ns-
Edited by - NightShade on 1/10/00 3:16:40 PM
Edited by - NightShade on 1/11/00 8:54:51 AM
-ns-
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement