MaDKit forum

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

You are not logged in.

#1 2011-10-05 09:51:38

Registered: 2011-10-03
Posts: 12

[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)
                return toDegrees(atan(y/x));
                return 360.0 + toDegrees(atan(y/x));
            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).


#2 2011-10-25 15:29:09

From: Montpellier
Registered: 2009-03-24
Posts: 172

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.


Board footer