[NTG-context] atan2 function

Max Chernoff mseven at telus.net
Fri Oct 14 02:35:51 CEST 2022


Hi,

> The angle function doesn't appear to provide the same calculation as
> my atantwo in all cases.

They both give the same results, but "angle" gives a result in degrees
while "atantwo" gives a result in radians. This demo:

   \startMPpage
       vardef atantwo( expr dy, dx ) =
           save theta;
   
           numeric theta;
   
           theta := 0;
   
           if (dx > 0):
               theta := atan( dy / dx );
           elseif (dx < 0) and (dy >= 0):
               theta := atan( dy / dx ) + pi;
           elseif (dx < 0) and (dy < 0):
               theta := atan( dy / dx ) - pi;
           elseif (dx == 0) and (dy > 0):
               theta := pi / 2;
           elseif (dx == 0) and (dy < 0):
               theta := -pi / 2;
           fi;
   
           theta
       enddef;
   
       def showangles(expr dx, dy) =
           message "---------------"
           message "atantwo " & decimal atantwo(dy, dx)
           message "angle " & decimal (angle(dx, dy) * pi / 180)
           message "angle " & decimal angle(dx, dy)
           message "(" & decimal dx & ", " & decimal dy & ")"
       enddef;
   
       showangles(1, 0);
       showangles(1, 1);
       showangles(0, 1);
       showangles(-1, 1);
       showangles(-1, 0);
       showangles(-1, -1);
       showangles(0, -1);
       showangles(1, -1);
   \stopMPpage

gives:

   (1, 0)
   angle 0
   angle 0
   atantwo 0
   ---------------
   (1, 1)
   angle 45
   angle 0.78539816339744828
   atantwo 0.78539816339744828
   ---------------
   (0, 1)
   angle 90
   angle 1.5707963267948966
   atantwo 1.5707963267948966
   ---------------
   (-1, 1)
   angle 135
   angle 2.3561944901923448
   atantwo 2.3561944901923448
   ---------------
   (-1, 0)
   angle 180
   angle 3.1415926535897931
   atantwo 3.1415926535897931
   ---------------
   (-1, -1)
   angle -135
   angle -2.3561944901923448
   atantwo -2.3561944901923448
   ---------------
   (0, -1)
   angle -90
   angle -1.5707963267948966
   atantwo -1.5707963267948966
   ---------------
   (1, -1)
   angle -45
   angle -0.78539816339744828
   atantwo -0.78539816339744828
   ---------------
   
Thanks,
-- Max


More information about the ntg-context mailing list