Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

gimp

Smarth code wanted for normalising degree's

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Something I do often in my code is normalise a numberto within a certain range. For example ensuring degree's are 0-359 or radians are 0-1. Can anyone suggest a smart trick for pulling this off? The best I can come up with is a sceme that cycles of the number adding or subtracting 360 until it falls in the range I want. Kludge. Is there a better way? Many thanks Chris Edited by - gimp on April 24, 2001 10:45:58 PM

Share this post


Link to post
Share on other sites
Advertisement
for integers

#define sgn(num) ((num) < 0 ? -1 : 1)
#define NORMALISE(num,min,max) sgn(num) * ((abs(num) - min) % (max - min)) + min

something like that.

For floats multiple num,min and max by 10000 covert to integers run the macro, divide result by 10000 (or whatever precision you want).



D.V.

Share this post


Link to post
Share on other sites
Thanks... I have a vague half thought of a method that would involve dividing the degree''s by the max and taking the remainder...

Share this post


Link to post
Share on other sites
Got it...

  
inline float NormaliseDegrees(const float a_Value)
{
return fmod(a_Value, 360.0f);
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!