[gameprogrammer] Re: Diff Between 2 Angles

  • From: "Matthew Weigel" <unique@xxxxxxxxxxx>
  • To: gameprogrammer@xxxxxxxxxxxxx
  • Date: Fri, 30 Sep 2005 15:28:05 -0500 (CDT)

Sami Näätänen wrote:
> On Friday 30 September 2005 10:08, Olof Bjarnason wrote:
>> double absanglediff(double angle1, double angle2) {
>>   // Rotate angle1 with angle2 so that the sought after
>>   // angle is between the resulting angle and the x-axis
>>   angle1 -= angle2;
>>
>>   // "Normalize" angle1 to range [-180,180)
>>   while(angle1 < -180)
>>     angle1 += 360;
>>   while(angle1 >= 180)
>
> small improvment is to left out the equal sign.
> after all abs(180-360) == abs(180)

Changing '>=' to '>' is better for a completely different reason: angle1
is a double, and equality is basically meaningless for floating point
numbers.

If the angles were integers, '>=' would be very appropriate so that you
are left with exactly 360 degrees - switching it to '>' would leave two
equivalent cases in the domain that could queer debugging, and maybe other
things.
-- 
 Matthew Weigel
 hacker
 unique@xxxxxxxxxxx


---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: