2
0

rect.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0-or-later
  3. */
  4. #ifndef QEMU_RECT_H
  5. #define QEMU_RECT_H
  6. typedef struct QemuRect {
  7. int16_t x;
  8. int16_t y;
  9. uint16_t width;
  10. uint16_t height;
  11. } QemuRect;
  12. static inline void qemu_rect_init(QemuRect *rect,
  13. int16_t x, int16_t y,
  14. uint16_t width, uint16_t height)
  15. {
  16. rect->x = x;
  17. rect->y = y;
  18. rect->width = width;
  19. rect->height = height;
  20. }
  21. static inline void qemu_rect_translate(QemuRect *rect,
  22. int16_t dx, int16_t dy)
  23. {
  24. rect->x += dx;
  25. rect->y += dy;
  26. }
  27. static inline bool qemu_rect_intersect(const QemuRect *a, const QemuRect *b,
  28. QemuRect *res)
  29. {
  30. int16_t x1, x2, y1, y2;
  31. x1 = MAX(a->x, b->x);
  32. y1 = MAX(a->y, b->y);
  33. x2 = MIN(a->x + a->width, b->x + b->width);
  34. y2 = MIN(a->y + a->height, b->y + b->height);
  35. if (x1 >= x2 || y1 >= y2) {
  36. if (res) {
  37. qemu_rect_init(res, 0, 0, 0, 0);
  38. }
  39. return false;
  40. }
  41. if (res) {
  42. qemu_rect_init(res, x1, y1, x2 - x1, y2 - y1);
  43. }
  44. return true;
  45. }
  46. #endif