rect.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef RECT_H
  2. #define RECT_H
  3. #include "typedef.h"
  4. #define AT_UI_RAM AT(.ui_ram)
  5. struct position {
  6. int x;
  7. int y;
  8. };
  9. struct rect {
  10. int left;
  11. int top;
  12. int width;
  13. int height;
  14. };
  15. #define rect_left(r) ((r)->left)
  16. #define rect_top(r) ((r)->top)
  17. #define rect_right(r) ((r)->left + (r)->width)
  18. #define rect_bottom(r) ((r)->top + (r)->height)
  19. //#define rect_height(v) ((v)->bottom - (v)->top)
  20. //#define rect_width(v) ((v)->right - (v)->left)
  21. static inline int in_rect(const struct rect *rect, struct position *pos)
  22. {
  23. if (rect->left <= pos->x && rect_right(rect) > pos->x) {
  24. if (rect->top <= pos->y && rect_bottom(rect) > pos->y) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. AT_UI_RAM
  31. static inline bool get_rect_cover(const struct rect *a, const struct rect *b, struct rect *c)
  32. {
  33. int right, bottom;
  34. c->top = MAX(a->top, b->top);
  35. c->left = MAX(a->left, b->left);
  36. right = MIN(rect_right(a), rect_right(b));
  37. bottom = MIN(rect_bottom(a), rect_bottom(b));
  38. if ((c->top < bottom) && (c->left < right)) {
  39. c->width = right - c->left;
  40. c->height = bottom - c->top;
  41. return true;
  42. }
  43. return false;
  44. }
  45. static inline bool get_rect_nocover_l(const struct rect *a, const struct rect *b, struct rect *c)
  46. {
  47. int right, bottom;
  48. c->left = MIN(rect_left(a), rect_left(b));
  49. c->top = MIN(rect_top(a), rect_top(b));
  50. right = MAX(rect_left(a), rect_left(b));
  51. bottom = MAX(rect_bottom(a), rect_bottom(b));
  52. if ((c->top < bottom) && (c->left < right)) {
  53. c->width = right - c->left;
  54. c->height = bottom - c->top;
  55. return true;
  56. }
  57. return false;
  58. }
  59. static inline bool get_rect_nocover_r(const struct rect *a, const struct rect *b, struct rect *c)
  60. {
  61. int right, bottom;
  62. c->left = MIN(rect_right(a), rect_right(b));
  63. c->top = MIN(rect_top(a), rect_top(b));
  64. right = MAX(rect_right(a), rect_right(b));
  65. bottom = MAX(rect_bottom(a), rect_bottom(b));
  66. if ((c->top < bottom) && (c->left < right)) {
  67. c->width = right - c->left;
  68. c->height = bottom - c->top;
  69. return true;
  70. }
  71. return false;
  72. }
  73. static inline bool get_rect_nocover_t(const struct rect *a, const struct rect *b, struct rect *c)
  74. {
  75. int right, bottom;
  76. c->left = MIN(rect_left(a), rect_left(b));
  77. c->top = MIN(rect_top(a), rect_top(b));
  78. right = MAX(rect_right(a), rect_right(b));
  79. bottom = MAX(rect_top(a), rect_top(b));
  80. if ((c->top < bottom) && (c->left < right)) {
  81. c->width = right - c->left;
  82. c->height = bottom - c->top;
  83. return true;
  84. }
  85. return false;
  86. }
  87. static inline bool get_rect_nocover_b(const struct rect *a, const struct rect *b, struct rect *c)
  88. {
  89. int right, bottom;
  90. c->left = MIN(rect_left(a), rect_left(b));
  91. c->top = MIN(rect_bottom(a), rect_bottom(b));
  92. right = MAX(rect_right(a), rect_right(b));
  93. bottom = MAX(rect_bottom(a), rect_bottom(b));
  94. if ((c->top < bottom) && (c->left < right)) {
  95. c->width = right - c->left;
  96. c->height = bottom - c->top;
  97. return true;
  98. }
  99. return false;
  100. }
  101. #endif