CUGL
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 zlib 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 
104  Size(const Size& other) {
105  width = other.width; height = other.height;
106  }
107 
108 
109 #pragma mark -
110 #pragma mark Setters
111 
118  Size& operator= (const Size& other) {
119  return set(other);
120  }
121 
131  Size& operator= (const float* array) {
132  return set(array);
133  }
134 
143  Size& set(float width, float height) {
144  this->width = width; this->height = height;
145  return *this;
146  }
147 
157  Size& set(const float* array) {
158  this->width = array[0]; this->height = array[1];
159  return *this;
160  }
161 
169  Size& set(const Size& other) {
170  this->width = other.width; this->height = other.height;
171  return *this;
172  }
173 
174 
175 #pragma mark -
176 #pragma mark Integer Access
177 
185  int getIWidth() const { return (int)ceilf(width); }
186 
194  int getIHeight() const { return (int)ceilf(height); }
195 
196 
197 #pragma mark -
198 #pragma mark Comparisons
199 
209  bool operator<(const Size& v) const {
210  return (width == v.width ? height < v.height : width < v.width);
211  }
212 
223  bool operator<=(const Size& v) const {
224  return (width == v.width ? height <= v.height : width <= v.width);
225  }
226 
237  bool operator>(const Size& v) const {
238  return (width == v.width ? height > v.height : width > v.width);
239  }
240 
251  bool operator>=(const Size& v) const {
252  return (width == v.width ? height >= v.height : width >= v.width);
253  }
254 
265  bool operator==(const Size& other) const {
266  return width == other.width && height == other.height;
267  }
268 
279  bool operator!=(const Size& other) const {
280  return width != other.width || height != other.height;
281  }
282 
293  bool inside(const Size& other) const {
294  return width <= other.width && height <= other.height;
295  }
296 
307  bool contains(const Size& other) const {
308  return width >= other.width && height >= other.height;
309  }
310 
321  bool equals(const Size& other, float variance=CU_MATH_EPSILON) const {
322  float dx = width-other.width;
323  float dy = height-other.height;
324  return ((-variance < dx && dx < variance) &&
325  (-variance < dy && dy < variance));
326  }
327 
328 
329 #pragma mark -
330 #pragma mark Operators
331 
339  Size& operator+=(const Size& right) {
340  width += right.width; height += right.height;
341  return *this;
342  }
343 
351  Size& operator-=(const Size& right) {
352  width -= right.width; height -= right.height;
353  return *this;
354  }
355 
363  Size& operator*=(float a) {
364  width *= a; height *= a;
365  return *this;
366  }
367 
375  Size& operator*=(const Size& right) {
376  width *= right.width; height *= right.height;
377  return *this;
378  }
379 
387  Size& operator/=(float a);
388 
398  Size& operator/=(const Size& right);
399 
409  Size operator+(const Size& right) const { return Size(*this) += right; }
410 
420  Size operator-(const Size& right) const { return Size(*this) -= right; }
421 
431  Size operator*(float scalar) const { return Size(*this) *= scalar; }
432 
443  Size operator*(const Size& right) const { return Size(*this) *= right; }
444 
454  Size operator/(float scalar) const { return Size(*this) /= scalar; }
455 
466  Size operator/(const Size& right) const { return Size(*this) /= right; }
467 
468 
469 #pragma mark -
470 #pragma mark Conversion Methods
471 public:
482  std::string toString(bool verbose = false) const;
483 
485  operator std::string() const { return toString(); }
486 
488  operator Vec2() const;
489 
497  explicit Size(const Vec2& point);
498 
505  Size(const Vec2& p1, const Vec2& p2);
506 
515  Size& set(const Vec2& p1, const Vec2& p2);
516 
526  Size& operator= (const Vec2& point);
527 };
528 
529 }
530 
531 #endif /* CU_SIZE_H */
Definition: CUSize.h:57
Size operator+(const Size &right) const
Definition: CUSize.h:409
bool operator==(const Size &other) const
Definition: CUSize.h:265
Size & operator*=(const Size &right)
Definition: CUSize.h:375
Size & set(float width, float height)
Definition: CUSize.h:143
bool operator<(const Size &v) const
Definition: CUSize.h:209
bool operator>=(const Size &v) const
Definition: CUSize.h:251
Size operator/(float scalar) const
Definition: CUSize.h:454
Definition: CUVec2.h:61
Size operator*(float scalar) const
Definition: CUSize.h:431
int getIWidth() const
Definition: CUSize.h:185
bool operator!=(const Size &other) const
Definition: CUSize.h:279
Size & set(const Size &other)
Definition: CUSize.h:169
Size & operator=(const Size &other)
Definition: CUSize.h:118
static const Size ZERO
Definition: CUSize.h:66
Size()
Definition: CUSize.h:77
Size(float width, float height)
Definition: CUSize.h:86
int getIHeight() const
Definition: CUSize.h:194
Size & set(const float *array)
Definition: CUSize.h:157
Size & operator/=(float a)
float width
Definition: CUSize.h:61
bool inside(const Size &other) const
Definition: CUSize.h:293
Size(const Size &other)
Definition: CUSize.h:104
float height
Definition: CUSize.h:63
Size operator*(const Size &right) const
Definition: CUSize.h:443
Size & operator*=(float a)
Definition: CUSize.h:363
bool operator>(const Size &v) const
Definition: CUSize.h:237
Size & operator+=(const Size &right)
Definition: CUSize.h:339
bool contains(const Size &other) const
Definition: CUSize.h:307
Size operator-(const Size &right) const
Definition: CUSize.h:420
Size operator/(const Size &right) const
Definition: CUSize.h:466
std::string toString(bool verbose=false) const
Size(const float *array)
Definition: CUSize.h:97
Definition: CUAnimationNode.h:52
bool operator<=(const Size &v) const
Definition: CUSize.h:223
Size & operator-=(const Size &right)
Definition: CUSize.h:351
bool equals(const Size &other, float variance=CU_MATH_EPSILON) const
Definition: CUSize.h:321