CUGL
Cornell University Game Library
CURect.h
1 //
2 // CURect.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a 2d bouding rectangle. This rectangle
6 // is not intended for drawing. Use CUPoly2 instead for rectangle graphics.
7 //
8 // Because math objects are intended to be on the stack, we do not provide
9 // any shared pointer support in this class.
10 //
11 // CUGL zlib License:
12 // This software is provided 'as-is', without any express or implied
13 // warranty. In no event will the authors be held liable for any damages
14 // arising from the use of this software.
15 //
16 // Permission is granted to anyone to use this software for any purpose,
17 // including commercial applications, and to alter it and redistribute it
18 // freely, subject to the following restrictions:
19 //
20 // 1. The origin of this software must not be misrepresented; you must not
21 // claim that you wrote the original software. If you use this software
22 // in a product, an acknowledgment in the product documentation would be
23 // appreciated but is not required.
24 //
25 // 2. Altered source versions must be plainly marked as such, and must not
26 // be misrepresented as being the original software.
27 //
28 // 3. This notice may not be removed or altered from any source distribution.
29 //
30 // Author: Walker White
31 // Version: 5/30/16
32 #ifndef __CU_RECT_H__
33 #define __CU_RECT_H__
34 
35 #include "CUSize.h"
36 #include "CUVec2.h"
37 
38 namespace cugl {
39 
45 class Rect {
46 #pragma mark Values
47 public:
52 
54  static const Rect ZERO;
56  static const Rect UNIT;
57 
58 
59 #pragma mark -
60 #pragma mark Constructors
61 public:
65  Rect() : Rect(Vec2::ZERO,Size::ZERO) {}
66 
75  Rect(float x, float y, float width, float height) :
76  Rect(Vec2(x,y),Size(width,height)) {}
77 
83  Rect(float* array) : Rect(Vec2(array),Size(&array[2])) {}
84 
85 
92  Rect(const Vec2& pos, const Size& dimen) {
93  origin = pos; size = dimen;
94  }
95 
101  Rect(const Rect& other) {
102  origin = other.origin; size = other.size;
103  }
104 
105 
106 #pragma mark -
107 #pragma mark Setters
108 
115  Rect& operator= (const Rect& other) {
116  return set(other);
117  }
118 
126  Rect& operator=(const float* array) {
127  return set(array);
128  }
129 
140  Rect& set(float x, float y, float width, float height) {
141  origin.set(x,y); size.set(width,height);
142  return *this;
143  }
144 
152  Rect& set(const float* array) {
153  origin = &array[0]; size = &array[2];
154  return *this;
155  }
164  Rect& set(const Vec2& pos, const Size& dimen) {
165  origin.set(pos); size.set(dimen);
166  return *this;
167  }
168 
176  Rect& set(const Rect& other) {
177  origin.set(other.origin); size.set(other.size);
178  return *this;
179  }
180 
181 
182 #pragma mark -
183 #pragma mark Accessors
184 
190  float getMinX() const { return (size.width < 0 ? origin.x+size.width : origin.x); }
191 
197  float getMidX() const { return origin.x + size.width / 2.0f; }
198 
204  float getMaxX() const { return (size.width < 0 ? origin.x : origin.x + size.width); }
205 
211  float getMinY() const { return (size.height < 0 ? origin.y+size.height : origin.y); }
212 
218  float getMidY() const { return origin.y + size.height / 2.0f; }
219 
225  float getMaxY() const { return (size.height < 0 ? origin.y : origin.y + size.height); }
226 
232  float isDegenerate() const { return size.width <= 0 || size.height <= 0; }
233 
234 
235 #pragma mark -
236 #pragma mark Comparisons
237 
246  bool operator==(const Rect& rect) const {
247  return origin == rect.origin && size == rect.size;
248  }
249 
259  bool operator!=(const Rect& rect) const {
260  return origin != rect.origin || size != rect.size;
261  }
262 
273  bool equals(const Rect& rect, float variance=CU_MATH_EPSILON) const {
274  return origin.equals(rect.origin,variance) && size.equals(rect.size,variance);
275  }
276 
287  bool operator<=(const Rect& rect) const {
288  return inside(rect);
289  }
290 
303  bool operator<(const Rect& rect) const;
304 
315  bool operator>=(const Rect& rect) const {
316  return contains(rect);
317  }
318 
331  bool operator>(const Rect& rect) const;
332 
343  bool inside(const Rect& rect) const;
344 
355  bool contains(const Rect& rect) const;
356 
367  bool touches(const Vec2& point) const;
368 
380  bool contains(const Vec2& point) const;
381 
393  bool contains(const Vec2& center, float radius) const;
394 
405  bool doesIntersect(const Rect& rect) const;
406 
418  bool doesIntersect(const Vec2& center, float radius) const;
419 
420 
421 #pragma mark -
422 #pragma mark Rectangle Arithmetic
423 
430  Rect& merge(const Rect & rect);
431 
441  Rect& intersect(const Rect & rect);
442 
454  Rect& expand(float factor);
455 
465  Rect& expand(const Vec2& point);
466 
476  Rect getMerge(const Rect & rect) {
477  Rect result(*this);
478  return result.merge(rect);
479  }
480 
492  Rect getIntersection(const Rect & rect) {
493  Rect result(*this);
494  return result.intersect(rect);
495  }
496 
510  Rect getExpansion(float factor) {
511  Rect result(*this);
512  return result.expand(factor);
513  }
514 
525  Rect getExpansion(const Vec2& point) {
526  Rect result(*this);
527  return result.expand(point);
528  }
529 
530 
531 
532 };
533 
534 }
535 
536 #endif /* __CU_RECT_H__ */
Definition: CUSize.h:57
bool contains(const Rect &rect) const
Rect getMerge(const Rect &rect)
Definition: CURect.h:476
Rect getExpansion(const Vec2 &point)
Definition: CURect.h:525
static const Rect ZERO
Definition: CURect.h:54
float x
Definition: CUVec2.h:66
Rect & set(const float *array)
Definition: CURect.h:152
Rect & operator=(const float *array)
Definition: CURect.h:126
Rect(const Rect &other)
Definition: CURect.h:101
Size & set(float width, float height)
Definition: CUSize.h:143
float y
Definition: CUVec2.h:68
Definition: CUVec2.h:61
bool operator==(const Rect &rect) const
Definition: CURect.h:246
Rect(float x, float y, float width, float height)
Definition: CURect.h:75
Rect & set(const Vec2 &pos, const Size &dimen)
Definition: CURect.h:164
float getMinX() const
Definition: CURect.h:190
Rect & operator=(const Rect &other)
Definition: CURect.h:115
float getMidY() const
Definition: CURect.h:218
bool operator<=(const Rect &rect) const
Definition: CURect.h:287
float getMaxY() const
Definition: CURect.h:225
float width
Definition: CUSize.h:61
static const Rect UNIT
Definition: CURect.h:56
bool operator>=(const Rect &rect) const
Definition: CURect.h:315
Rect & expand(float factor)
Rect()
Definition: CURect.h:65
float height
Definition: CUSize.h:63
bool equals(const Rect &rect, float variance=CU_MATH_EPSILON) const
Definition: CURect.h:273
Rect & merge(const Rect &rect)
bool operator!=(const Rect &rect) const
Definition: CURect.h:259
float getMinY() const
Definition: CURect.h:211
Definition: CURect.h:45
Vec2 origin
Definition: CURect.h:49
float getMaxX() const
Definition: CURect.h:204
bool operator<(const Rect &rect) const
float isDegenerate() const
Definition: CURect.h:232
Rect(const Vec2 &pos, const Size &dimen)
Definition: CURect.h:92
bool equals(const Vec2 &v, float variance=CU_MATH_EPSILON) const
Definition: CUVec2.h:890
Rect & set(const Rect &other)
Definition: CURect.h:176
Size size
Definition: CURect.h:51
Rect & intersect(const Rect &rect)
bool touches(const Vec2 &point) const
Vec2 & set(float x, float y)
Definition: CUVec2.h:176
Rect getExpansion(float factor)
Definition: CURect.h:510
Definition: CUAnimationNode.h:52
Rect(float *array)
Definition: CURect.h:83
float getMidX() const
Definition: CURect.h:197
bool inside(const Rect &rect) const
bool doesIntersect(const Rect &rect) const
bool equals(const Size &other, float variance=CU_MATH_EPSILON) const
Definition: CUSize.h:321
Rect & set(float x, float y, float width, float height)
Definition: CURect.h:140
bool operator>(const Rect &rect) const
Rect getIntersection(const Rect &rect)
Definition: CURect.h:492