diff --git a/compiler/res/prog8lib/math.p8 b/compiler/res/prog8lib/math.p8 index 84105d0d1..e99c6a04e 100644 --- a/compiler/res/prog8lib/math.p8 +++ b/compiler/res/prog8lib/math.p8 @@ -254,7 +254,7 @@ _quadrant_region_to_direction: }} } -asmsub atan(ubyte x1 @R0, ubyte y1 @R1, ubyte x2 @R2, ubyte y2 @R3) -> ubyte @A { +asmsub atan2(ubyte x1 @R0, ubyte y1 @R1, ubyte x2 @R2, ubyte y2 @R3) -> ubyte @A { ;; Calculate the angle, in a 256-degree circle, between two points into A. ;; The points (x1, y1) and (x2, y2) have to use *unsigned coordinates only* from the positive quadrant in the carthesian plane! ;; https://www.codebase64.org/doku.php?id=base:8bit_atan2_8-bit_angle diff --git a/compiler/res/prog8lib/virtual/math.p8 b/compiler/res/prog8lib/virtual/math.p8 index cd64824b5..84c3893b0 100644 --- a/compiler/res/prog8lib/virtual/math.p8 +++ b/compiler/res/prog8lib/virtual/math.p8 @@ -187,7 +187,7 @@ math { sub direction(ubyte x1, ubyte y1, ubyte x2, ubyte y2) -> ubyte { ; From a pair of positive coordinates, calculate discrete direction between 0 and 23 into A. ; This adjusts the atan() result so that the direction N is centered on the angle=N instead of having it as a boundary - ubyte angle = atan(x1, y1, x2, y2) - 256/48 + ubyte angle = atan2(x1, y1, x2, y2) - 256/48 return 23-lsb(mkword(angle,0) / 2730) } @@ -226,14 +226,14 @@ sub direction_qd(ubyte quadrant, ubyte xdelta, ubyte ydelta) -> ubyte { } } -sub atan(ubyte x1, ubyte y1, ubyte x2, ubyte y2) -> ubyte { +sub atan2(ubyte x1, ubyte y1, ubyte x2, ubyte y2) -> ubyte { ;; Calculate the angle, in a 256-degree circle, between two points into A. ;; The points (x1, y1) and (x2, y2) have to use *unsigned coordinates only* from the positive quadrant in the carthesian plane! %ir {{ - loadm.b r65532,math.atan.x1 - loadm.b r65533,math.atan.y1 - loadm.b r65534,math.atan.x2 - loadm.b r65535,math.atan.y2 + loadm.b r65532,math.atan2.x1 + loadm.b r65533,math.atan2.y1 + loadm.b r65534,math.atan2.x2 + loadm.b r65535,math.atan2.y2 syscall 44 (r65532.b, r65533.b, r65534.b, r65535.b): r0.b returnr.b r0 }} diff --git a/docs/source/libraries.rst b/docs/source/libraries.rst index e2f45581a..31da82991 100644 --- a/docs/source/libraries.rst +++ b/docs/source/libraries.rst @@ -384,7 +384,7 @@ but perhaps the provided ones can be of service too. Fast 8-bit byte cosine of angle 0..179 (each is a 2 degree step), result is in range -127..127 Angles 180..255 will yield a garbage result! -``atan (ubyte x1, ubyte y1, ubyte x2, ubyte y2)`` +``atan2 (ubyte x1, ubyte y1, ubyte x2, ubyte y2)`` Fast arctan routine that uses more memory because of large lookup tables. Calculate the angle, in a 256-degree circle, between two points in the positive quadrant. diff --git a/examples/cx16/spotlight.p8 b/examples/cx16/spotlight.p8 index 4e07c5e90..f00f8918c 100644 --- a/examples/cx16/spotlight.p8 +++ b/examples/cx16/spotlight.p8 @@ -1,6 +1,12 @@ %import math %import palette +; use the mouse to move the cursor around the screen +; it uses the fast direction routine to spotlight your mouse position. + +; you can use the atan2() routine as well for more precision laser beams, +; but it will use a lot more memory due to the required lookup tables. + main { sub start() { const uword WIDTH=320