forked from aseprite/laf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rect.h
382 lines (319 loc) · 8.22 KB
/
rect.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// LAF Gfx Library
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef GFX_RECT_H_INCLUDED
#define GFX_RECT_H_INCLUDED
#pragma once
namespace gfx {
template<typename T> class PointT;
template<typename T> class SizeT;
template<typename T> class BorderT;
// A rectangle.
template<typename T>
class RectT {
public:
T x, y, w, h;
T x2() const { return x+w; }
T y2() const { return y+h; }
// Creates a new empty rectangle with the origin in 0,0.
RectT() : x(0), y(0), w(0), h(0) {
}
// Creates a new rectangle with the specified size with the origin in 0,0.
RectT(const T& w, const T& h) :
x(0), y(0),
w(w), h(h) {
}
// Creates a new rectangle with the specified size with the origin in 0,0.
explicit RectT(const SizeT<T>& size) :
x(0), y(0),
w(size.w), h(size.h) {
}
RectT(const RectT<T>& rect) :
x(rect.x), y(rect.y),
w(rect.w), h(rect.h) {
}
template<typename U>
RectT(const RectT<U>& rect) :
x(static_cast<T>(rect.x)), y(static_cast<T>(rect.y)),
w(static_cast<T>(rect.w)), h(static_cast<T>(rect.h)) {
}
RectT(const PointT<T>& point, const SizeT<T>& size) :
x(point.x), y(point.y),
w(size.w), h(size.h) {
}
// Creates a new rectangle with the origin in point1 and size
// equal to point2-point1.
//
// If a coordinate of point1 is greater than point2, the coordinates
// are swapped. The resulting rectangle will be:
//
// x = min(point1.x, point2.x)
// y = min(point1.y, point2.y)
// w = max(point1.x, point2.x) - x
// h = max(point1.x, point2.x) - y
//
// See that point2 isn't included in the rectangle, it's like the
// point returned by point2() member function.
RectT(const PointT<T>& point1, const PointT<T>& point2) {
PointT<T> leftTop = point1;
PointT<T> rightBottom = point2;
T t;
if (leftTop.x > rightBottom.x) {
t = leftTop.x;
leftTop.x = rightBottom.x;
rightBottom.x = t;
}
if (leftTop.y > rightBottom.y) {
t = leftTop.y;
leftTop.y = rightBottom.y;
rightBottom.y = t;
}
this->x = leftTop.x;
this->y = leftTop.y;
this->w = rightBottom.x - leftTop.x;
this->h = rightBottom.y - leftTop.y;
}
RectT(const T& x, const T& y, const T& w, const T& h) : x(x), y(y), w(w), h(h) {
}
// Verifies if the width and/or height of the rectangle are less or
// equal than zero.
bool isEmpty() const {
return (w <= 0 || h <= 0);
}
// Returns the middle point of the rectangle (x+w/2, y+h/2).
PointT<T> center() const {
return PointT<T>(x+w/2, y+h/2);
}
// Returns the point in the upper-left corner (that is inside the
// rectangle).
PointT<T> origin() const {
return PointT<T>(x, y);
}
// Returns point in the lower-right corner that is outside the
// rectangle (x+w, y+h).
PointT<T> point2() const {
return PointT<T>(x+w, y+h);
}
SizeT<T> size() const {
return SizeT<T>(w, h);
}
RectT& setOrigin(const PointT<T>& pt) {
x = pt.x;
y = pt.y;
return *this;
}
RectT& setSize(const SizeT<T>& sz) {
w = sz.w;
h = sz.h;
return *this;
}
// Moves the rectangle origin in the specified delta.
RectT& offset(const T& dx, const T& dy) {
x += dx;
y += dy;
return *this;
}
template<typename U>
RectT& offset(const PointT<U>& delta) {
x += delta.x;
y += delta.y;
return *this;
}
RectT& inflate(const T& delta) {
w += delta;
h += delta;
return *this;
}
RectT& inflate(const T& dw, const T& dh) {
w += dw;
h += dh;
return *this;
}
RectT& inflate(const SizeT<T>& delta) {
w += delta.w;
h += delta.h;
return *this;
}
RectT& enlarge(const T& unit) {
x -= unit;
y -= unit;
w += unit*2;
h += unit*2;
return *this;
}
RectT& enlarge(const BorderT<T>& br) {
x -= br.left();
y -= br.top();
w += br.left() + br.right();
h += br.top() + br.bottom();
return *this;
}
RectT& enlargeXW(const T& unit) {
x -= unit;
w += unit*2;
return *this;
}
RectT& enlargeYH(const T& unit) {
y -= unit;
h += unit*2;
return *this;
}
RectT& shrink(const T& unit) {
x += unit;
y += unit;
w -= unit*2;
h -= unit*2;
return *this;
}
RectT& shrink(const BorderT<T>& br) {
x += br.left();
y += br.top();
w -= br.left() + br.right();
h -= br.top() + br.bottom();
return *this;
}
// Returns true if this rectangle encloses the pt point.
bool contains(const PointT<T>& pt) const {
return
pt.x >= x && pt.x < x+w &&
pt.y >= y && pt.y < y+h;
}
bool contains(const T& u, const T& v) const {
return
u >= x && u < x+w &&
v >= y && v < y+h;
}
// Returns true if this rectangle entirely contains the rc rectangle.
bool contains(const RectT& rc) const {
if (isEmpty() || rc.isEmpty())
return false;
return
rc.x >= x && rc.x+rc.w <= x+w &&
rc.y >= y && rc.y+rc.h <= y+h;
}
// Returns true if the intersection between this rectangle with rc
// rectangle is not empty.
bool intersects(const RectT& rc) const {
if (isEmpty() || rc.isEmpty())
return false;
return
rc.x < x+w && rc.x+rc.w > x &&
rc.y < y+h && rc.y+rc.h > y;
}
// Returns the union rectangle between this and rc rectangle.
RectT createUnion(const RectT& rc) const {
if (isEmpty())
return rc;
else if (rc.isEmpty())
return *this;
else
return RectT(PointT<T>(x < rc.x ? x: rc.x,
y < rc.y ? y: rc.y),
PointT<T>(x+w > rc.x+rc.w ? x+w: rc.x+rc.w,
y+h > rc.y+rc.h ? y+h: rc.y+rc.h));
}
// Returns the intersection rectangle between this and rc rectangles.
RectT createIntersection(const RectT& rc) const {
if (intersects(rc))
return RectT(PointT<T>(x > rc.x ? x: rc.x,
y > rc.y ? y: rc.y),
PointT<T>(x+w < rc.x+rc.w ? x+w: rc.x+rc.w,
y+h < rc.y+rc.h ? y+h: rc.y+rc.h));
else
return RectT();
}
const RectT& operator+=(const BorderT<T>& br) {
enlarge(br);
return *this;
}
const RectT& operator-=(const BorderT<T>& br) {
shrink(br);
return *this;
}
RectT& operator*=(const T factor) {
x *= factor;
y *= factor;
w *= factor;
h *= factor;
return *this;
}
RectT& operator/=(const T factor) {
x /= factor;
y /= factor;
w /= factor;
h /= factor;
return *this;
}
RectT& operator*=(const SizeT<T>& size) {
x *= size.w;
y *= size.h;
w *= size.w;
h *= size.h;
return *this;
}
RectT& operator/=(const SizeT<T>& size) {
x /= size.w;
y /= size.h;
w /= size.w;
h /= size.h;
return *this;
}
const RectT& operator|=(const RectT& rc) {
return *this = createUnion(rc);
}
const RectT& operator&=(const RectT& rc) {
return *this = createIntersection(rc);
}
RectT operator+(const BorderT<T>& br) const {
return RectT(*this).enlarge(br);
}
RectT operator-(const BorderT<T>& br) const {
return RectT(*this).shrink(br);
}
RectT operator|(const RectT& other) const {
return createUnion(other);
}
RectT operator&(const RectT& other) const {
return createIntersection(other);
}
RectT operator*(const SizeT<T>& size) const {
return RectT(x*size.w, y*size.h,
w*size.w, h*size.h);
}
RectT operator/(const SizeT<T>& size) const {
return RectT(x/size.w, y/size.h,
w/size.w, h/size.h);
}
bool operator==(const RectT& rc) const {
return
x == rc.x && w == rc.w &&
y == rc.y && h == rc.h;
}
bool operator!=(const RectT& rc) const {
return
x != rc.x || w != rc.w ||
y != rc.y || h != rc.h;
}
RectT& fitIn(const RectT& bounds) {
if (w < h) {
w = w * bounds.h / h;
x = bounds.x + bounds.w/2 - w/2;
y = bounds.y;
h = bounds.h;
}
else {
h = h * bounds.w / w;
y = bounds.y + bounds.h/2 - h/2;
x = bounds.x;
w = bounds.w;
}
return *this;
}
};
typedef RectT<int> Rect;
typedef RectT<double> RectF;
} // namespace gfx
#endif