On 10/14/2022 2:35 AM, Max Chernoff via ntg-context wrote:
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
So you suggest to add atantwo? As side note, you can redure your definition to: vardef atantwo( expr dy, dx ) = if (dx > 0): atan( dy / dx ) elseif (dx < 0) and (dy >= 0): atan( dy / dx ) + pi elseif (dx < 0) and (dy < 0): atan( dy / dx ) - pi elseif (dx == 0) and (dy > 0): pi / 2 elseif (dx == 0) and (dy < 0): -pi / 2 else : 0 fi enddef ; and then vardef atantwo(expr dy, dx) = if dx == 0 : if dy < 0 : - fi pi / 2 else : atan(dy/dx) if dx > 0 : if dy < 0 : - else : + fi pi fi fi enddef ; going further makes us end up in an one-line obscurity Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------