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