-
Notifications
You must be signed in to change notification settings - Fork 0
/
irsrect.h
375 lines (320 loc) · 8.63 KB
/
irsrect.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
//! \file
//! \ingroup single_type_group
//! \brief Rect
//!
//! Äàòà: 06.05.2011\n
//! Äàòà ñîçäàíèÿ: 15.04.2010
#ifndef IRSRECTH
#define IRSRECTH
#include <irsdefs.h>
#include <irscpp.h>
#include <irslimits.h>
#include <irsfinal.h>
namespace irs {
//! \addtogroup single_type_group
//! @{
struct point_t {
typedef irs_size_t size_type;
size_type left;
size_type top;
point_t(
size_type a_left = 0,
size_type a_top = 0
):
left(a_left),
top(a_top)
{}
inline bool operator==(const point_t& a_point);
inline bool operator!=(const point_t& a_point);
};
inline bool point_t::operator==(const point_t& a_point)
{
return (left == a_point.left) && (top == a_point.top);
}
inline bool point_t::operator!=(const point_t& a_point)
{
return !operator==(a_point);
}
struct rect_t {
typedef irs_size_t size_type;
typedef irs_ptrdiff_t difference_type;
size_type left;
size_type top;
size_type right;
size_type bottom;
rect_t(
size_type a_left = 0,
size_type a_top = 0,
size_type a_right = 0,
size_type a_bottom = 0
):
left(a_left),
top(a_top),
right(a_right),
bottom(a_bottom)
{}
rect_t(const rect_t& a_rect):
left(a_rect.left),
top(a_rect.top),
right(a_rect.right),
bottom(a_rect.bottom)
{}
inline bool operator==(const rect_t& a_rect) const;
inline bool operator!=(const rect_t& a_rect) const;
inline size_type width() const;
inline size_type height() const;
//! \brief Ïåðåìåùàåò äèàïàçîí â òî÷êó ñ êîîðäèíàòàìè (a_left, a_top).
inline void move_to(const size_type a_left, const size_type a_top);
//! \brief Ïåðåìåùàåò äèïàçîí â òî÷êó a_left_top.
inline void move_to(const point_t& a_left_top);
inline void move_left(const size_type a_left);
inline void move_top(const size_type a_top);
inline void move_right(const size_type a_right);
inline void move_bottom(const size_type a_bottom);
inline void offset(const difference_type a_x_diff,
const difference_type a_y_diff);
inline void union_with(const rect_t& a_rect);
inline rect_t get_united(const rect_t& a_rect) const;
//! \brief Âîçâðàùàåò true, åñëè òî÷êà ñ êîîðäèíàòàìè (a_left, a_top)
//! âõîäèò â äèàïàçîí.
inline bool contains(const size_type a_left, const size_type a_top) const;
//! \brief Âîçâðàùàåò true, åñëè òî÷êà a_point âõîäèò â äèàïàçîí.
inline bool contains(const point_t& a_point) const;
//! \brief Âîçâðàùàåò true, åñëè äèàïàçîí a_rect ÿâëÿåòñÿ ïîääèàïàçîíîì.
inline bool contains(const rect_t& a_rect) const;
//intersect
//get_intersect
inline bool intersects_with(const rect_t& a_rect) const;
};
// rect_intersect & | &= |= == != += -=
inline rect_t rect_union(const rect_t& a_first_rect,
const rect_t& a_second_rect)
{
return a_first_rect.get_united(a_second_rect);
}
inline bool rect_t::operator==(const rect_t& a_rect) const
{
return (left == a_rect.left) &&
(top == a_rect.top) &&
(right == a_rect.right) &&
(bottom == a_rect.bottom);
}
inline bool rect_t::operator!=(const rect_t& a_rect) const
{
return !operator==(a_rect);
}
inline rect_t::size_type rect_t::width() const
{
return right - left + 1;
}
inline rect_t::size_type rect_t::height() const
{
return bottom - top + 1;
}
inline void rect_t::move_to(const size_type a_left, const size_type a_top)
{
right += (a_left - left);
bottom += (a_top - bottom);
left = a_left;
top = a_top;
}
inline void rect_t::move_to(const point_t& a_left_top)
{
right += (a_left_top.left - left);
bottom += (a_left_top.top - bottom);
left = a_left_top.left;
top = a_left_top.top;
}
inline void rect_t::move_left(const size_type a_left)
{
right += (a_left - left);
left = a_left;
}
inline void rect_t::move_top(const size_type a_top)
{
bottom += (a_top - top);
top = a_top;
}
inline void rect_t::move_right(const size_type a_right)
{
left += (a_right - right);
right = a_right;
}
inline void rect_t::move_bottom(const size_type a_bottom)
{
top += (a_bottom - bottom);
bottom = a_bottom;
}
inline void rect_t::offset(const difference_type a_x_diff,
const difference_type a_y_diff)
{
left += a_x_diff;
right += a_x_diff;
top += a_y_diff;
bottom += a_y_diff;
}
inline void rect_t::union_with(const rect_t& a_rect)
{
left = min(left, a_rect.left);
top = min(top, a_rect.top);
right = max(right, a_rect.right);
bottom = max(bottom, a_rect.bottom);
}
inline rect_t rect_t::get_united(const rect_t& a_rect) const
{
rect_t rect_result(*this);
rect_result.union_with(a_rect);
return rect_result;
}
inline bool rect_t::contains(const size_type a_left,
const size_type a_top) const
{
return (a_left >= left) && (a_left <= right) && (a_top >= top) &&
(a_top <= bottom);
}
inline bool rect_t::contains(const point_t& a_point) const
{
return contains(a_point.left, a_point.top);
}
inline bool rect_t::contains(const rect_t& a_rect) const
{
return (a_rect.left >= left) && (a_rect.right <= right) &&
(a_rect.top >= top) && (a_rect.bottom <= bottom);
}
inline bool rect_t::intersects_with(const rect_t& a_rect) const
{
return
(
((a_rect.left <= left) && (left <= a_rect.right)) ||
((left <= a_rect.left) && (a_rect.left <= right))
) && (
((a_rect.top <= top) && (top <= a_rect.bottom)) ||
((top <= a_rect.top) && (a_rect.top <= bottom))
);
}
template <class T>
struct mx_point_t {
typedef T value_type;
value_type left;
value_type top;
mx_point_t(
value_type a_left = value_type(),
value_type a_top = value_type()
):
left(a_left),
top(a_top)
{
}
};
template <class T>
bool operator==(const mx_point_t<T>& a_first,
const mx_point_t<T>& a_second)
{
return (a_first.left == a_second.left) && (a_first.top == a_second.top);
}
template <class T>
bool operator!=(const mx_point_t<T>& a_first,
const mx_point_t<T>& a_second)
{
return !(a_first == a_second);
}
template <class T>
struct mx_rect_t {
typedef T value_type;
value_type left;
value_type top;
value_type right;
value_type bottom;
mx_rect_t(
value_type a_left = value_type(),
value_type a_top = value_type(),
value_type a_right = value_type(),
value_type a_bottom = value_type()
):
left(a_left),
top(a_top),
right(a_right),
bottom(a_bottom)
{
}
value_type width() const;
value_type height() const;
};
template <class T>
typename mx_rect_t<T>::value_type mx_rect_width(
const mx_rect_t<T>& a_rect, integral_type_tag)
{
return a_rect.right - a_rect.left + 1;
}
template <class T>
typename mx_rect_t<T>::value_type mx_rect_width(
const mx_rect_t<T>& a_rect, floating_point_type_tag)
{
return a_rect.right - a_rect.left;
}
template <class T>
typename mx_rect_t<T>::value_type mx_rect_height(
const mx_rect_t<T>& a_rect, integral_type_tag)
{
return a_rect.bottom - a_rect.top + 1;
}
template <class T>
typename mx_rect_t<T>::value_type mx_rect_height(
const mx_rect_t<T>& a_rect, floating_point_type_tag)
{
// top - bottom ñäåëàíî íàìåðåíî. Ñ÷èòàåì, ÷òî íàïðàâëåíèÿ óâåëè÷åíèÿ
// ïî îñè Y êàê â ãðàôèêàõ ââåðõ, à íå êàê â ôîðìàõ è îêíàõ âíèç
return a_rect.top - a_rect.bottom;
}
template <class T>
typename mx_rect_t<T>::value_type mx_rect_t<T>::width() const
{
return mx_rect_width(*this, type_detect_t<T>::tag_type);
}
template <class T>
typename mx_rect_t<T>::value_type mx_rect_t<T>::height() const
{
return mx_rect_height(*this, type_detect_t<T>::tag_type);
}
template <class T>
bool operator==(const mx_rect_t<T>& a_first,
const mx_rect_t<T>& a_second)
{
return (a_first.left == a_second.left) && (a_first.top == a_second.top) &&
(a_first.right == a_second.right) && (a_first.bottom == a_second.bottom);
}
template <class T>
bool operator!=(const mx_rect_t<T>& a_first,
const mx_rect_t<T>& a_second)
{
return !(a_first == a_second);
}
template <class T>
struct mx_bounds_t {
typedef T value_type;
value_type begin;
value_type end;
mx_bounds_t(
value_type a_begin = value_type(),
value_type a_end = value_type()
):
begin(a_begin),
end(a_end)
{
}
};
template <class T>
bool operator==(const mx_bounds_t<T>& a_first,
const mx_bounds_t<T>& a_second)
{
return (a_first.begin == a_second.begin) && (a_first.end == a_second.end);
}
template <class T>
bool operator!=(const mx_bounds_t<T>& a_first,
const mx_bounds_t<T>& a_second)
{
return !(a_first == a_second);
}
//! @}
} //namespace irs
#endif //IRSDEFSH