CUGL 1.3
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 MIT 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 
96 
97 #pragma mark -
98 #pragma mark Setters
99 
106  Rect& operator=(const float* array) {
107  return set(array);
108  }
109 
120  Rect& set(float x, float y, float width, float height) {
121  origin.set(x,y); size.set(width,height);
122  return *this;
123  }
124 
132  Rect& set(const float* array) {
133  origin = &array[0]; size = &array[2];
134  return *this;
135  }
144  Rect& set(const Vec2& pos, const Size& dimen) {
145  origin.set(pos); size.set(dimen);
146  return *this;
147  }
148 
156  Rect& set(const Rect& other) {
157  origin.set(other.origin); size.set(other.size);
158  return *this;
159  }
160 
161 
162 #pragma mark -
163 #pragma mark Accessors
164 
170  float getMinX() const { return (size.width < 0 ? origin.x+size.width : origin.x); }
171 
177  float getMidX() const { return origin.x + size.width / 2.0f; }
178 
184  float getMaxX() const { return (size.width < 0 ? origin.x : origin.x + size.width); }
185 
191  float getMinY() const { return (size.height < 0 ? origin.y+size.height : origin.y); }
192 
198  float getMidY() const { return origin.y + size.height / 2.0f; }
199 
205  float getMaxY() const { return (size.height < 0 ? origin.y : origin.y + size.height); }
206 
212  float isDegenerate() const { return size.width <= 0 || size.height <= 0; }
213 
214 
215 #pragma mark -
216 #pragma mark Comparisons
217 
226  bool operator==(const Rect& rect) const {
227  return origin == rect.origin && size == rect.size;
228  }
229 
239  bool operator!=(const Rect& rect) const {
240  return origin != rect.origin || size != rect.size;
241  }
242 
253  bool equals(const Rect& rect, float variance=CU_MATH_EPSILON) const {
254  return origin.equals(rect.origin,variance) && size.equals(rect.size,variance);
255  }
256 
267  bool operator<=(const Rect& rect) const {
268  return inside(rect);
269  }
270 
283  bool operator<(const Rect& rect) const;
284 
295  bool operator>=(const Rect& rect) const {
296  return contains(rect);
297  }
298 
311  bool operator>(const Rect& rect) const;
312 
323  bool inside(const Rect& rect) const;
324 
335  bool contains(const Rect& rect) const;
336 
347  bool touches(const Vec2& point) const;
348 
360  bool contains(const Vec2& point) const;
361 
373  bool contains(const Vec2& center, float radius) const;
374 
385  bool doesIntersect(const Rect& rect) const;
386 
398  bool doesIntersect(const Vec2& center, float radius) const;
399 
400 
401 #pragma mark -
402 #pragma mark Rectangle Arithmetic
403 
410  Rect& merge(const Rect & rect);
411 
421  Rect& intersect(const Rect & rect);
422 
434  Rect& expand(float factor);
435 
445  Rect& expand(const Vec2& point);
446 
456  Rect getMerge(const Rect & rect) const {
457  Rect result(*this);
458  return result.merge(rect);
459  }
460 
472  Rect getIntersection(const Rect & rect) const {
473  Rect result(*this);
474  return result.intersect(rect);
475  }
476 
490  Rect getExpansion(float factor) const {
491  Rect result(*this);
492  return result.expand(factor);
493  }
494 
505  Rect getExpansion(const Vec2& point) const {
506  Rect result(*this);
507  return result.expand(point);
508  }
509 
510 
511 
512 };
513 
514 }
515 
516 #endif /* __CU_RECT_H__ */
cugl::Rect::getExpansion
Rect getExpansion(float factor) const
Definition: CURect.h:490
cugl::Rect::Rect
Rect()
Definition: CURect.h:65
cugl::Rect::operator<
bool operator<(const Rect &rect) const
cugl::Rect::equals
bool equals(const Rect &rect, float variance=CU_MATH_EPSILON) const
Definition: CURect.h:253
cugl::Rect::Rect
Rect(float *array)
Definition: CURect.h:83
cugl::Rect::operator=
Rect & operator=(const float *array)
Definition: CURect.h:106
cugl::Rect::getMidX
float getMidX() const
Definition: CURect.h:177
cugl::Rect::operator>=
bool operator>=(const Rect &rect) const
Definition: CURect.h:295
cugl::Size::set
Size & set(float width, float height)
Definition: CUSize.h:123
cugl::Rect::touches
bool touches(const Vec2 &point) const
cugl::Rect::intersect
Rect & intersect(const Rect &rect)
cugl::Size
Definition: CUSize.h:57
cugl::Size::width
float width
Definition: CUSize.h:61
cugl::Rect::Rect
Rect(const Vec2 &pos, const Size &dimen)
Definition: CURect.h:92
cugl::Rect::operator==
bool operator==(const Rect &rect) const
Definition: CURect.h:226
cugl::Rect::expand
Rect & expand(float factor)
cugl::Rect::contains
bool contains(const Rect &rect) const
cugl::Vec2::x
float x
Definition: CUVec2.h:66
cugl::Rect
Definition: CURect.h:45
cugl::Rect::getIntersection
Rect getIntersection(const Rect &rect) const
Definition: CURect.h:472
cugl::Vec2::equals
bool equals(const Vec2 &v, float variance=CU_MATH_EPSILON) const
Definition: CUVec2.h:867
cugl::Rect::set
Rect & set(float x, float y, float width, float height)
Definition: CURect.h:120
cugl::Rect::origin
Vec2 origin
Definition: CURect.h:49
cugl::Rect::getMaxY
float getMaxY() const
Definition: CURect.h:205
cugl::Rect::Rect
Rect(float x, float y, float width, float height)
Definition: CURect.h:75
cugl::Vec2::set
Vec2 & set(float x, float y)
Definition: CUVec2.h:153
cugl::Rect::UNIT
static const Rect UNIT
Definition: CURect.h:56
cugl::Vec2::y
float y
Definition: CUVec2.h:68
cugl::Rect::operator>
bool operator>(const Rect &rect) const
cugl::Rect::set
Rect & set(const Vec2 &pos, const Size &dimen)
Definition: CURect.h:144
cugl::Rect::getMinY
float getMinY() const
Definition: CURect.h:191
cugl::Vec2
Definition: CUVec2.h:61
cugl::Rect::doesIntersect
bool doesIntersect(const Rect &rect) const
cugl::Rect::inside
bool inside(const Rect &rect) const
cugl::Rect::operator<=
bool operator<=(const Rect &rect) const
Definition: CURect.h:267
cugl::Rect::merge
Rect & merge(const Rect &rect)
cugl::Rect::set
Rect & set(const Rect &other)
Definition: CURect.h:156
cugl::Rect::set
Rect & set(const float *array)
Definition: CURect.h:132
cugl::Rect::getExpansion
Rect getExpansion(const Vec2 &point) const
Definition: CURect.h:505
cugl::Rect::getMidY
float getMidY() const
Definition: CURect.h:198
cugl::Size::equals
bool equals(const Size &other, float variance=CU_MATH_EPSILON) const
Definition: CUSize.h:301
cugl::Rect::operator!=
bool operator!=(const Rect &rect) const
Definition: CURect.h:239
cugl::Rect::getMerge
Rect getMerge(const Rect &rect) const
Definition: CURect.h:456
cugl::Rect::size
Size size
Definition: CURect.h:51
cugl::Rect::isDegenerate
float isDegenerate() const
Definition: CURect.h:212
cugl::Size::height
float height
Definition: CUSize.h:63
cugl::Rect::ZERO
static const Rect ZERO
Definition: CURect.h:54
cugl::Rect::getMinX
float getMinX() const
Definition: CURect.h:170
cugl::Rect::getMaxX
float getMaxX() const
Definition: CURect.h:184