[NTG-context] atan2 function

Thangalin thangalin at gmail.com
Thu Oct 13 07:40:59 CEST 2022


Hi there,

How is atan2 called? I rolled my own as follows:

% SOT
\startuseMPgraphic{HexGridBaseGraphic}
  vardef atantwo( expr ax, ay, bx, by ) =
    save theta;

    numeric dx;
    numeric dy;
    numeric theta;

    dx := bx - ax;
    dy := by - ay;
    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 := atan( dy, dx );

    theta
  enddef;
% EOT

When this runs, the expected result is generated:

https://pdfhost.io/v/xdNxANU18_scaled

When uncommenting "atan( dy, dx )", an unexpected result is generated:

https://pdfhost.io/v/QdfU89IL._scaled

Is atan with two parameters supposed to behave like atan2?

Cheers!


More information about the ntg-context mailing list