manipulations.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "manipulations.h"
  2. #include <math.h>
  3. #ifndef M_PI
  4. #define M_PI 3.14159265358979323846264338327950288
  5. #endif
  6. #include <furi.h>
  7. #define DEG_TO_RAD(deg) ((deg)*M_PI / 180.0)
  8. #define MIN3(a, b, c) MIN(MIN((a), (b)), (c))
  9. #define MAX3(a, b, c) MAX(MAX((a), (b)), (c))
  10. void point_translate(Point* point, int32_t x, int32_t y) {
  11. point->x += x;
  12. point->y += y;
  13. }
  14. void point_rotate(Point* point, Point around, float deg) {
  15. float rad = DEG_TO_RAD(deg);
  16. float s = sin(rad);
  17. float c = cos(rad);
  18. int32_t dx = point->x - around.x;
  19. int32_t dy = point->y - around.y;
  20. point->x = c * dx - s * dy;
  21. point->y = s * dx + c * dy;
  22. point->x += around.x;
  23. point->y += around.y;
  24. }
  25. void line_translate(Line* line, int32_t x, int32_t y) {
  26. point_translate(&line->a, x, y);
  27. point_translate(&line->b, x, y);
  28. }
  29. void line_rotate(Line* line, Point around, float deg) {
  30. point_rotate(&line->a, around, deg);
  31. point_rotate(&line->b, around, deg);
  32. }
  33. void line_group_translate(Line* line_group, uint8_t count, int32_t x, int32_t y) {
  34. for(uint8_t i = 0; i < count; i++) line_translate(&line_group[i], x, y);
  35. }
  36. void line_group_rotate(Line* line_group, uint8_t count, Point around, float deg) {
  37. for(uint8_t i = 0; i < count; i++) line_rotate(&line_group[i], around, deg);
  38. }
  39. Point line_group_get_center(Line* line_group, uint8_t count) {
  40. // Finding the corners
  41. int32_t min_x = INT32_MAX, min_y = INT32_MAX, max_x = INT32_MIN, max_y = INT32_MIN;
  42. for(uint8_t i = 0; i < count; i++) {
  43. min_x = MIN3(min_x, line_group[i].a.x, line_group[i].b.x);
  44. min_y = MIN3(min_y, line_group[i].a.y, line_group[i].b.y);
  45. max_x = MAX3(max_x, line_group[i].a.x, line_group[i].b.x);
  46. max_y = MAX3(max_y, line_group[i].a.y, line_group[i].b.y);
  47. }
  48. // Finding the center
  49. int32_t center_x = (min_x + max_x) / 2;
  50. int32_t center_y = (min_y + max_y) / 2;
  51. const Point center = {center_x, center_y};
  52. return center;
  53. }
  54. Point line_group_rotate_center(Line* line_group, uint8_t count, float deg) {
  55. const Point center = line_group_get_center(line_group, count);
  56. // Rotating around the center
  57. line_group_rotate(line_group, count, center, deg);
  58. return center;
  59. }