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/Vector.h"
00026 #include "cinder/Area.h"
00027
00028 #include <vector>
00029
00030 namespace cinder {
00031
00032 template<typename T>
00033 class RectT {
00034 public:
00035 RectT() {}
00037 RectT( const std::vector<Vec2<T> > &points );
00038 RectT( T aX1, T aY1, T aX2, T aY2 ) {
00039 set( aX1, aY1, aX2, aY2 );
00040 }
00041 RectT( const Vec2<T> &v1, const Vec2<T> &v2 ) {
00042 set( v1.x, v1.y, v2.x, v2.y );
00043 }
00044 RectT( const Area &area );
00045
00046 void set( T aX1, T aY1, T aX2, T aY2 );
00047
00048 T getWidth() const { return x2 - x1; }
00049 T getHeight() const { return y2 - y1; }
00050 T getAspectRatio() const { return getWidth() / getHeight(); }
00051 T calcArea() const { return getWidth() * getHeight(); }
00052
00053 void canonicalize();
00054 RectT canonicalized() const;
00055
00056 void clipBy( const RectT &clip );
00057 RectT getClipBy( const RectT &clip ) const;
00058 Area getInteriorArea() const;
00059 void offset( const Vec2<T> &offset );
00060 RectT getOffset( const Vec2<T> &off ) { RectT result( *this ); result.offset( off ); return result; }
00062 void offsetCenterTo( const Vec2<T> ¢er ) { offset( center - getCenter() ); }
00063 void scaleCentered( const Vec2<T> &scale );
00064 void scaleCentered( T scale );
00065 RectT scaledCentered( T scale ) const;
00066 void scale( T scale );
00067 RectT scaled( T scale ) const;
00068
00070 template<typename Y>
00071 bool isInside( const Vec2<Y> &pt ) const { return ( pt.x >= x1 ) && ( pt.x <= x2 ) && ( pt.y >= y1 ) && ( pt.y <= y2 ); }
00073 bool intersects( const RectT &rect ) const;
00074
00075 T getX1() const { return x1; }
00076 T getY1() const { return y1; }
00077 T getX2() const { return x2; }
00078 T getY2() const { return y2; }
00079
00080 Vec2<T> getUpperLeft() const { return Vec2<T>( x1, y1 ); };
00081 Vec2<T> getUpperRight() const { return Vec2<T>( x2, y1 ); };
00082 Vec2<T> getLowerRight() const { return Vec2<T>( x2, y2 ); };
00083 Vec2<T> getLowerLeft() const { return Vec2<T>( x1, y2 ); };
00084 Vec2<T> getCenter() const { return Vec2<T>( ( x1 + x2 ) / 2, ( y1 + y2 ) / 2 ); }
00085
00087 RectT getCenteredFit( const RectT &other, bool expand ) const;
00088
00090 void include( const Vec2<T> &point );
00092 void include( const std::vector<Vec2<T> > &points );
00094 void include( const RectT &rect );
00095
00096 T x1, y1, x2, y2;
00097
00098 friend std::ostream& operator<<( std::ostream &o, const RectT &rect )
00099 {
00100 return o << "(" << rect.x1 << ", " << rect.y1 << ")-(" << rect.x2 << ", " << rect.y2 << ")";
00101 }
00102
00103 };
00104
00105 typedef RectT<float> Rectf;
00106 typedef RectT<double> Rectd;
00107
00108
00109
00110 class RectMapping {
00111 public:
00112 RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect )
00113 : mSrcRect( aSrcRect ), mDstRect( aDstRect ) {}
00114 RectMapping( const Rectf &aSrcRect, const Rectf &aDstRect, bool preserveSrcAspect );
00115
00116 Vec2f map( const Vec2f &srcPoint ) const;
00117 Rectf map( const Rectf &srcRect ) const;
00118
00119 private:
00120 Rectf mSrcRect, mDstRect;
00121 };
00122
00123 extern void getClippedScaledRects( const Area &srcSurfaceBounds, const Rectf &srcRect, const Area &dstSurfaceBounds, const Area &dstArea, Rectf *resultSrcRect, Area *resultDstRect );
00124
00125 }