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-2024 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
33
39static inline int gp_bbox_empty(gp_bbox box)
40{
41 return box.w == 0 || box.h == 0;
42}
43
55{
56 gp_bbox box = {
57 .x = x,
58 .y = y,
59 .w = w,
60 .h = h,
61 };
62
63 return box;
64}
65
74static inline gp_bbox gp_bbox_merge(gp_bbox box1, gp_bbox box2)
75{
76 gp_bbox box = {
77 .x = GP_MIN(box1.x, box2.x),
78 .y = GP_MIN(box1.y, box2.y),
79 .w = GP_MAX(box1.x + box1.w, box2.x + box2.w) - GP_MIN(box1.x, box2.x),
80 .h = GP_MAX(box1.y + box1.h, box2.y + box2.h) - GP_MIN(box1.y, box2.y),
81 };
82
83 return box;
84}
85
95{
96 gp_bbox box = {
97 .x = GP_MAX(box1.x, box2.x),
98 .y = GP_MAX(box1.y, box2.y),
99 .w = GP_MIN(box1.x + box1.w, box2.x + box2.w) - GP_MAX(box1.x, box2.x),
100 .h = GP_MIN(box1.y + box1.h, box2.y + box2.h) - GP_MAX(box1.y, box2.y),
101 };
102
103 return box;
104}
105
114static inline int gp_bbox_intersects(gp_bbox box1, gp_bbox box2)
115{
116 if (box1.x > box2.x + (gp_coord)box2.w)
117 return 0;
118
119 if (box2.x > box1.x + (gp_coord)box1.w)
120 return 0;
121
122 if (box1.y > box2.y + (gp_coord)box2.h)
123 return 0;
124
125 if (box2.y > box1.y + (gp_coord)box1.h)
126 return 0;
127
128 return 1;
129}
130
141{
142 return gp_bbox_pack(box.x + x, box.y + y, box.w, box.h);
143}
144
146#define GP_BBOX_FMT "[%i, %i] w=%u h=%u"
148#define GP_BBOX_PARS(bbox) (bbox).x, (bbox).y, (bbox).w, (bbox).h
149
150#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:94
static gp_bbox gp_bbox_move(gp_bbox box, gp_coord x, gp_coord y)
Moves bounding box.
Definition gp_bbox.h:140
static gp_bbox gp_bbox_merge(gp_bbox box1, gp_bbox box2)
Merges two bounding boxes.
Definition gp_bbox.h:74
static int gp_bbox_empty(gp_bbox box)
Returns true if bounding box is empty.
Definition gp_bbox.h:39
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:54
static int gp_bbox_intersects(gp_bbox box1, gp_bbox box2)
Returns true if bouding boxes intersects.
Definition gp_bbox.h:114
A bounding box.
Definition gp_bbox.h:23
gp_size w
A bbox width.
Definition gp_bbox.h:29
gp_coord x
A left bbox corner coordinate.
Definition gp_bbox.h:25
gp_size h
A bbox height.
Definition gp_bbox.h:31
gp_coord y
A top bbox corner coordinate.
Definition gp_bbox.h:27