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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PPAPI_CPP_RECT_H_
#define PPAPI_CPP_RECT_H_
#include "ppapi/c/pp_rect.h"
#include "ppapi/cpp/point.h"
#include "ppapi/cpp/size.h"
/// @file
/// This file defines the APIs for creating a 2 dimensional rectangle.
namespace pp {
/// A 2 dimensional rectangle. A rectangle is represented by x and y (which
/// identifies the upper-left corner of the rectangle), width, and height.
class Rect {
public:
/// The default constructor. Creates a <code>Rect</code> in the upper-left
/// at 0,0 with height and width of 0.
Rect() {
rect_.point.x = 0;
rect_.point.y = 0;
rect_.size.width = 0;
rect_.size.height = 0;
}
/// A constructor accepting a reference to a <code>PP_Rect and</code>
/// converting the <code>PP_Rect</code> to a <code>Rect</code>. This is an
/// implicit conversion constructor.
///
/// @param[in] rect A <code>PP_Rect</code>.
Rect(const PP_Rect& rect) { // Implicit.
set_x(rect.point.x);
set_y(rect.point.y);
set_width(rect.size.width);
set_height(rect.size.height);
}
/// A constructor accepting two int32_t values for width and height and
/// converting them to a <code>Rect</code> in the upper-left starting
/// coordinate of 0,0.
///
/// @param[in] w An int32_t value representing a width.
/// @param[in] h An int32_t value representing a height.
Rect(int32_t w, int32_t h) {
set_x(0);
set_y(0);
set_width(w);
set_height(h);
}
/// A constructor accepting four int32_t values for width, height, x, and y.
///
/// @param[in] x An int32_t value representing a horizontal coordinate
/// of a point, starting with 0 as the left-most coordinate.
/// @param[in] y An int32_t value representing a vertical coordinate
/// of a point, starting with 0 as the top-most coordinate.
/// @param[in] w An int32_t value representing a width.
/// @param[in] h An int32_t value representing a height.
Rect(int32_t x, int32_t y, int32_t w, int32_t h) {
set_x(x);
set_y(y);
set_width(w);
set_height(h);
}
/// A constructor accepting a pointer to a Size and converting the
/// <code>Size</code> to a <code>Rect</code> in the upper-left starting
/// coordinate of 0,0.
///
/// @param[in] s A pointer to a <code>Size</code>.
explicit Rect(const Size& s) {
set_x(0);
set_y(0);
set_size(s);
}
/// A constructor accepting a pointer to a <code>Point</code> representing
/// the origin of the rectangle and a pointer to a <code>Size</code>
/// representing the height and width.
///
/// @param[in] origin A pointer to a <code>Point</code> representing the
/// upper-left starting coordinate.
/// @param[in] size A pointer to a <code>Size</code> representing the height
/// and width.
Rect(const Point& origin, const Size& size) {
set_point(origin);
set_size(size);
}
/// Destructor.
~Rect() {
}
/// PP_Rect() allows implicit conversion of a <code>Rect</code> to a
/// <code>PP_Rect</code>.
///
/// @return A <code>Point</code>.
operator PP_Rect() const {
return rect_;
}
/// Getter function for returning the internal <code>PP_Rect</code> struct.
///
/// @return A const reference to the internal <code>PP_Rect</code> struct.
const PP_Rect& pp_rect() const {
return rect_;
}
/// Getter function for returning the internal <code>PP_Rect</code> struct.
///
/// @return A mutable reference to the <code>PP_Rect</code> struct.
PP_Rect& pp_rect() {
return rect_;
}
/// Getter function for returning the value of x.
///
/// @return The value of x for this <code>Point</code>.
int32_t x() const {
return rect_.point.x;
}
/// Setter function for setting the value of x.
///
/// @param[in] in_x A new x value.
void set_x(int32_t in_x) {
rect_.point.x = in_x;
}
/// Getter function for returning the value of y.
///
/// @return The value of y for this <code>Point</code>.
int32_t y() const {
return rect_.point.y;
}
/// Setter function for setting the value of y.
///
/// @param[in] in_y A new y value.
void set_y(int32_t in_y) {
rect_.point.y = in_y;
}
/// Getter function for returning the value of width.
///
/// @return The value of width for this <code>Rect</code>.
int32_t width() const {
return rect_.size.width;
}
/// Setter function for setting the value of width.
///
/// @param[in] w A new width value.
void set_width(int32_t w) {
if (w < 0) {
PP_DCHECK(w >= 0);
w = 0;
}
rect_.size.width = w;
}
/// Getter function for returning the value of height.
///
/// @return The value of height for this <code>Rect</code>.
int32_t height() const {
return rect_.size.height;
}
/// Setter function for setting the value of height.
///
/// @param[in] h A new width height.
void set_height(int32_t h) {
if (h < 0) {
PP_DCHECK(h >= 0);
h = 0;
}
rect_.size.height = h;
}
/// Getter function for returning the <code>Point</code>.
///
/// @return A <code>Point</code>.
Point point() const {
return Point(rect_.point);
}
/// Setter function for setting the value of the <code>Point</code>.
///
/// @param[in] origin A <code>Point</code> representing the upper-left
/// starting coordinate.
void set_point(const Point& origin) {
rect_.point = origin;
}
/// Getter function for returning the <code>Size</code>.
///
/// @return The size of the rectangle.
Size size() const {
return Size(rect_.size);
}
/// Setter function for setting the <code>Size</code>.
///
/// @param[in] s A pointer to a <code>Size</code> representing the height
/// and width.
void set_size(const Size& s) {
rect_.size.width = s.width();
rect_.size.height = s.height();
}
/// Getter function to get the upper-bound for the x-coordinates of the
/// rectangle. Note that this coordinate value is one past the highest x
/// value of pixels in the rectangle. This loop will access all the pixels
/// in a horizontal line in the rectangle:
/// <code>for (int32_t x = rect.x(); x < rect.right(); ++x) {}</code>
///
/// @return The value of x + width for this point.
int32_t right() const {
return x() + width();
}
/// Getter function to get the upper-bound for the y-coordinates of the
/// rectangle. Note that this coordinate value is one past the highest xy
/// value of pixels in the rectangle. This loop will access all the pixels
/// in a horizontal line in the rectangle:
/// <code>for (int32_t y = rect.y(); y < rect.bottom(); ++y) {}</code>
///
/// @return The value of y + height for this point.
int32_t bottom() const {
return y() + height();
}
/// Setter function for setting the value of the <code>Rect</code>.
///
/// @param[in] x A new x value.
/// @param[in] y A new y value.
/// @param[in] w A new width value.
/// @param[in] h A new height value.
void SetRect(int32_t x, int32_t y, int32_t w, int32_t h) {
set_x(x);
set_y(y);
set_width(w);
set_height(h);
}
/// Setter function for setting the value of the <code>Rect</code>.
///
/// @param[in] rect A pointer to a <code>PP_Rect</code>.
void SetRect(const PP_Rect& rect) {
rect_ = rect;
}
/// Inset() shrinks the rectangle by a horizontal and vertical
/// distance on all sides.
///
/// @param[in] horizontal An int32_t value representing a horizontal
/// shrinking distance.
/// @param[in] vertical An int32_t value representing a vertical
/// shrinking distance.
void Inset(int32_t horizontal, int32_t vertical) {
Inset(horizontal, vertical, horizontal, vertical);
}
/// Inset() shrinks the rectangle by the specified amount on each
/// side.
///
/// @param[in] left An int32_t value representing a left
/// shrinking distance.
/// @param[in] top An int32_t value representing a top
/// shrinking distance.
/// @param[in] right An int32_t value representing a right
/// shrinking distance.
/// @param[in] bottom An int32_t value representing a bottom
/// shrinking distance.
void Inset(int32_t left, int32_t top, int32_t right, int32_t bottom);
/// Offset() moves the rectangle by a horizontal and vertical distance.
///
/// @param[in] horizontal An int32_t value representing a horzontal
/// move distance.
/// @param[in] vertical An int32_t value representing a vertical
/// move distance.
void Offset(int32_t horizontal, int32_t vertical);
/// Offset() moves the rectangle by a horizontal and vertical distance.
///
/// @param[in] point A pointer to a <code>Point</code> representing the
/// horizontal and vertical move distances.
void Offset(const Point& point) {
Offset(point.x(), point.y());
}
/// IsEmpty() determines if the area of a rectangle is zero. Returns true if
/// the area of the rectangle is zero.
///
/// @return true if the area of the rectangle is zero.
bool IsEmpty() const {
return rect_.size.width == 0 && rect_.size.height == 0;
}
/// Contains() determines if the point identified by point_x and point_y
/// falls inside this rectangle. The point (x, y) is inside the rectangle,
/// but the point (x + width, y + height) is not.
///
/// @param[in] point_x An int32_t value representing a x value.
/// @param[in] point_y An int32_t value representing a y value.
///
/// @return true if the point_x and point_y fall inside the rectangle.
bool Contains(int32_t point_x, int32_t point_y) const;
/// Contains() determines if the specified point is contained by this
/// rectangle.
///
/// @param[in] point A pointer to a Point representing a 2D coordinate.
///
/// @return true if the point_x and point_y fall inside the rectangle.
bool Contains(const Point& point) const {
return Contains(point.x(), point.y());
}
/// Contains() determines if this rectangle contains the specified rectangle.
///
/// @param[in] rect A pointer to a <code>Rect</code>.
///
/// @return true if the rectangle fall inside this rectangle.
bool Contains(const Rect& rect) const;
/// Insersects() determines if this rectangle intersects the specified
/// rectangle.
///
/// @param[in] rect A pointer to a <code>Rect</code>.
///
/// @return true if the rectangle intersects this rectangle.
bool Intersects(const Rect& rect) const;
/// Intersect() computes the intersection of this rectangle with the given
/// rectangle.
///
/// @param[in] rect A pointer to a <code>Rect</code>.
///
/// @return A <code>Rect</code> representing the intersection.
Rect Intersect(const Rect& rect) const;
/// Union() computes the union of this rectangle with the given rectangle.
/// The union is the smallest rectangle containing both rectangles.
///
/// @param[in] rect A pointer to a <code>Rect</code>.
///
/// @return A <code>Rect</code> representing the union.
Rect Union(const Rect& rect) const;
/// Subtract() computes the rectangle resulting from subtracting
/// <code>rect</code> from this Rect. If <code>rect</code>does not intersect
/// completely in either the x or y direction, then <code>*this</code> is
/// returned. If <code>rect</code> contains <code>this</code>, then an empty
/// <code>Rect</code> is returned.
///
/// @param[in] rect A pointer to a <code>Rect</code>.
///
/// @return A <code>Rect</code> representing the subtraction.
Rect Subtract(const Rect& rect) const;
/// AdjustToFit() fits as much of the receiving rectangle within
/// the supplied rectangle as possible, returning the result. For example,
/// if the receiver had a x-location of 2 and a width of 4, and the supplied
/// rectangle had an x-location of 0 with a width of 5, the returned
/// rectangle would have an x-location of 1 with a width of 4.
///
/// @param[in] rect A pointer to a <code>Rect</code>.
///
/// @return A <code>Rect</code> representing the difference between this
/// rectangle and the receiving rectangle.
Rect AdjustToFit(const Rect& rect) const;
/// CenterPoint() determines the center of this rectangle.
///
/// @return A <code>Point</code> representing the center of this rectangle.
Point CenterPoint() const;
/// SharesEdgeWith() determines if this rectangle shares an entire edge
/// (same width or same height) with the given rectangle, and the
/// rectangles do not overlap.
///
/// @param[in] rect A pointer to a <code>Rect</code>.
///
/// @return true if this rectangle and supplied rectangle share an edge.
bool SharesEdgeWith(const Rect& rect) const;
private:
PP_Rect rect_;
};
} // namespace pp
/// This function determines whether the x, y, width, and height values of two
/// rectangles and are equal.
///
/// @param[in] lhs The <code>Rect</code> on the left-hand side of the equation.
/// @param[in] rhs The <code>Rect</code> on the right-hand side of the equation.
///
/// @return true if they are equal, false if unequal.
inline bool operator==(const pp::Rect& lhs, const pp::Rect& rhs) {
return lhs.x() == rhs.x() &&
lhs.y() == rhs.y() &&
lhs.width() == rhs.width() &&
lhs.height() == rhs.height();
}
/// This function determines whether two Rects are not equal.
///
/// @param[in] lhs The <code>Rect</code> on the left-hand side of the equation.
/// @param[in] rhs The <code>Rect</code> on the right-hand side of the
/// equation.
///
/// @return true if the given Rects are equal, otherwise false.
inline bool operator!=(const pp::Rect& lhs, const pp::Rect& rhs) {
return !(lhs == rhs);
}
#endif
|