ATAN2


atan2( dx, dy )
atan2 "2-argument arctangent"
dx a number
dy a number

The atan2(  ) function is used to return the angle of the numbers provided.



An example from the manual shows us how to get and draw the angle between two points using atan2:

X=20 Y=30
FUNCTION _UPDATE()
  IF (BTN(0)) X-=2
  IF (BTN(1)) X+=2
  IF (BTN(2)) Y-=2
  IF (BTN(3)) Y+=2
END

FUNCTION _DRAW()
  CLS()
  CIRCFILL(X,Y,2,14)
  CIRCFILL(64,64,2,7)

  A=ATAN2(X-64, Y-64)
  PRINT("ANGLE: "..A)
  LINE(64,64,
    64+COS(A)*10,
    64+SIN(A)*10,7)
END



We've added some more details to show what is happening here:

angle = atan2( x1-x2, y1-y2 )

Using the angle found with atan2() and combining it with cos(angle) and sin(angle), we are able to draw the line that always points from one point to the other.

437

4 Sep 2023

Font