# Cinder

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

# include/cinder/Rect.h

Go to the documentation of this file.

    00001 /*
    00002  Copyright (c) 2010, The Barbarian Group
    00003  All rights reserved.
    00004
    00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
    00006  the following conditions are met:
    00007
    00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
    00009     the following disclaimer.
    00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
    00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
    00012
    00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
    00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
    00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
    00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    00020  POSSIBILITY OF SUCH DAMAGE.
    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(); // return rect w/ properly ordered coordinates
    00054     RectT       canonicalized() const; // return rect w/ properly ordered coordinates
    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> &center ) { 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 // This class maps a rectangle into another rectangle
    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 } // namespace cinder