CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUSize.h
1 //
2 // CUSize.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for representing the size of a 2d rectangle.
6 // This is critical for screen dimensions and scene graphs.
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 // This module is based on an original file from Cocos2d: http://cocos2d-x.org
12 // It has been modified to support the CUGL framework.
13 //
14 // CUGL MIT License:
15 // This software is provided 'as-is', without any express or implied
16 // warranty. In no event will the authors be held liable for any damages
17 // arising from the use of this software.
18 //
19 // Permission is granted to anyone to use this software for any purpose,
20 // including commercial applications, and to alter it and redistribute it
21 // freely, subject to the following restrictions:
22 //
23 // 1. The origin of this software must not be misrepresented; you must not
24 // claim that you wrote the original software. If you use this software
25 // in a product, an acknowledgment in the product documentation would be
26 // appreciated but is not required.
27 //
28 // 2. Altered source versions must be plainly marked as such, and must not
29 // be misrepresented as being the original software.
30 //
31 // 3. This notice may not be removed or altered from any source distribution.
32 //
33 // Author: Walker White
34 // Version: 5/30/16
35 
36 #ifndef CU_SIZE_H
37 #define CU_SIZE_H
38 
39 #include <string>
40 #include <math.h>
41 #include "CUMathBase.h"
42 
43 namespace cugl {
44 
46 class Vec2;
47 
57 class Size {
58 #pragma mark Values
59 public:
61  float width;
63  float height;
64 
66  static const Size ZERO;
67 
68 #pragma mark -
69 #pragma mark Constructors
70 public:
71 
77  Size() : width(0), height(0) {}
78 
86  Size(float width, float height) {
87  this->width = width; this->height = height;
88  }
89 
97  Size(const float* array) { width = array[0]; height = array[1]; }
98 
99 
100 #pragma mark -
101 #pragma mark Setters
102 
111  Size& operator= (const float* array) {
112  return set(array);
113  }
114 
123  Size& set(float width, float height) {
124  this->width = width; this->height = height;
125  return *this;
126  }
127 
137  Size& set(const float* array) {
138  this->width = array[0]; this->height = array[1];
139  return *this;
140  }
141 
149  Size& set(const Size& other) {
150  this->width = other.width; this->height = other.height;
151  return *this;
152  }
153 
154 
155 #pragma mark -
156 #pragma mark Integer Access
157 
165  int getIWidth() const { return (int)ceilf(width); }
166 
174  int getIHeight() const { return (int)ceilf(height); }
175 
176 
177 #pragma mark -
178 #pragma mark Comparisons
179 
189  bool operator<(const Size& v) const {
190  return (width == v.width ? height < v.height : width < v.width);
191  }
192 
203  bool operator<=(const Size& v) const {
204  return (width == v.width ? height <= v.height : width <= v.width);
205  }
206 
217  bool operator>(const Size& v) const {
218  return (width == v.width ? height > v.height : width > v.width);
219  }
220 
231  bool operator>=(const Size& v) const {
232  return (width == v.width ? height >= v.height : width >= v.width);
233  }
234 
245  bool operator==(const Size& other) const {
246  return width == other.width && height == other.height;
247  }
248 
259  bool operator!=(const Size& other) const {
260  return width != other.width || height != other.height;
261  }
262 
273  bool inside(const Size& other) const {
274  return width <= other.width && height <= other.height;
275  }
276 
287  bool contains(const Size& other) const {
288  return width >= other.width && height >= other.height;
289  }
290 
301  bool equals(const Size& other, float variance=CU_MATH_EPSILON) const {
302  float dx = width-other.width;
303  float dy = height-other.height;
304  return ((-variance < dx && dx < variance) &&
305  (-variance < dy && dy < variance));
306  }
307 
308 
309 #pragma mark -
310 #pragma mark Operators
311 
319  Size& operator+=(const Size& right) {
320  width += right.width; height += right.height;
321  return *this;
322  }
323 
331  Size& operator-=(const Size& right) {
332  width -= right.width; height -= right.height;
333  return *this;
334  }
335 
343  Size& operator*=(float a) {
344  width *= a; height *= a;
345  return *this;
346  }
347 
355  Size& operator*=(const Size& right) {
356  width *= right.width; height *= right.height;
357  return *this;
358  }
359 
367  Size& operator/=(float a);
368 
378  Size& operator/=(const Size& right);
379 
389  Size operator+(const Size& right) const { return Size(*this) += right; }
390 
400  Size operator-(const Size& right) const { return Size(*this) -= right; }
401 
411  Size operator*(float scalar) const { return Size(*this) *= scalar; }
412 
423  Size operator*(const Size& right) const { return Size(*this) *= right; }
424 
434  Size operator/(float scalar) const { return Size(*this) /= scalar; }
435 
446  Size operator/(const Size& right) const { return Size(*this) /= right; }
447 
448 
449 #pragma mark -
450 #pragma mark Conversion Methods
451 public:
462  std::string toString(bool verbose = false) const;
463 
465  operator std::string() const { return toString(); }
466 
468  operator Vec2() const;
469 
477  explicit Size(const Vec2& point);
478 
485  Size(const Vec2& p1, const Vec2& p2);
486 
495  Size& set(const Vec2& p1, const Vec2& p2);
496 
506  Size& operator= (const Vec2& point);
507 };
508 
509 }
510 
511 #endif /* CU_SIZE_H */
cugl::Size::operator!=
bool operator!=(const Size &other) const
Definition: CUSize.h:259
cugl::Size::set
Size & set(const float *array)
Definition: CUSize.h:137
cugl::Size::operator+=
Size & operator+=(const Size &right)
Definition: CUSize.h:319
cugl::Size::set
Size & set(const Size &other)
Definition: CUSize.h:149
cugl::Size::set
Size & set(float width, float height)
Definition: CUSize.h:123
cugl::Size::operator>=
bool operator>=(const Size &v) const
Definition: CUSize.h:231
cugl::Size::contains
bool contains(const Size &other) const
Definition: CUSize.h:287
cugl::Size::operator+
Size operator+(const Size &right) const
Definition: CUSize.h:389
cugl::Size
Definition: CUSize.h:57
cugl::Size::width
float width
Definition: CUSize.h:61
cugl::Size::operator=
Size & operator=(const float *array)
Definition: CUSize.h:111
cugl::Size::operator*=
Size & operator*=(float a)
Definition: CUSize.h:343
cugl::Size::getIWidth
int getIWidth() const
Definition: CUSize.h:165
cugl::Size::operator>
bool operator>(const Size &v) const
Definition: CUSize.h:217
cugl::Size::operator-=
Size & operator-=(const Size &right)
Definition: CUSize.h:331
cugl::Size::Size
Size()
Definition: CUSize.h:77
cugl::Size::operator/
Size operator/(float scalar) const
Definition: CUSize.h:434
cugl::Size::operator/
Size operator/(const Size &right) const
Definition: CUSize.h:446
cugl::Size::operator/=
Size & operator/=(float a)
cugl::Size::toString
std::string toString(bool verbose=false) const
cugl::Size::getIHeight
int getIHeight() const
Definition: CUSize.h:174
cugl::Size::operator==
bool operator==(const Size &other) const
Definition: CUSize.h:245
cugl::Vec2
Definition: CUVec2.h:61
cugl::Size::operator<=
bool operator<=(const Size &v) const
Definition: CUSize.h:203
cugl::Size::Size
Size(const float *array)
Definition: CUSize.h:97
cugl::Size::operator-
Size operator-(const Size &right) const
Definition: CUSize.h:400
cugl::Size::inside
bool inside(const Size &other) const
Definition: CUSize.h:273
cugl::Size::operator<
bool operator<(const Size &v) const
Definition: CUSize.h:189
cugl::Size::equals
bool equals(const Size &other, float variance=CU_MATH_EPSILON) const
Definition: CUSize.h:301
cugl::Size::Size
Size(float width, float height)
Definition: CUSize.h:86
cugl::Size::ZERO
static const Size ZERO
Definition: CUSize.h:66
cugl::Size::height
float height
Definition: CUSize.h:63
cugl::Size::operator*=
Size & operator*=(const Size &right)
Definition: CUSize.h:355
cugl::Size::operator*
Size operator*(const Size &right) const
Definition: CUSize.h:423
cugl::Size::operator*
Size operator*(float scalar) const
Definition: CUSize.h:411