PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MathUtil.h 1.1KB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef MATH_UTIL_H__
  2. #define MATH_UTIL_H__
  3. struct Point16 {
  4. int_fast16_t x;
  5. int_fast16_t y;
  6. };
  7. struct PointU16 {
  8. uint_fast16_t x;
  9. uint_fast16_t y;
  10. };
  11. struct PointU8 {
  12. uint_fast8_t x;
  13. uint_fast8_t y;
  14. };
  15. struct PointF {
  16. float x;
  17. float y;
  18. };
  19. uint_fast8_t lerp8( uint_fast8_t a, uint_fast8_t b, float progress ){
  20. // Cast to int, avoid horrible values when b-a is less than zero
  21. return a + (int_fast8_t)((int_fast8_t)b-(int_fast8_t)a)*progress;
  22. }
  23. float lerp( float a, float b, float progress ){
  24. return a + (b-a)*progress;
  25. }
  26. // x=0,y=0 returns center of screen.
  27. // z=1: Normalized coordinates with screen (x==-1 is left, x==1 is right, etc.)
  28. // screenW_2 and screenH_2 are __half__ the screen size, and it's your job to pass in these numbers
  29. // because you're probably re-using them for several points on the other end :P
  30. // Assumes landscape orientation, so screenW_2 is greater than screenH_2.
  31. Point16 xyz2screen( float x, float y, float z, uint_fast16_t screenW_2, uint_fast16_t screenH_2 ) {
  32. float invZ = 1.0f / z;
  33. return (Point16){
  34. (int_fast16_t)(screenW_2 + x*invZ*screenW_2),
  35. (int_fast16_t)(screenH_2 + y*invZ*screenW_2)
  36. };
  37. }
  38. #endif