GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_bbox.h
Go to the documentation of this file.
1//SPDX-License-Identifier: LGPL-2.0-or-later
2
3/*
4
5 Copyright (c) 2014-2020 Cyril Hrubis <metan@ucw.cz>
6
7 */
8
14#ifndef GP_BBOX_H
15#define GP_BBOX_H
16
17#include <core/gp_types.h>
18#include <core/gp_common.h>
19
23typedef struct gp_bbox {
24 gp_coord x;
25 gp_coord y;
26 gp_size w;
27 gp_size h;
29
35static inline int gp_bbox_empty(gp_bbox box)
36{
37 return box.w == 0 || box.h == 0;
38}
39
51{
52 gp_bbox box = {
53 .x = x,
54 .y = y,
55 .w = w,
56 .h = h,
57 };
58
59 return box;
60}
61
70static inline gp_bbox gp_bbox_merge(gp_bbox box1, gp_bbox box2)
71{
72 gp_bbox box = {
73 .x = GP_MIN(box1.x, box2.x),
74 .y = GP_MIN(box1.y, box2.y),
75 .w = GP_MAX(box1.x + box1.w, box2.x + box2.w) - GP_MIN(box1.x, box2.x),
76 .h = GP_MAX(box1.y + box1.h, box2.y + box2.h) - GP_MIN(box1.y, box2.y),
77 };
78
79 return box;
80}
81
91{
92 gp_bbox box = {
93 .x = GP_MAX(box1.x, box2.x),
94 .y = GP_MAX(box1.y, box2.y),
95 .w = GP_MIN(box1.x + box1.w, box2.x + box2.w) - GP_MAX(box1.x, box2.x),
96 .h = GP_MIN(box1.y + box1.h, box2.y + box2.h) - GP_MAX(box1.y, box2.y),
97 };
98
99 return box;
100}
101
110static inline int gp_bbox_intersects(gp_bbox box1, gp_bbox box2)
111{
112 if (box1.x > box2.x + (gp_coord)box2.w)
113 return 0;
114
115 if (box2.x > box1.x + (gp_coord)box1.w)
116 return 0;
117
118 if (box1.y > box2.y + (gp_coord)box2.h)
119 return 0;
120
121 if (box2.y > box1.y + (gp_coord)box1.h)
122 return 0;
123
124 return 1;
125}
126
137{
138 return gp_bbox_pack(box.x + x, box.y + y, box.w, box.h);
139}
140
142#define GP_BBOX_FMT "[%i, %i] w=%u h=%u"
144#define GP_BBOX_PARS(bbox) (bbox).x, (bbox).y, (bbox).w, (bbox).h
145
146#endif /* GP_BBOX_H */
Common macros.
#define GP_MAX(a, b)
Returns a maximum of the two numbers.
Definition gp_common.h:59
#define GP_MIN(a, b)
Returns a minimum of the two numbers.
Definition gp_common.h:31
A common types.
int gp_coord
Integer type for coordinates i.e. x, y, ...
Definition gp_types.h:19
unsigned int gp_size
Integer type for sizes i.e. w, h, ...
Definition gp_types.h:24
static gp_bbox gp_bbox_intersection(gp_bbox box1, gp_bbox box2)
Calculates a bounding box intersection.
Definition gp_bbox.h:90
static gp_bbox gp_bbox_move(gp_bbox box, gp_coord x, gp_coord y)
Moves bounding box.
Definition gp_bbox.h:136
static gp_bbox gp_bbox_merge(gp_bbox box1, gp_bbox box2)
Merges two bounding boxes.
Definition gp_bbox.h:70
static int gp_bbox_empty(gp_bbox box)
Returns true if bounding box is empty.
Definition gp_bbox.h:35
static gp_bbox gp_bbox_pack(gp_coord x, gp_coord y, gp_coord w, gp_coord h)
Creates a bounding box from coordinates and size.
Definition gp_bbox.h:50
static int gp_bbox_intersects(gp_bbox box1, gp_bbox box2)
Returns true if bouding boxes intersects.
Definition gp_bbox.h:110
A bounding box.
Definition gp_bbox.h:23