Jump to content
  • Advertisement
Sign in to follow this  
Sync Views

make numbers wrap around

This topic is 3720 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

How can I make a double or int wrap around between 0 <= x < 360 So x would go to y
x     -> y
-90   -> 270
360   -> 0
-180  -> 180
-45   -> 315
etc
I know I can do it by a loop but is there a better way for wrapping numbers around?
double WrapDeg(double Dir)
{
	while(Dir <  0)   Dir += 360;
	while(Dir >= 360) Dir -= 360;
	return Dir;
}

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Sync Views
How can I make a double or int wrap around between 0 <= x < 360


Assuming C++...

#include <cmath>
...
double wrappedValue = fmod (value,360.0); // modulo for real (eg. float or double) values

Share this post


Link to post
Share on other sites
Quote:
Original post by blueapple
Instead of using loops, you could use the modulus operator (%):
int angle = 725;
angle = angle % 360; // angle is now 25
angle = angle % 360; // angle is still 25


That won't work.

The modulo operator only works for integral types. The OP specified he is using doubles. Use fmod() for real (eg. floating point) valued types.

Share this post


Link to post
Share on other sites
Well, actually, he asked:
Quote:
Original post by Sync Views
How can I make a double or int wrap around between 0 <= x < 360

But you're right, for doubles, it won't work (and I'm not sure about negative numbers).

Share this post


Link to post
Share on other sites
Quote:
Original post by blueapple
Well, actually, he asked:
Quote:
Original post by Sync Views
How can I make a double or int wrap around between 0 <= x < 360

But you're right, for doubles, it won't work (and I'm not sure about negative numbers).


My bad. I cant read :)

Share this post


Link to post
Share on other sites
Quote:
Original post by blueapple
Instead of using loops, you could use the modulus operator (%):
int angle = 725;
angle = angle % 360; // angle is now 25
angle = angle % 360; // angle is still 25


Actually the angle is 5º (2×360º=720º)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!