Welcome All !

Please, use explicit topic names when creating new threads. Thanks.

Please, use explicit topic names when creating new threads. Thanks.

You are not logged in.

- Topics: Active | Unanswered

**lois.vanhee****Member**- Registered: 2011-10-03
- Posts: 12

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)

return toDegrees(atan(y/x));

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).

Offline

Thanks a lot !

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

Offline