Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Area.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/Vector.h"
27 
28 #include <iostream>
29 #include <utility>
30 #include <vector>
31 
32 namespace cinder {
33 
34 template<typename T>
35 class RectT;
36 
37 class Area {
38  public:
39  Area() {}
40  Area( const Vec2i &UL, const Vec2i &LR );
41  Area( int32_t aX1, int32_t aY1, int32_t aX2, int32_t aY2 )
42  { set( aX1, aY1, aX2, aY2 ); }
43  explicit Area( const RectT<float> &r );
44 
45  void set( int32_t aX1, int32_t aY1, int32_t aX2, int32_t aY2 );
46 
47  int32_t getWidth() const { return x2 - x1; }
48  int32_t getHeight() const { return y2 - y1; }
49  Vec2i getSize() const { return Vec2i( x2 - x1, y2 - y1 ); }
50  Vec2f getCenter() const { return Vec2f( ( x1 + x2 ) / 2.0f, ( y1 + y2 ) / 2.0f ); }
51  int32_t calcArea() const { return getWidth() * getHeight(); }
52 
53  void clipBy( const Area &clip );
54  Area getClipBy( const Area &clip ) const;
55 
57  void offset( const Vec2i &off );
59  Area getOffset( const Vec2i &off ) const;
61  void moveULTo( const Vec2i &newUL );
63  Area getMoveULTo( const Vec2i &newUL ) const;
65  void expand( int32_t expandX, int32_t expandY ) { x1 -= expandX; x2 += expandX; y1 -= expandY; y2 += expandY; }
66 
67  int32_t getX1() const { return x1; }
68  void setX1( int32_t aX1 ) { x1 = aX1; }
69  int32_t getY1() const { return y1; }
70  void setY1( int32_t aY1 ) { y1 = aY1; }
71  int32_t getX2() const { return x2; }
72  void setX2( int32_t aX2 ) { x2 = aX2; }
73  int32_t getY2() const { return y2; }
74  void setY2( int32_t aY2 ) { y2 = aY2; }
75  Vec2i getUL() const { return Vec2i( x1, y1 ); } // left-top offset
76  Vec2i getLR() const { return Vec2i( x2, y2 ); } // right-bottom offset
77 
78  bool contains( const Vec2i &offset ) const;
79  template<typename Y>
80  bool contains( const Vec2<Y> &offset ) const { return contains( Vec2i( (int32_t)math<Y>::ceil( offset. x ), (int32_t)math<Y>::ceil( offset.y ) ) ); }
81  bool intersects( const Area &area ) const;
82 
84  void include( const Vec2i &point );
86  void include( const std::vector<Vec2i > &points );
88  void include( const Area &area );
89 
91  template<typename Y>
92  float distance( const Vec2<Y> &pt ) const;
94  template<typename Y>
95  float distanceSquared( const Vec2<Y> &pt ) const;
96 
98  template<typename Y>
99  Vec2<Y> closestPoint( const Vec2<Y> &pt ) const;
100 
101  int32_t x1, y1, x2, y2;
102 
103  bool operator==( const Area &aArea ) const { return ( ( x1 == aArea.x1 ) && ( y1 == aArea.y1 ) && ( x2 == aArea.x2 ) && ( y2 == aArea.y2 ) ); }
104  bool operator<( const Area &aArea ) const;
105 
106  const Area operator+( const Vec2i &o ) const { return this->getOffset( o ); }
107  const Area operator-( const Vec2i &o ) const { return this->getOffset( -o ); }
108 
109  const Area operator+( const Area& rhs ) const { return Area( x1 + rhs.x1, y1 + rhs.y1, x2 + rhs.x2, y2 + rhs.y2 ); }
110  const Area operator-( const Area& rhs ) const { return Area( x1 - rhs.x1, y1 - rhs.y1, x2 - rhs.x2, y2 - rhs.y2 ); }
111 
112  Area& operator+=( const Vec2i &o ) { offset( o ); return *this; }
113  Area& operator-=( const Vec2i &o ) { offset( -o ); return *this; }
114 
116  static Area zero() { return Area( 0, 0, 0, 0 ); }
117  static Area proportionalFit( const Area &srcArea, const Area &dstArea, bool center, bool expand = false );
118 
119  friend std::ostream& operator<<( std::ostream &o, const Area &area )
120  {
121  return o << "(" << area.x1 << ", " << area.y1 << ")-(" << area.x2 << ", " << area.y2 << ")";
122  }
123 };
124 
125 extern std::pair<Area,Vec2i> clippedSrcDst( const Area &srcSurfaceBounds, const Area &srcArea, const Area &dstSurfaceBounds, const Vec2i &dstLT );
126 
127 } // namespace cinder
void include(const Vec2i &point)
Expands the Area to include point in its interior.
Definition: Area.cpp:125
GLdouble GLdouble GLdouble r
Definition: GLee.h:1474
int32_t getY1() const
Definition: Area.h:69
bool intersects(const Area &area) const
Definition: Area.cpp:117
Definition: CinderMath.h:40
void setX2(int32_t aX2)
Definition: Area.h:72
void expand(int32_t expandX, int32_t expandY)
Expands the Area by expandX horizontally and expandY vertically. expandX is subtracted from x1 and ad...
Definition: Area.h:65
Vec2< float > Vec2f
Definition: Vector.h:1314
int32_t getY2() const
Definition: Area.h:73
Definition: Area.h:37
int32_t getX2() const
Definition: Area.h:71
int32_t y2
Definition: Area.h:101
Area(int32_t aX1, int32_t aY1, int32_t aX2, int32_t aY2)
Definition: Area.h:41
bool contains(const Vec2i &offset) const
Definition: Area.cpp:112
const Area operator+(const Vec2i &o) const
Definition: Area.h:106
const Area operator+(const Area &rhs) const
Definition: Area.h:109
std::pair< Area, Vec2i > clippedSrcDst(const Area &srcSurfaceBounds, const Area &srcArea, const Area &dstSurfaceBounds, const Vec2i &dstLT)
Definition: Area.cpp:222
static Area proportionalFit(const Area &srcArea, const Area &dstArea, bool center, bool expand=false)
Definition: Area.cpp:193
void moveULTo(const Vec2i &newUL)
Translates the Area so that its upper-left corner is newUL.
Definition: Area.cpp:102
void setY1(int32_t aY1)
Definition: Area.h:70
Vec2i getUL() const
Definition: Area.h:75
bool operator<(const Area &aArea) const
Definition: Area.cpp:183
friend std::ostream & operator<<(std::ostream &o, const Area &area)
Definition: Area.h:119
Definition: Area.h:35
Area getOffset(const Vec2i &off) const
Returns a copy of the Area translated by off.
Definition: Area.cpp:97
int32_t getX1() const
Definition: Area.h:67
Area & operator+=(const Vec2i &o)
Definition: Area.h:112
Definition: Vector.h:68
Area & operator-=(const Vec2i &o)
Definition: Area.h:113
float distanceSquared(const Vec2< Y > &pt) const
Returns the squared distance between the point pt and the rectangle. Points inside the rectangle retu...
Definition: Area.cpp:161
int32_t getWidth() const
Definition: Area.h:47
Area getMoveULTo(const Vec2i &newUL) const
Returns a copy of the Area translated so that its upper-left corner is newUL.
Definition: Area.cpp:107
Area getClipBy(const Area &clip) const
Definition: Area.cpp:82
GLenum GLint x
Definition: GLee.h:987
int32_t x1
Definition: Area.h:101
GLintptr offset
Definition: GLee.h:2095
int32_t x2
Definition: Area.h:101
void set(int32_t aX1, int32_t aY1, int32_t aX2, int32_t aY2)
Definition: Area.cpp:40
Area()
Definition: Area.h:39
void offset(const Vec2i &off)
Translates the Area by off.
Definition: Area.cpp:89
const Area operator-(const Area &rhs) const
Definition: Area.h:110
Vec2i getSize() const
Definition: Area.h:49
T y
Definition: Vector.h:71
bool operator==(const Area &aArea) const
Definition: Area.h:103
int32_t y1
Definition: Area.h:101
float distance(const Vec2< Y > &pt) const
Returns the distance between the point pt and the rectangle. Points inside the Area return 0...
Definition: Area.cpp:146
bool contains(const Vec2< Y > &offset) const
Definition: Area.h:80
GLsizei const GLfloat * points
Definition: GLee.h:6298
int32_t calcArea() const
Definition: Area.h:51
void setX1(int32_t aX1)
Definition: Area.h:68
const Area operator-(const Vec2i &o) const
Definition: Area.h:107
Vec2< Y > closestPoint(const Vec2< Y > &pt) const
Returns the nearest point on the Rect rect. Points inside the rectangle return pt.
Definition: Area.cpp:173
Vec2f getCenter() const
Definition: Area.h:50
void setY2(int32_t aY2)
Definition: Area.h:74
void clipBy(const Area &clip)
Definition: Area.cpp:61
static Area zero()
Constructs an Area with all values initialized to zero.
Definition: Area.h:116
GLclampf f
Definition: GLee.h:15307
Vec2i getLR() const
Definition: Area.h:76
int32_t getHeight() const
Definition: Area.h:48
Vec2< int > Vec2i
Definition: Vector.h:1313