jmasterx
jmasterx

Reputation: 54133

Wrapping a number?

I have this float which is a rotation angle.

Camera.roty += (float) diffx * 0.2;

where diff is the change in mouse position.

In OpenGL it will wrap it if it exceeds 360 or is below 0, but how could I do this if I want to verify if the angle is between 0 and 180?

Thanks

Upvotes: 0

Views: 1621

Answers (3)

Jacob
Jacob

Reputation: 3686

If I understand your question correctly you're basically looking for something like this?:

float Wrap( const float Number, const float Max, const float Min ) {
 if( Number > 0.0f ) {
  return fmod( Number, Max ) + Min;
 }
 else {
  return Max - fmod( abs( Number ), Max ) + Min;
 }
}

Upvotes: 2

jamesdlin
jamesdlin

Reputation: 90085

To deal with floating-point values, you could do:

angle = angle - floor(angle / 360) * 360;

This should deal with negative values properly too (-1 would be converted to 359).

Upvotes: 1

Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 10467

According to comment by @bta:

why not use:

angle % 180

and save that number as an angle?

Upvotes: 0

Related Questions