1/**************************************************************************/
2/* rect2i.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef RECT2I_H
32#define RECT2I_H
33
34#include "core/error/error_macros.h"
35#include "core/math/vector2i.h"
36
37class String;
38struct Rect2;
39
40struct _NO_DISCARD_ Rect2i {
41 Point2i position;
42 Size2i size;
43
44 const Point2i &get_position() const { return position; }
45 void set_position(const Point2i &p_position) { position = p_position; }
46 const Size2i &get_size() const { return size; }
47 void set_size(const Size2i &p_size) { size = p_size; }
48
49 int get_area() const { return size.width * size.height; }
50
51 _FORCE_INLINE_ Vector2i get_center() const { return position + (size / 2); }
52
53 inline bool intersects(const Rect2i &p_rect) const {
54#ifdef MATH_CHECKS
55 if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
56 ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
57 }
58#endif
59 if (position.x >= (p_rect.position.x + p_rect.size.width)) {
60 return false;
61 }
62 if ((position.x + size.width) <= p_rect.position.x) {
63 return false;
64 }
65 if (position.y >= (p_rect.position.y + p_rect.size.height)) {
66 return false;
67 }
68 if ((position.y + size.height) <= p_rect.position.y) {
69 return false;
70 }
71
72 return true;
73 }
74
75 inline bool encloses(const Rect2i &p_rect) const {
76#ifdef MATH_CHECKS
77 if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
78 ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
79 }
80#endif
81 return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
82 ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
83 ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
84 }
85
86 _FORCE_INLINE_ bool has_area() const {
87 return size.x > 0 && size.y > 0;
88 }
89
90 // Returns the intersection between two Rect2is or an empty Rect2i if there is no intersection.
91 inline Rect2i intersection(const Rect2i &p_rect) const {
92 Rect2i new_rect = p_rect;
93
94 if (!intersects(new_rect)) {
95 return Rect2i();
96 }
97
98 new_rect.position.x = MAX(p_rect.position.x, position.x);
99 new_rect.position.y = MAX(p_rect.position.y, position.y);
100
101 Point2i p_rect_end = p_rect.position + p_rect.size;
102 Point2i end = position + size;
103
104 new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x;
105 new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
106
107 return new_rect;
108 }
109
110 inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
111#ifdef MATH_CHECKS
112 if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
113 ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
114 }
115#endif
116 Rect2i new_rect;
117
118 new_rect.position.x = MIN(p_rect.position.x, position.x);
119 new_rect.position.y = MIN(p_rect.position.y, position.y);
120
121 new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
122 new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
123
124 new_rect.size = new_rect.size - new_rect.position; // Make relative again.
125
126 return new_rect;
127 }
128 bool has_point(const Point2i &p_point) const {
129#ifdef MATH_CHECKS
130 if (unlikely(size.x < 0 || size.y < 0)) {
131 ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
132 }
133#endif
134 if (p_point.x < position.x) {
135 return false;
136 }
137 if (p_point.y < position.y) {
138 return false;
139 }
140
141 if (p_point.x >= (position.x + size.x)) {
142 return false;
143 }
144 if (p_point.y >= (position.y + size.y)) {
145 return false;
146 }
147
148 return true;
149 }
150
151 bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
152 bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
153
154 Rect2i grow(int p_amount) const {
155 Rect2i g = *this;
156 g.position.x -= p_amount;
157 g.position.y -= p_amount;
158 g.size.width += p_amount * 2;
159 g.size.height += p_amount * 2;
160 return g;
161 }
162
163 inline Rect2i grow_side(Side p_side, int p_amount) const {
164 Rect2i g = *this;
165 g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
166 (SIDE_TOP == p_side) ? p_amount : 0,
167 (SIDE_RIGHT == p_side) ? p_amount : 0,
168 (SIDE_BOTTOM == p_side) ? p_amount : 0);
169 return g;
170 }
171
172 inline Rect2i grow_side_bind(uint32_t p_side, int p_amount) const {
173 return grow_side(Side(p_side), p_amount);
174 }
175
176 inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const {
177 Rect2i g = *this;
178 g.position.x -= p_left;
179 g.position.y -= p_top;
180 g.size.width += p_left + p_right;
181 g.size.height += p_top + p_bottom;
182
183 return g;
184 }
185
186 _FORCE_INLINE_ Rect2i expand(const Vector2i &p_vector) const {
187 Rect2i r = *this;
188 r.expand_to(p_vector);
189 return r;
190 }
191
192 inline void expand_to(const Point2i &p_vector) {
193#ifdef MATH_CHECKS
194 if (unlikely(size.x < 0 || size.y < 0)) {
195 ERR_PRINT("Rect2i size is negative, this is not supported. Use Rect2i.abs() to get a Rect2i with a positive size.");
196 }
197#endif
198 Point2i begin = position;
199 Point2i end = position + size;
200
201 if (p_vector.x < begin.x) {
202 begin.x = p_vector.x;
203 }
204 if (p_vector.y < begin.y) {
205 begin.y = p_vector.y;
206 }
207
208 if (p_vector.x > end.x) {
209 end.x = p_vector.x;
210 }
211 if (p_vector.y > end.y) {
212 end.y = p_vector.y;
213 }
214
215 position = begin;
216 size = end - begin;
217 }
218
219 _FORCE_INLINE_ Rect2i abs() const {
220 return Rect2i(Point2i(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs());
221 }
222
223 _FORCE_INLINE_ void set_end(const Vector2i &p_end) {
224 size = p_end - position;
225 }
226
227 _FORCE_INLINE_ Vector2i get_end() const {
228 return position + size;
229 }
230
231 operator String() const;
232 operator Rect2() const;
233
234 Rect2i() {}
235 Rect2i(int p_x, int p_y, int p_width, int p_height) :
236 position(Point2i(p_x, p_y)),
237 size(Size2i(p_width, p_height)) {
238 }
239 Rect2i(const Point2i &p_pos, const Size2i &p_size) :
240 position(p_pos),
241 size(p_size) {
242 }
243};
244
245#endif // RECT2I_H
246