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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ppapi/cpp/rect.h"
#include <algorithm>
namespace {
template<typename T>
void AdjustAlongAxis(T dst_origin, T dst_size,
T* origin, T* size) {
if (*origin < dst_origin) {
*origin = dst_origin;
*size = std::min(dst_size, *size);
} else {
*size = std::min(dst_size, *size);
*origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
}
}
} // namespace
namespace pp {
void Rect::Inset(int32_t left, int32_t top, int32_t right, int32_t bottom) {
Offset(left, top);
set_width(std::max<int32_t>(width() - left - right, 0));
set_height(std::max<int32_t>(height() - top - bottom, 0));
}
void Rect::Offset(int32_t horizontal, int32_t vertical) {
rect_.point.x += horizontal;
rect_.point.y += vertical;
}
bool Rect::Contains(int32_t point_x, int32_t point_y) const {
return (point_x >= x()) && (point_x < right()) &&
(point_y >= y()) && (point_y < bottom());
}
bool Rect::Contains(const Rect& rect) const {
return (rect.x() >= x() && rect.right() <= right() &&
rect.y() >= y() && rect.bottom() <= bottom());
}
bool Rect::Intersects(const Rect& rect) const {
return !(rect.x() >= right() || rect.right() <= x() ||
rect.y() >= bottom() || rect.bottom() <= y());
}
Rect Rect::Intersect(const Rect& rect) const {
int32_t rx = std::max(x(), rect.x());
int32_t ry = std::max(y(), rect.y());
int32_t rr = std::min(right(), rect.right());
int32_t rb = std::min(bottom(), rect.bottom());
if (rx >= rr || ry >= rb)
rx = ry = rr = rb = 0; // non-intersecting
return Rect(rx, ry, rr - rx, rb - ry);
}
Rect Rect::Union(const Rect& rect) const {
// special case empty rects...
if (IsEmpty())
return rect;
if (rect.IsEmpty())
return *this;
int32_t rx = std::min(x(), rect.x());
int32_t ry = std::min(y(), rect.y());
int32_t rr = std::max(right(), rect.right());
int32_t rb = std::max(bottom(), rect.bottom());
return Rect(rx, ry, rr - rx, rb - ry);
}
Rect Rect::Subtract(const Rect& rect) const {
// boundary cases:
if (!Intersects(rect))
return *this;
if (rect.Contains(*this))
return Rect();
int32_t rx = x();
int32_t ry = y();
int32_t rr = right();
int32_t rb = bottom();
if (rect.y() <= y() && rect.bottom() >= bottom()) {
// complete intersection in the y-direction
if (rect.x() <= x()) {
rx = rect.right();
} else {
rr = rect.x();
}
} else if (rect.x() <= x() && rect.right() >= right()) {
// complete intersection in the x-direction
if (rect.y() <= y()) {
ry = rect.bottom();
} else {
rb = rect.y();
}
}
return Rect(rx, ry, rr - rx, rb - ry);
}
Rect Rect::AdjustToFit(const Rect& rect) const {
int32_t new_x = x();
int32_t new_y = y();
int32_t new_width = width();
int32_t new_height = height();
AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
return Rect(new_x, new_y, new_width, new_height);
}
Point Rect::CenterPoint() const {
return Point(x() + (width() + 1) / 2, y() + (height() + 1) / 2);
}
bool Rect::SharesEdgeWith(const Rect& rect) const {
return (y() == rect.y() && height() == rect.height() &&
(x() == rect.right() || right() == rect.x())) ||
(x() == rect.x() && width() == rect.width() &&
(y() == rect.bottom() || bottom() == rect.y()));
}
void FloatRect::Inset(float left, float top, float right, float bottom) {
Offset(left, top);
set_width(std::max<float>(width() - left - right, 0.0f));
set_height(std::max<float>(height() - top - bottom, 0.0f));
}
void FloatRect::Offset(float horizontal, float vertical) {
rect_.point.x += horizontal;
rect_.point.y += vertical;
}
bool FloatRect::Contains(float point_x, float point_y) const {
return (point_x >= x()) && (point_x < right()) &&
(point_y >= y()) && (point_y < bottom());
}
bool FloatRect::Contains(const FloatRect& rect) const {
return (rect.x() >= x() && rect.right() <= right() &&
rect.y() >= y() && rect.bottom() <= bottom());
}
bool FloatRect::Intersects(const FloatRect& rect) const {
return !(rect.x() >= right() || rect.right() <= x() ||
rect.y() >= bottom() || rect.bottom() <= y());
}
FloatRect FloatRect::Intersect(const FloatRect& rect) const {
float rx = std::max(x(), rect.x());
float ry = std::max(y(), rect.y());
float rr = std::min(right(), rect.right());
float rb = std::min(bottom(), rect.bottom());
if (rx >= rr || ry >= rb)
rx = ry = rr = rb = 0; // non-intersecting
return FloatRect(rx, ry, rr - rx, rb - ry);
}
FloatRect FloatRect::Union(const FloatRect& rect) const {
// special case empty rects...
if (IsEmpty())
return rect;
if (rect.IsEmpty())
return *this;
float rx = std::min(x(), rect.x());
float ry = std::min(y(), rect.y());
float rr = std::max(right(), rect.right());
float rb = std::max(bottom(), rect.bottom());
return FloatRect(rx, ry, rr - rx, rb - ry);
}
FloatRect FloatRect::Subtract(const FloatRect& rect) const {
// boundary cases:
if (!Intersects(rect))
return *this;
if (rect.Contains(*this))
return FloatRect();
float rx = x();
float ry = y();
float rr = right();
float rb = bottom();
if (rect.y() <= y() && rect.bottom() >= bottom()) {
// complete intersection in the y-direction
if (rect.x() <= x()) {
rx = rect.right();
} else {
rr = rect.x();
}
} else if (rect.x() <= x() && rect.right() >= right()) {
// complete intersection in the x-direction
if (rect.y() <= y()) {
ry = rect.bottom();
} else {
rb = rect.y();
}
}
return FloatRect(rx, ry, rr - rx, rb - ry);
}
FloatRect FloatRect::AdjustToFit(const FloatRect& rect) const {
float new_x = x();
float new_y = y();
float new_width = width();
float new_height = height();
AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
return FloatRect(new_x, new_y, new_width, new_height);
}
FloatPoint FloatRect::CenterPoint() const {
return FloatPoint(x() + (width() + 1.0f) / 2.0f,
y() + (height() + 1.0f) / 2.0f);
}
bool FloatRect::SharesEdgeWith(const FloatRect& rect) const {
return (y() == rect.y() && height() == rect.height() &&
(x() == rect.right() || right() == rect.x())) ||
(x() == rect.x() && width() == rect.width() &&
(y() == rect.bottom() || bottom() == rect.y()));
}
} // namespace pp
|