aboutsummaryrefslogtreecommitdiff
path: root/lib/lib8tion
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lib8tion')
-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 6ef3ce625..cfba6373f 100644
--- a/lib/lib8tion/trig8.h
+++ b/lib/lib8tion/trig8.h
@@ -255,5 +255,30 @@ LIB8STATIC uint8_t cos8( uint8_t theta)
255 return sin8( theta + 64); 255 return sin8( theta + 64);
256} 256}
257 257
258/// Fast 16-bit approximation of atan2(x).
259/// @returns atan2, value between 0 and 255
260LIB8STATIC uint8_t atan2_8(int16_t dy, int16_t dx)
261{
262 if (dy == 0)
263 {
264 if (dx >= 0)
265 return 0;
266 else
267 return 128;
268 }
269
270 int16_t abs_y = dy > 0 ? dy : -dy;
271 int8_t a;
272
273 if (dx >= 0)
274 a = 32 - (32 * (dx - abs_y) / (dx + abs_y));
275 else
276 a = 96 - (32 * (dx + abs_y) / (abs_y - dx));
277
278 if (dy < 0)
279 return -a; // negate if in quad III or IV
280 return a;
281}
282
258///@} 283///@}
259#endif 284#endif