<![CDATA[MaDKit forum / [Bug fix] Bug on Turtle.directionAngleToPoint(x,y)]]> 2011-10-25T14:29:09Z FluxBB http://www.madkit.net/madkit/forum/viewtopic.php?id=118 <![CDATA[Re: [Bug fix] Bug on Turtle.directionAngleToPoint(x,y)]]> Thanks a lot !

This bug is fixed in the 2.4.8 which is now available.

]]>
<![CDATA[[Bug fix] Bug on Turtle.directionAngleToPoint(x,y)]]> The method directionAngleToPoint(x,y) appears to be buggy when x = 0. The aim of this function seems to compute the angle between the vector (1,0) and (x,y). It is used by the TurtleKit translation of the famous NetLogo construction "heading towards".

Here is the initial code :
private double directionAngleToPoint(double x, double y) {
if (x == 0 && y == 0) return 0.0;
if(x > 0)
if(y > 0)
else
return 360.0 + toDegrees(atan(y/x));
else
return 180.0 + toDegrees(atan(y/x));
}

If x==0, we should return "180.0 + toDegrees(atan(y/x))".
As x==0, y/x is not a number and should trigger a NaN exception. x and y are typed as doubles, so Java evaluates "y/x" as the infinity with the sign of y (if y>0, NaN otherwise). Consequently, if x==0 and y> 0 atan(y/x) is -Pi/2 but should be the opposite (and symmetrically when x==0 and y<0).

A quick solution for this bug is to replace the first "x>0" by "x>=0".

The first statement "if (x == 0 && y == 0) return 0.0" should raise an error. NetLogo do it when this case occors (because there is no angle between 2 vectors if one of those is the zero vector, more simply how to answser the question "what is the direction of the North pole ?" when stepping on the North pole).

]]>