summaryrefslogtreecommitdiff
path: root/lib/lib8tion/trig8.h
diff options
context:
space:
mode:
authorFlorian Didron <fdidron@users.noreply.github.com>2019-05-20 15:48:36 +0900
committerGitHub <noreply@github.com>2019-05-20 15:48:36 +0900
commit3ed039ed768252d64ef64865737d1cb10e8a3f2f (patch)
treef75d7d0bcef60fda2c0407bdeaa5062a54100f44 /lib/lib8tion/trig8.h
parentbe58a6847364a9a8243a77b5398e9c209628d1ef (diff)
parent24d05fee49bd9d86863f896d70eafd25674ec248 (diff)
Merge branch 'master' into fix/mouse_keys
Diffstat (limited to 'lib/lib8tion/trig8.h')
-rw-r--r--lib/lib8tion/trig8.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/lib8tion/trig8.h b/lib/lib8tion/trig8.h
index 6ef3ce625f..cfba6373fb 100644
--- a/lib/lib8tion/trig8.h
+++ b/lib/lib8tion/trig8.h
@@ -255,5 +255,30 @@ LIB8STATIC uint8_t cos8( uint8_t theta)
return sin8( theta + 64);
}
+/// Fast 16-bit approximation of atan2(x).
+/// @returns atan2, value between 0 and 255
+LIB8STATIC uint8_t atan2_8(int16_t dy, int16_t dx)
+{
+ if (dy == 0)
+ {
+ if (dx >= 0)
+ return 0;
+ else
+ return 128;
+ }
+
+ int16_t abs_y = dy > 0 ? dy : -dy;
+ int8_t a;
+
+ if (dx >= 0)
+ a = 32 - (32 * (dx - abs_y) / (dx + abs_y));
+ else
+ a = 96 - (32 * (dx + abs_y) / (abs_y - dx));
+
+ if (dy < 0)
+ return -a; // negate if in quad III or IV
+ return a;
+}
+
///@}
#endif