degree mod 180°

Posted By: HeelX

degree mod 180° - 07/05/14 10:20

Hi all,
I have written the following Lite-C function to shift a degree as a double value between -180° ... 0° ... +180°. The outcome is the same as the ang() function, but it works different: instead of repeatedly adding or subtracting a multiple of 360 degrees, I use the fmod function (which implements that maybe anyway, I don't know):

Code:
// clamps a floating point degree between -180° ... 0° ... +180°
double avAngleDegreeMod180(double angleDegree) {
   if (angleDegree > 180) {
      double remainder = fmod(angleDegree, 360);
      if (remainder > 180) {
         return -180 + (remainder - 180);
      } else {
         return remainder;
      }
   } else {
      if (angleDegree >= -180) {
         return angleDegree;
      } else {
         return -avAngleDegreeMod180(-angleDegree);
      }
   }
}


Can anyone think of a faster/more elegant solution? This feels somehow bloated... eek
Posted By: Superku

Re: degree mod 180° - 07/05/14 10:35

Code:
var my_ang(var x)
{
	x = x%360;
	x = (x+360)%360;
	if(x > 180) return x-360;
	
	return x;
}

void main()
{
	fps_max = 60;
	while(1)
	{
		var i = 540*sinv(total_ticks*2);
		DEBUG_VAR((int)i,20);
		DEBUG_VAR((int)my_ang(i),40);
		DEBUG_VAR((int)ang(i),60);
		wait(1);
	}
}



I don't know if you like that approach more than yours, probably not.
Posted By: Kartoffel

Re: degree mod 180° - 07/05/14 10:41

Not sure if I understood what you're trying to do, but:
(x + 180) % 360 - 180
confused
Posted By: HeelX

Re: degree mod 180° - 07/05/14 12:02

Problem is, that I do not want to use fixed point arithmetics to get highest precision wink
© 2024 lite-C Forums