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 <boost/rational.hpp>
00031
00032 namespace cinder {
00033
00034 template<typename T>
00035 class AreaT {
00036 public:
00037 AreaT() {}
00038 AreaT( const Vec2<T> &UL, const Vec2<T> &LR );
00039 AreaT( T aX1, T aY1, T aX2, T aY2 )
00040 { set( aX1, aY1, aX2, aY2 ); }
00041 template<typename Y>
00042 explicit AreaT( const AreaT<Y> &aAreaBase );
00043
00044 void set( T aX1, T aY1, T aX2, T aY2 );
00045
00046 T getWidth() const { return x2 - x1; }
00047 T getHeight() const { return y2 - y1; }
00048 Vec2<T> getSize() const { return Vec2<T>( x2 - x1, y2 - y1 ); }
00049 T calcArea() const { return getWidth() * getHeight(); }
00050
00051 void clipBy( const AreaT<T> &clip );
00052 AreaT<T> getClipBy( const AreaT<T> &clip ) const;
00053
00055 void offsetBy( const Vec2<T> &offset );
00057 AreaT<T> getOffsetBy( const Vec2<T> &offset ) const;
00059 void moveULTo( const Vec2<T> &newUL );
00061 AreaT<T> getMoveULTo( const Vec2<T> &newUL ) const;
00063 void expand( T expandX, T expandY ) { x1 -= expandX; x2 += expandX; y1 -= expandY; y2 += expandY; }
00064
00065 T getX1() const { return x1; }
00066 void setX1( T aX1 ) { x1 = aX1; }
00067 T getY1() const { return y1; }
00068 void setY1( T aY1 ) { y1 = aY1; }
00069 T getX2() const { return x2; }
00070 void setX2( T aX2 ) { x2 = aX2; }
00071 T getY2() const { return y2; }
00072 void setY2( T aY2 ) { y2 = aY2; }
00073 Vec2<T> getUL() const { return Vec2<T>( x1, y1 ); }
00074 Vec2<T> getLR() const { return Vec2<T>( x2, y2 ); }
00075
00076 bool isInside( const Vec2<T> &offset ) const;
00077 template<typename Y>
00078 bool isInside( const Vec2<Y> &offset ) const { return isInside( Vec2<T>( (T)math<Y>::ceil( offset. x ), (T)math<Y>::ceil( offset.y ) ) ); }
00079 bool intersects( const AreaT<T> &area ) const;
00080
00081 T x1, y1, x2, y2;
00082
00083 bool operator==( const AreaT<T> &aArea ) const { return ( ( x1 == aArea.x1 ) && ( y1 == aArea.y1 ) && ( x2 == aArea.x2 ) && ( y2 == aArea.y2 ) ); }
00084 bool operator<( const AreaT<T> &aArea ) const;
00085 AreaT<T> operator+( const Vec2<T> &offset ) const { return this->getOffsetBy( offset ); }
00086
00087 static AreaT<T> proportionalFit( const AreaT<T> &srcArea, const AreaT<T> &dstArea, bool center, bool expand = false );
00088
00089 friend std::ostream& operator<<( std::ostream &o, const AreaT<T> &area )
00090 {
00091 return o << "(" << area.x1 << ", " << area.y1 << ")-(" << area.x2 << ", " << area.y2 << ")";
00092 }
00093 };
00094
00095 typedef AreaT<int32_t> Area;
00096 typedef AreaT<boost::rational<int32_t> > AreaRational;
00097
00098 extern std::pair<Area,Vec2i> clippedSrcDst( const Area &srcSurfaceBounds, const Area &srcArea, const Area &dstSurfaceBounds, const Vec2i &dstLT );
00099
00100 }