title: "Atan2" description: TRSE built-in method (from IDE help)
Atan2
Systems: X16,C64, MEGA65, PLUS4, C128, VIC20, PET, NES , OK64, BBCM, ACORN, ATARI800
Parameters: b,b,b,b
[byte angle] = Atan2( [byte x1], [byte x2], [byte y1], [byte y2] );
- [byte x1] - x value 1
- [byte x2] - x value 2
- [byte y1] - y value 1
- [byte y2] - y value 2
Description
Returns atan2 of the arguments (x2-x1, y2-y1) (Atan2 calculates the angle between two points x and y). Note, if using sprite x positions, scale the x and y positions by half for the Atan2 parameters.
The return result is a value between 0 and 255, where:
- 3 o'clock = 0
- 6 o'clock = 63
- 9 o'clock = 127
- 12 o'clock = 192
Example
program MyProgram;
var
x1, x2, y1, y2, angle : byte;
begin
ClearScreen(32, ^$0400);
x1 := 100; y1 := 100;
x2 := 200; y2 := 100; // position 2 is at 3 o'clock from position 1
angle := Atan2(x1, x2, y1, y2);
MoveTo(0,0,$04);
PrintDecimal(angle, 6);
Loop();
end.