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