blob: b47226bd707c9f6058fc7d2fd16381d7e88c19bb [file] [log] [blame]
[email protected]100b1682011-06-07 23:05:501// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]1758e882010-11-01 16:16:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef PPAPI_CPP_RECT_H_
6#define PPAPI_CPP_RECT_H_
7
8#include "ppapi/c/pp_rect.h"
9#include "ppapi/cpp/point.h"
10#include "ppapi/cpp/size.h"
11
[email protected]100b1682011-06-07 23:05:5012/// @file
13/// This file defines the APIs for creating a 2 dimensional rectangle.
14
[email protected]1758e882010-11-01 16:16:5015namespace pp {
16
[email protected]100b1682011-06-07 23:05:5017/// A 2 dimensional rectangle. A rectangle is represented by x and y (which
18/// identifies the upper-left corner of the rectangle), width, and height.
[email protected]1758e882010-11-01 16:16:5019class Rect {
20 public:
[email protected]100b1682011-06-07 23:05:5021
[email protected]b15cce52011-07-06 20:44:3122 /// The default constructor. Creates a <code>Rect</code> in the upper-left
23 /// at 0,0 with height and width of 0.
[email protected]1758e882010-11-01 16:16:5024 Rect() {
25 rect_.point.x = 0;
26 rect_.point.y = 0;
27 rect_.size.width = 0;
28 rect_.size.height = 0;
29 }
[email protected]100b1682011-06-07 23:05:5030
[email protected]b15cce52011-07-06 20:44:3131 /// A constructor accepting a reference to a <code>PP_Rect and</code>
32 /// converting the <code>PP_Rect</code> to a <code>Rect</code>. This is an
33 /// implicit conversion constructor.
34 ///
35 /// @param[in] rect A <code>PP_Rect</code>.
[email protected]1758e882010-11-01 16:16:5036 Rect(const PP_Rect& rect) { // Implicit.
37 set_x(rect.point.x);
38 set_y(rect.point.y);
39 set_width(rect.size.width);
40 set_height(rect.size.height);
41 }
[email protected]100b1682011-06-07 23:05:5042
43 /// A constructor accepting two int32_t values for width and height and
[email protected]b15cce52011-07-06 20:44:3144 /// converting them to a <code>Rect</code> in the upper-left starting
45 /// coordinate of 0,0.
46 ///
[email protected]100b1682011-06-07 23:05:5047 /// @param[in] w An int32_t value representing a width.
48 /// @param[in] h An int32_t value representing a height.
[email protected]1758e882010-11-01 16:16:5049 Rect(int32_t w, int32_t h) {
50 set_x(0);
51 set_y(0);
52 set_width(w);
53 set_height(h);
54 }
[email protected]100b1682011-06-07 23:05:5055
56 /// A constructor accepting four int32_t values for width, height, x, and y.
[email protected]b15cce52011-07-06 20:44:3157 ///
[email protected]100b1682011-06-07 23:05:5058 /// @param[in] x An int32_t value representing a horizontal coordinate
59 /// of a point, starting with 0 as the left-most coordinate.
60 /// @param[in] y An int32_t value representing a vertical coordinate
61 /// of a point, starting with 0 as the top-most coordinate.
62 /// @param[in] w An int32_t value representing a width.
63 /// @param[in] h An int32_t value representing a height.
[email protected]1758e882010-11-01 16:16:5064 Rect(int32_t x, int32_t y, int32_t w, int32_t h) {
65 set_x(x);
66 set_y(y);
67 set_width(w);
68 set_height(h);
69 }
[email protected]100b1682011-06-07 23:05:5070
[email protected]b15cce52011-07-06 20:44:3171 /// A constructor accepting a pointer to a Size and converting the
72 /// <code>Size</code> to a <code>Rect</code> in the upper-left starting
73 /// coordinate of 0,0.
74 ///
75 /// @param[in] s A pointer to a <code>Size</code>.
[email protected]1758e882010-11-01 16:16:5076 explicit Rect(const Size& s) {
77 set_x(0);
78 set_y(0);
79 set_size(s);
80 }
[email protected]100b1682011-06-07 23:05:5081
[email protected]b15cce52011-07-06 20:44:3182 /// A constructor accepting a pointer to a <code>Point</code> representing
[email protected]80426e1b2011-08-09 18:56:1683 /// the origin of the rectangle and a pointer to a <code>Size</code>
84 /// representing the height and width.
[email protected]b15cce52011-07-06 20:44:3185 ///
[email protected]80426e1b2011-08-09 18:56:1686 /// @param[in] origin A pointer to a <code>Point</code> representing the
[email protected]b15cce52011-07-06 20:44:3187 /// upper-left starting coordinate.
88 /// @param[in] size A pointer to a <code>Size</code> representing the height
89 /// and width.
[email protected]1758e882010-11-01 16:16:5090 Rect(const Point& origin, const Size& size) {
91 set_point(origin);
92 set_size(size);
93 }
94
[email protected]100b1682011-06-07 23:05:5095 /// Destructor.
[email protected]1758e882010-11-01 16:16:5096 ~Rect() {
97 }
98
[email protected]b15cce52011-07-06 20:44:3199 /// PP_Rect() allows implicit conversion of a <code>Rect</code> to a
100 /// <code>PP_Rect</code>.
101 ///
102 /// @return A <code>Point</code>.
[email protected]1758e882010-11-01 16:16:50103 operator PP_Rect() const {
104 return rect_;
105 }
[email protected]100b1682011-06-07 23:05:50106
[email protected]b15cce52011-07-06 20:44:31107 /// Getter function for returning the internal <code>PP_Rect</code> struct.
108 ///
109 /// @return A const reference to the internal <code>PP_Rect</code> struct.
[email protected]1758e882010-11-01 16:16:50110 const PP_Rect& pp_rect() const {
111 return rect_;
112 }
[email protected]100b1682011-06-07 23:05:50113
[email protected]b15cce52011-07-06 20:44:31114 /// Getter function for returning the internal <code>PP_Rect</code> struct.
115 ///
116 /// @return A mutable reference to the <code>PP_Rect</code> struct.
[email protected]1758e882010-11-01 16:16:50117 PP_Rect& pp_rect() {
118 return rect_;
119 }
120
[email protected]100b1682011-06-07 23:05:50121
122 /// Getter function for returning the value of x.
[email protected]b15cce52011-07-06 20:44:31123 ///
124 /// @return The value of x for this <code>Point</code>.
[email protected]1758e882010-11-01 16:16:50125 int32_t x() const {
126 return rect_.point.x;
127 }
[email protected]100b1682011-06-07 23:05:50128
129 /// Setter function for setting the value of x.
[email protected]b15cce52011-07-06 20:44:31130 ///
[email protected]100b1682011-06-07 23:05:50131 /// @param[in] in_x A new x value.
[email protected]1758e882010-11-01 16:16:50132 void set_x(int32_t in_x) {
133 rect_.point.x = in_x;
134 }
135
[email protected]100b1682011-06-07 23:05:50136 /// Getter function for returning the value of y.
[email protected]b15cce52011-07-06 20:44:31137 ///
138 /// @return The value of y for this <code>Point</code>.
[email protected]1758e882010-11-01 16:16:50139 int32_t y() const {
140 return rect_.point.y;
141 }
[email protected]100b1682011-06-07 23:05:50142
143 /// Setter function for setting the value of y.
[email protected]b15cce52011-07-06 20:44:31144 ///
[email protected]100b1682011-06-07 23:05:50145 /// @param[in] in_y A new y value.
[email protected]1758e882010-11-01 16:16:50146 void set_y(int32_t in_y) {
147 rect_.point.y = in_y;
148 }
149
[email protected]100b1682011-06-07 23:05:50150 /// Getter function for returning the value of width.
[email protected]b15cce52011-07-06 20:44:31151 ///
152 /// @return The value of width for this <code>Rect</code>.
[email protected]1758e882010-11-01 16:16:50153 int32_t width() const {
154 return rect_.size.width;
155 }
[email protected]100b1682011-06-07 23:05:50156
157 /// Setter function for setting the value of width.
[email protected]b15cce52011-07-06 20:44:31158 ///
[email protected]100b1682011-06-07 23:05:50159 /// @param[in] w A new width value.
[email protected]1758e882010-11-01 16:16:50160 void set_width(int32_t w) {
161 if (w < 0) {
162 PP_DCHECK(w >= 0);
163 w = 0;
164 }
165 rect_.size.width = w;
166 }
167
[email protected]100b1682011-06-07 23:05:50168 /// Getter function for returning the value of height.
[email protected]b15cce52011-07-06 20:44:31169 ///
170 /// @return The value of height for this <code>Rect</code>.
[email protected]1758e882010-11-01 16:16:50171 int32_t height() const {
172 return rect_.size.height;
173 }
[email protected]100b1682011-06-07 23:05:50174
175 /// Setter function for setting the value of height.
[email protected]b15cce52011-07-06 20:44:31176 ///
[email protected]100b1682011-06-07 23:05:50177 /// @param[in] h A new width height.
[email protected]1758e882010-11-01 16:16:50178 void set_height(int32_t h) {
179 if (h < 0) {
180 PP_DCHECK(h >= 0);
181 h = 0;
182 }
183 rect_.size.height = h;
184 }
185
[email protected]b15cce52011-07-06 20:44:31186 /// Getter function for returning the <code>Point</code>.
187 ///
188 /// @return A <code>Point</code>.
[email protected]1758e882010-11-01 16:16:50189 Point point() const {
190 return Point(rect_.point);
191 }
[email protected]100b1682011-06-07 23:05:50192
[email protected]b15cce52011-07-06 20:44:31193 /// Setter function for setting the value of the <code>Point</code>.
194 ///
195 /// @param[in] origin A <code>Point</code> representing the upper-left
[email protected]100b1682011-06-07 23:05:50196 /// starting coordinate.
[email protected]1758e882010-11-01 16:16:50197 void set_point(const Point& origin) {
198 rect_.point = origin;
199 }
200
[email protected]b15cce52011-07-06 20:44:31201 /// Getter function for returning the <code>Size</code>.
202 ///
[email protected]100b1682011-06-07 23:05:50203 /// @return The size of the rectangle.
[email protected]1758e882010-11-01 16:16:50204 Size size() const {
205 return Size(rect_.size);
206 }
[email protected]100b1682011-06-07 23:05:50207
[email protected]b15cce52011-07-06 20:44:31208 /// Setter function for setting the <code>Size</code>.
209 ///
210 /// @param[in] s A pointer to a <code>Size</code> representing the height
211 /// and width.
[email protected]1758e882010-11-01 16:16:50212 void set_size(const Size& s) {
213 rect_.size.width = s.width();
214 rect_.size.height = s.height();
215 }
216
[email protected]100b1682011-06-07 23:05:50217 /// Getter function to get the upper-bound for the x-coordinates of the
218 /// rectangle. Note that this coordinate value is one past the highest x
219 /// value of pixels in the rectangle. This loop will access all the pixels
220 /// in a horizontal line in the rectangle:
221 /// <code>for (int32_t x = rect.x(); x < rect.right(); ++x) {}</code>
222 ///
223 /// @return The value of x + width for this point.
[email protected]1758e882010-11-01 16:16:50224 int32_t right() const {
225 return x() + width();
226 }
[email protected]100b1682011-06-07 23:05:50227
228 /// Getter function to get the upper-bound for the y-coordinates of the
229 /// rectangle. Note that this coordinate value is one past the highest xy
230 /// value of pixels in the rectangle. This loop will access all the pixels
231 /// in a horizontal line in the rectangle:
232 /// <code>for (int32_t y = rect.y(); y < rect.bottom(); ++y) {}</code>
233 ///
234 /// @return The value of y + height for this point.
[email protected]1758e882010-11-01 16:16:50235 int32_t bottom() const {
236 return y() + height();
237 }
238
[email protected]b15cce52011-07-06 20:44:31239 /// Setter function for setting the value of the <code>Rect</code>.
240 ///
[email protected]100b1682011-06-07 23:05:50241 /// @param[in] x A new x value.
242 /// @param[in] y A new y value.
243 /// @param[in] w A new width value.
244 /// @param[in] h A new height value.
[email protected]1758e882010-11-01 16:16:50245 void SetRect(int32_t x, int32_t y, int32_t w, int32_t h) {
246 set_x(x);
247 set_y(y);
248 set_width(w);
249 set_height(h);
250 }
[email protected]100b1682011-06-07 23:05:50251
[email protected]b15cce52011-07-06 20:44:31252 /// Setter function for setting the value of the <code>Rect</code>.
253 ///
254 /// @param[in] rect A pointer to a <code>PP_Rect</code>.
[email protected]1758e882010-11-01 16:16:50255 void SetRect(const PP_Rect& rect) {
256 rect_ = rect;
257 }
258
[email protected]b15cce52011-07-06 20:44:31259 /// Inset() shrinks the rectangle by a horizontal and vertical
[email protected]100b1682011-06-07 23:05:50260 /// distance on all sides.
[email protected]b15cce52011-07-06 20:44:31261 ///
[email protected]100b1682011-06-07 23:05:50262 /// @param[in] horizontal An int32_t value representing a horizontal
263 /// shrinking distance.
264 /// @param[in] vertical An int32_t value representing a vertical
265 /// shrinking distance.
[email protected]1758e882010-11-01 16:16:50266 void Inset(int32_t horizontal, int32_t vertical) {
267 Inset(horizontal, vertical, horizontal, vertical);
268 }
269
[email protected]b15cce52011-07-06 20:44:31270 /// Inset() shrinks the rectangle by the specified amount on each
[email protected]100b1682011-06-07 23:05:50271 /// side.
[email protected]b15cce52011-07-06 20:44:31272 ///
[email protected]100b1682011-06-07 23:05:50273 /// @param[in] left An int32_t value representing a left
274 /// shrinking distance.
275 /// @param[in] top An int32_t value representing a top
276 /// shrinking distance.
277 /// @param[in] right An int32_t value representing a right
278 /// shrinking distance.
279 /// @param[in] bottom An int32_t value representing a bottom
280 /// shrinking distance.
[email protected]1758e882010-11-01 16:16:50281 void Inset(int32_t left, int32_t top, int32_t right, int32_t bottom);
282
[email protected]b15cce52011-07-06 20:44:31283 /// Offset() moves the rectangle by a horizontal and vertical distance.
284 ///
[email protected]935d00fd2013-03-29 22:26:15285 /// @param[in] horizontal An int32_t value representing a horizontal
[email protected]100b1682011-06-07 23:05:50286 /// move distance.
287 /// @param[in] vertical An int32_t value representing a vertical
288 /// move distance.
[email protected]1758e882010-11-01 16:16:50289 void Offset(int32_t horizontal, int32_t vertical);
[email protected]100b1682011-06-07 23:05:50290
[email protected]b15cce52011-07-06 20:44:31291 /// Offset() moves the rectangle by a horizontal and vertical distance.
292 ///
293 /// @param[in] point A pointer to a <code>Point</code> representing the
294 /// horizontal and vertical move distances.
[email protected]1758e882010-11-01 16:16:50295 void Offset(const Point& point) {
296 Offset(point.x(), point.y());
297 }
298
[email protected]b15cce52011-07-06 20:44:31299 /// IsEmpty() determines if the area of a rectangle is zero. Returns true if
300 /// the area of the rectangle is zero.
301 ///
[email protected]63e627d2011-08-16 19:15:31302 /// @return true if the area of the rectangle is zero.
[email protected]1758e882010-11-01 16:16:50303 bool IsEmpty() const {
304 return rect_.size.width == 0 && rect_.size.height == 0;
305 }
306
[email protected]b15cce52011-07-06 20:44:31307 /// Contains() determines if the point identified by point_x and point_y
308 /// falls inside this rectangle. The point (x, y) is inside the rectangle,
309 /// but the point (x + width, y + height) is not.
310 ///
[email protected]100b1682011-06-07 23:05:50311 /// @param[in] point_x An int32_t value representing a x value.
312 /// @param[in] point_y An int32_t value representing a y value.
[email protected]b15cce52011-07-06 20:44:31313 ///
[email protected]63e627d2011-08-16 19:15:31314 /// @return true if the point_x and point_y fall inside the rectangle.
[email protected]1758e882010-11-01 16:16:50315 bool Contains(int32_t point_x, int32_t point_y) const;
316
[email protected]b15cce52011-07-06 20:44:31317 /// Contains() determines if the specified point is contained by this
[email protected]100b1682011-06-07 23:05:50318 /// rectangle.
[email protected]b15cce52011-07-06 20:44:31319 ///
[email protected]100b1682011-06-07 23:05:50320 /// @param[in] point A pointer to a Point representing a 2D coordinate.
[email protected]b15cce52011-07-06 20:44:31321 ///
[email protected]63e627d2011-08-16 19:15:31322 /// @return true if the point_x and point_y fall inside the rectangle.
[email protected]1758e882010-11-01 16:16:50323 bool Contains(const Point& point) const {
324 return Contains(point.x(), point.y());
325 }
326
[email protected]b15cce52011-07-06 20:44:31327 /// Contains() determines if this rectangle contains the specified rectangle.
328 ///
329 /// @param[in] rect A pointer to a <code>Rect</code>.
330 ///
[email protected]63e627d2011-08-16 19:15:31331 /// @return true if the rectangle fall inside this rectangle.
[email protected]1758e882010-11-01 16:16:50332 bool Contains(const Rect& rect) const;
333
[email protected]935d00fd2013-03-29 22:26:15334 /// Intersects() determines if this rectangle intersects the specified
[email protected]100b1682011-06-07 23:05:50335 /// rectangle.
[email protected]b15cce52011-07-06 20:44:31336 ///
337 /// @param[in] rect A pointer to a <code>Rect</code>.
338 ///
[email protected]63e627d2011-08-16 19:15:31339 /// @return true if the rectangle intersects this rectangle.
[email protected]1758e882010-11-01 16:16:50340 bool Intersects(const Rect& rect) const;
341
[email protected]b15cce52011-07-06 20:44:31342 /// Intersect() computes the intersection of this rectangle with the given
343 /// rectangle.
344 ///
345 /// @param[in] rect A pointer to a <code>Rect</code>.
346 ///
347 /// @return A <code>Rect</code> representing the intersection.
[email protected]1758e882010-11-01 16:16:50348 Rect Intersect(const Rect& rect) const;
349
[email protected]b15cce52011-07-06 20:44:31350 /// Union() computes the union of this rectangle with the given rectangle.
351 /// The union is the smallest rectangle containing both rectangles.
352 ///
353 /// @param[in] rect A pointer to a <code>Rect</code>.
354 ///
355 /// @return A <code>Rect</code> representing the union.
[email protected]1758e882010-11-01 16:16:50356 Rect Union(const Rect& rect) const;
357
[email protected]b15cce52011-07-06 20:44:31358 /// Subtract() computes the rectangle resulting from subtracting
359 /// <code>rect</code> from this Rect. If <code>rect</code>does not intersect
360 /// completely in either the x or y direction, then <code>*this</code> is
[email protected]80426e1b2011-08-09 18:56:16361 /// returned. If <code>rect</code> contains <code>this</code>, then an empty
[email protected]b15cce52011-07-06 20:44:31362 /// <code>Rect</code> is returned.
363 ///
364 /// @param[in] rect A pointer to a <code>Rect</code>.
365 ///
366 /// @return A <code>Rect</code> representing the subtraction.
[email protected]1758e882010-11-01 16:16:50367 Rect Subtract(const Rect& rect) const;
368
[email protected]b15cce52011-07-06 20:44:31369 /// AdjustToFit() fits as much of the receiving rectangle within
370 /// the supplied rectangle as possible, returning the result. For example,
371 /// if the receiver had a x-location of 2 and a width of 4, and the supplied
[email protected]100b1682011-06-07 23:05:50372 /// rectangle had an x-location of 0 with a width of 5, the returned
373 /// rectangle would have an x-location of 1 with a width of 4.
[email protected]b15cce52011-07-06 20:44:31374 ///
375 /// @param[in] rect A pointer to a <code>Rect</code>.
376 ///
377 /// @return A <code>Rect</code> representing the difference between this
378 /// rectangle and the receiving rectangle.
[email protected]1758e882010-11-01 16:16:50379 Rect AdjustToFit(const Rect& rect) const;
380
[email protected]b15cce52011-07-06 20:44:31381 /// CenterPoint() determines the center of this rectangle.
382 ///
383 /// @return A <code>Point</code> representing the center of this rectangle.
[email protected]1758e882010-11-01 16:16:50384 Point CenterPoint() const;
385
[email protected]b15cce52011-07-06 20:44:31386 /// SharesEdgeWith() determines if this rectangle shares an entire edge
[email protected]100b1682011-06-07 23:05:50387 /// (same width or same height) with the given rectangle, and the
388 /// rectangles do not overlap.
[email protected]b15cce52011-07-06 20:44:31389 ///
390 /// @param[in] rect A pointer to a <code>Rect</code>.
391 ///
392 /// @return true if this rectangle and supplied rectangle share an edge.
[email protected]1758e882010-11-01 16:16:50393 bool SharesEdgeWith(const Rect& rect) const;
394
395 private:
396 PP_Rect rect_;
397};
398
399} // namespace pp
400
[email protected]b15cce52011-07-06 20:44:31401/// This function determines whether the x, y, width, and height values of two
402/// rectangles and are equal.
403///
404/// @param[in] lhs The <code>Rect</code> on the left-hand side of the equation.
405/// @param[in] rhs The <code>Rect</code> on the right-hand side of the equation.
406///
[email protected]100b1682011-06-07 23:05:50407/// @return true if they are equal, false if unequal.
[email protected]1758e882010-11-01 16:16:50408inline bool operator==(const pp::Rect& lhs, const pp::Rect& rhs) {
409 return lhs.x() == rhs.x() &&
410 lhs.y() == rhs.y() &&
411 lhs.width() == rhs.width() &&
412 lhs.height() == rhs.height();
413}
414
[email protected]b15cce52011-07-06 20:44:31415/// This function determines whether two Rects are not equal.
416///
417/// @param[in] lhs The <code>Rect</code> on the left-hand side of the equation.
418/// @param[in] rhs The <code>Rect</code> on the right-hand side of the
419/// equation.
420///
[email protected]100b1682011-06-07 23:05:50421/// @return true if the given Rects are equal, otherwise false.
[email protected]1758e882010-11-01 16:16:50422inline bool operator!=(const pp::Rect& lhs, const pp::Rect& rhs) {
423 return !(lhs == rhs);
424}
425
426#endif
427