# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/Rect.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #ifndef Rect_INCLUDE_ONCE
    00033 #define Rect_INCLUDE_ONCE
    00034
    00035 #include <vlCore/Vector2.hpp>
    00036
    00037 namespace vl
    00038 {
    00042   template<typename T>
    00043   class Rect
    00044   {
    00045   public:
    00046     virtual const char* className() { return "vl::Rect"; }
    00047     Rect(const Rect& other)
    00048     {
    00049       mX = other.x();
    00050       mY = other.y();
    00051       mWidth  = other.width();
    00052       mHeight = other.height();
    00053     }
    00054     Rect()
    00055     {
    00056       // null Rect
    00057       mX = 0;
    00058       mY = 0;
    00059       mWidth  = -1;
    00060       mHeight = -1;
    00061     }
    00062     Rect(T x, T y, T width, T height)
    00063     {
    00064       mX = x;
    00065       mY = y;
    00066       mWidth = width;
    00067       mHeight = height;
    00068     }
    00069
    00070     T x() const { return mX; }
    00071     T y() const { return mY; }
    00072     T width() const  { return mWidth; }
    00073     T height() const { return mHeight; }
    00074     T bottom() const { return mY; }
    00075     T left() const   { return mX; }
    00076     T top() const    { return mY+mHeight-1; }
    00077     T right() const  { return mX+mWidth-1;  }
    00078
    00079     void setX(T x) { mX = x; }
    00080     void setY(T y) { mY = y; }
    00081     void setWidth(T w)  { mWidth  = w; }
    00082     void setHeight(T h) { mHeight = h; }
    00083
    00084     bool isNull()  const { return width() <  0 || height() <  0; }
    00085     bool isPoint() const { return width() == 0 && height() == 0; }
    00086
    00087     Rect intersected(const Rect& other) const
    00088     {
    00089       if (isNull() || other.isNull())
    00090         return other;
    00091       T Ax1 = left();
    00092       T Ax2 = right();
    00093       T Ay1 = bottom();
    00094       T Ay2 = top();
    00095       T Bx1 = other.left();
    00096       T Bx2 = other.right();
    00097       T By1 = other.bottom();
    00098       T By2 = other.top();
    00099       if (Ax1 < Bx1) Ax1 = Bx1;
    00100       if (Ay1 < By1) Ay1 = By1;
    00101       if (Ax2 > Bx2) Ax2 = Bx2;
    00102       if (Ay2 > By2) Ay2 = By2;
    00103       return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
    00104     }
    00105
    00106     Rect united(const Rect& other) const
    00107     {
    00108       if (other.isNull())
    00109         return *this;
    00110       if (isNull())
    00111         return other;
    00112       T Ax1 = left();
    00113       T Ax2 = right();
    00114       T Ay1 = bottom();
    00115       T Ay2 = top();
    00116       T Bx1 = other.left();
    00117       T Bx2 = other.right();
    00118       T By1 = other.bottom();
    00119       T By2 = other.top();
    00120       if (Ax1 > Bx1) Ax1 = Bx1;
    00121       if (Ay1 > By1) Ay1 = By1;
    00122       if (Ax2 < Bx2) Ax2 = Bx2;
    00123       if (Ay2 < By2) Ay2 = By2;
    00124       return Rect(Ax1,Ay1,Ax2-Ax1+1,Ay2-Ay1+1);
    00125     }
    00126
    00130     bool operator<(const Rect& other) const
    00131     {
    00132       if (mX != other.mX)
    00133         return mX < other.mX;
    00134       else
    00135       if (mWidth != other.mWidth)
    00136         return mWidth < other.mWidth;
    00137       else
    00138       if (mHeight != other.mHeight)
    00139         return mHeight < other.mHeight;
    00140       else
    00141       if (mY != other.mY)
    00142         return mY < other.mY;
    00143       else
    00144         return false;
    00145     }
    00146
    00147   protected:
    00148     T mX;
    00149     T mY;
    00150     T mWidth;
    00151     T mHeight;
    00152   };
    00153
    00164   class RectI: public Rect<int>
    00165   {
    00166   public:
    00167     virtual const char* className() { return "vl::RectI"; }
    00168     RectI() {}
    00169     RectI(int x, int y, int width, int height) { mX=x; mY=y; mWidth=width; mHeight=height; }
    00170     RectI(const Rect<int>& other) { *this = other; }
    00171     RectI(const RectI& other): Rect<int>(other) { *this = other; }
    00172     // operator Rect<int>() const { return Rect<int>(x(), y(), width(), height()); }
    00173     RectI& operator=(const Rect<int>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
    00174     RectI& operator=(const RectI& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
    00175     int top() const    { return mY+mHeight-1; }
    00176     int right() const  { return mX+mWidth-1;  }
    00177     ivec2 bottomLeft() const { return ivec2(bottom(),left()); }
    00178     ivec2 topRight() const { return ivec2(top(),right()); }
    00179   };
    00180
    00191   class RectF: public Rect<float>
    00192   {
    00193   public:
    00194     virtual const char* className() { return "vl::RectF"; }
    00195     RectF() {}
    00196     RectF(float x, float y, float width, float height) { mX=x; mY=y; mWidth=width; mHeight=height; }
    00197     RectF(const RectF& other): Rect<float>(other) { *this = other; }
    00198     RectF(const Rect<float>& other) { *this = other; }
    00199     // operator Rect<float>() const { return Rect<float>(x(), y(), width(), height()); }
    00200     RectF& operator=(const Rect<float>& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
    00201     RectF& operator=(const RectF& other) { mX=other.x(); mY=other.y(); mWidth=other.width(); mHeight=other.height(); return *this; }
    00202     float top() const    { return mY+mHeight; }
    00203     float right() const  { return mX+mWidth;  }
    00204     fvec2 bottomLeft() const { return fvec2(bottom(),left()); }
    00205     fvec2 topRight() const { return fvec2(top(),right()); }
    00206     void addPoint(const fvec2& p)
    00207     {
    00208       fvec2 tr = topRight();
    00209       fvec2 bl = bottomLeft();
    00210       if (p.x() < bl.x())
    00211         bl.x() = p.x();
    00212       if (p.y() < bl.y())
    00213         bl.y() = p.y();
    00214       if (p.x() > tr.x())
    00215         tr.x() = p.x();
    00216       if (p.y() > tr.y())
    00217         tr.y() = p.y();
    00218       mX = bl.x();
    00219       mY = bl.y();
    00220       mWidth  = tr.x() - bl.x();
    00221       mHeight = tr.y() - bl.y();
    00222     }
    00223   };
    00224 }
    00225
    00226 #endif

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:21.
Permission is granted to use this page to write and publish articles regarding Visualization Library.