Difference between angles
I was wondering, how do you find the difference between 2 angles? because -10 degrees is the same as 350 degress, -180 is the same as 180, 90 is the same as 450 degress, and so on...
Any ideas?
- John Moses
What is it you want to do? Make sure the angle is in the range 0-360?
My stuff.Shameless promotion: FreePop: The GPL god-sim.
Take the mod 360 of both angles, then subtract. OR subtract, then mod the result 360.
You know what I never noticed before?
Oh sorry if i didn''t specify what I needed to do. See, I''d like some kind of function of algorithim that would tell me what the difference is between two differnt angles, but I''m having trouble, so for instance I know the difference between -90 and 270 is 0 degrees, and the difference between 10 and 350 is just 10 degrees, so I''d like some kind of function that can tell me the difference between any 2 angles and still works with examples like those, but I''m not quite sure of how to set it up
float DifferenceBetweenAngles(float ang1, float ang2)
{
float difference = ang1 - ang2; // this doesnt work :-(
return difference;
}
Anyone have any ideas?
float DifferenceBetweenAngles(float ang1, float ang2)
{
float difference = ang1 - ang2; // this doesnt work :-(
return difference;
}
Anyone have any ideas?
- John Moses
Here is a simple code fragment. Optimized for clarity. Note the use of doubles. You should be using them rather than floats unless you have a specific reason not to. Also, depending on what you are doing, it may be easier to have all of your angles in radians rather than degrees. Otherwise you have to convert before doing any of the various trigonometric functions on them. Hope this helps.
-D
[edited by - tyrecius on June 20, 2003 1:38:28 AM]
double DifferenceBetweenAngles(double ang1, double ang2){ double difference = ang1 - ang2; // set up the difference difference = fabs(fmod(difference, 360)); // normalize it to the range [0..360) if (difference > 180) // normalize it to the range [0..180] { difference = 360 - difference; // we flip it because 10 == 350 and 30 == 330 etc. } return difference; // Every angle has now been mapped onto a specific number on the range [0..180]}
-D
[edited by - tyrecius on June 20, 2003 1:38:28 AM]
vanillacoke, if one mods the angles by 360 and then subtracts, one can still get funky values. If, for example, the first number mods to 20 and the second to 300, the result would be -280.
The second method you suggest is almost there. But it does not take into account that there are two equivalent angle differences for any two vectors. Therefore DifferenceBetweenAngles(10, 40) != DifferenceBetweenAngles(40, 10). Of course, differences are generally non-commutative. But in the case of vectors/angles, it is more useful to make the two cases equivalent as I''ve shown.
-D
The second method you suggest is almost there. But it does not take into account that there are two equivalent angle differences for any two vectors. Therefore DifferenceBetweenAngles(10, 40) != DifferenceBetweenAngles(40, 10). Of course, differences are generally non-commutative. But in the case of vectors/angles, it is more useful to make the two cases equivalent as I''ve shown.
-D
Oh thanks a bunch Tyrecius, thanks a bunch! That did the trick! I tried making my own algorithm for the last few days and couldn''t get it working
Thanks man
Thanks man
- John Moses
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement