Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #pragma once
00024
00025 #include "cinder/Cinder.h"
00026 #include "cinder/Vector.h"
00027
00028 #include <iostream>
00029 #include <utility>
00030 #include <vector>
00031
00032 namespace cinder {
00033
00034 template<typename T>
00035 class RectT;
00036
00037 class Area {
00038 public:
00039 Area() {}
00040 Area( const Vec2i &UL, const Vec2i &LR );
00041 Area( int32_t aX1, int32_t aY1, int32_t aX2, int32_t aY2 )
00042 { set( aX1, aY1, aX2, aY2 ); }
00043 explicit Area( const RectT<float> &r );
00044
00045 void set( int32_t aX1, int32_t aY1, int32_t aX2, int32_t aY2 );
00046
00047 int32_t getWidth() const { return x2 - x1; }
00048 int32_t getHeight() const { return y2 - y1; }
00049 Vec2i getSize() const { return Vec2i( x2 - x1, y2 - y1 ); }
00050 Vec2f getCenter() const { return Vec2f( ( x1 + x2 ) / 2.0f, ( y1 + y2 ) / 2.0f ); }
00051 int32_t calcArea() const { return getWidth() * getHeight(); }
00052
00053 void clipBy( const Area &clip );
00054 Area getClipBy( const Area &clip ) const;
00055
00057 void offset( const Vec2i &off );
00059 Area getOffset( const Vec2i &off ) const;
00061 void moveULTo( const Vec2i &newUL );
00063 Area getMoveULTo( const Vec2i &newUL ) const;
00065 void expand( int32_t expandX, int32_t expandY ) { x1 -= expandX; x2 += expandX; y1 -= expandY; y2 += expandY; }
00066
00067 int32_t getX1() const { return x1; }
00068 void setX1( int32_t aX1 ) { x1 = aX1; }
00069 int32_t getY1() const { return y1; }
00070 void setY1( int32_t aY1 ) { y1 = aY1; }
00071 int32_t getX2() const { return x2; }
00072 void setX2( int32_t aX2 ) { x2 = aX2; }
00073 int32_t getY2() const { return y2; }
00074 void setY2( int32_t aY2 ) { y2 = aY2; }
00075 Vec2i getUL() const { return Vec2i( x1, y1 ); }
00076 Vec2i getLR() const { return Vec2i( x2, y2 ); }
00077
00078 bool contains( const Vec2i &offset ) const;
00079 template<typename Y>
00080 bool contains( const Vec2<Y> &offset ) const { return contains( Vec2i( (int32_t)math<Y>::ceil( offset. x ), (int32_t)math<Y>::ceil( offset.y ) ) ); }
00081 bool intersects( const Area &area ) const;
00082
00084 void include( const Vec2i &point );
00086 void include( const std::vector<Vec2i > &points );
00088 void include( const Area &area );
00089
00091 template<typename Y>
00092 float distance( const Vec2<Y> &pt ) const;
00094 template<typename Y>
00095 float distanceSquared( const Vec2<Y> &pt ) const;
00096
00098 template<typename Y>
00099 Vec2<Y> closestPoint( const Vec2<Y> &pt ) const;
00100
00101 int32_t x1, y1, x2, y2;
00102
00103 bool operator==( const Area &aArea ) const { return ( ( x1 == aArea.x1 ) && ( y1 == aArea.y1 ) && ( x2 == aArea.x2 ) && ( y2 == aArea.y2 ) ); }
00104 bool operator<( const Area &aArea ) const;
00105
00106 const Area operator+( const Vec2i &o ) const { return this->getOffset( o ); }
00107 const Area operator-( const Vec2i &o ) const { return this->getOffset( -o ); }
00108
00109 const Area operator+( const Area& rhs ) const { return Area( x1 + rhs.x1, y1 + rhs.y1, x2 + rhs.x2, y2 + rhs.y2 ); }
00110 const Area operator-( const Area& rhs ) const { return Area( x1 - rhs.x1, y1 - rhs.y1, x2 - rhs.x2, y2 - rhs.y2 ); }
00111
00112 Area& operator+=( const Vec2i &o ) { offset( o ); return *this; }
00113 Area& operator-=( const Vec2i &o ) { offset( -o ); return *this; }
00114
00115 static Area proportionalFit( const Area &srcArea, const Area &dstArea, bool center, bool expand = false );
00116
00117 friend std::ostream& operator<<( std::ostream &o, const Area &area )
00118 {
00119 return o << "(" << area.x1 << ", " << area.y1 << ")-(" << area.x2 << ", " << area.y2 << ")";
00120 }
00121 };
00122
00123 extern std::pair<Area,Vec2i> clippedSrcDst( const Area &srcSurfaceBounds, const Area &srcArea, const Area &dstSurfaceBounds, const Vec2i &dstLT );
00124
00125 }