Rect.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2002-2003 The Pentagram Team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #ifndef RECT_H_INCLUDED
00020 #define RECT_H_INCLUDED
00021 
00022 namespace Pentagram {
00023 
00024 struct Rect {
00025         sint32          x, y;
00026         sint32          w, h;
00027 
00028         Rect() : x(0), y(0), w(0), h(0) {}
00029         Rect(int nx, int ny, int nw, int nh) : x(nx), y(ny), w(nw), h(nh) {}
00030         Rect(const Rect& o) : x(o.x), y(o.y), w(o.w), h(o.h) {}
00031         
00032         void    Set(int nx, int ny, int nw, int nh) { x=nx; y=ny; w=nw; h=nh; }
00033         void    Set(Rect &o) { *this = o; }
00034         
00035         // Check to see if a Rectangle is 'valid'
00036         bool    IsValid() const { return w > 0 && h > 0; }
00037 
00038         // Check to see if a point is within the Rectangle
00039         bool    InRect(int px, int py) const { return px >= x && py >= y && px < (x+w) && py < (y+h); }
00040 
00041         // Move the Rect (Relative)
00042         void    MoveRel(sint32 dx, sint32 dy) { x=x+dx; y=y+dy; }
00043 
00044         // Move the Rect (Absolute)
00045         void    MoveAbs(sint32 nx, sint32 ny) { x=nx; y=ny; }
00046 
00047         // Resize the Rect (Relative)
00048         void    ResizeRel(sint32 dw, sint32 dh) { w=w+dw; h=h+dh; }
00049 
00050         // Resize the Rect (Absolute)
00051         void    ResizeAbs(sint32 nw, sint32 nh) { w=nw; h=nh; }
00052 
00053         // Intersect/Clip this rect with another
00054         void    Intersect(int ox, int oy, int ow, int oh)
00055         {
00056                 int x2 = x + w,         y2 = y + h;
00057                 int ox2 = ox + ow,      oy2 = oy + oh;
00058 
00059                 if (x < ox) x = ox;
00060                 else if (x > ox2) x = ox2;
00061 
00062                 if (x2 < ox) x2 = ox;
00063                 else if (x2 > ox2) x2 = ox2;
00064 
00065                 if (y < oy) y = oy;
00066                 else if (y > oy2) y = oy2;
00067 
00068                 if (y2 < oy) y2 = oy;
00069                 else if (y2 > oy2) y2 = oy2;
00070 
00071                 w = x2 - x;
00072                 h = y2 - y;
00073 
00074         }
00075 
00076         // Intersect/Clip this another with this
00077         void    IntersectOther(int &ox, int &oy, int &ow, int &oh) const
00078         {
00079                 int x2 = x + w,         y2 = y + h;
00080                 int ox2 = ox + ow,      oy2 = oy + oh;
00081 
00082                 if (ox < x) ox = x;
00083                 else if (ox > x2) ox = x2;
00084 
00085                 if (ox2 < x) ox2 = x;
00086                 else if (ox2 > x2) ox2 = x2;
00087 
00088                 if (oy < y) oy = y;
00089                 else if (oy > y2) oy = y2;
00090 
00091                 if (oy2 < y) oy2 = y;
00092                 else if (oy2 > y2) oy2 = y2;
00093 
00094                 ow = ox2 - ox;
00095                 oh = oy2 - oy;
00096         }
00097 
00098         // Intersect/Clip this rect with another
00099         void    Intersect(const Rect& o)
00100         {
00101                 Intersect(o.x, o.y, o.w, o.h);
00102         }
00103 
00104         // Union/Add this rect with another
00105         void    Union(int ox, int oy, int ow, int oh)
00106         {
00107                 int x2 = x + w,         y2 = y + h;
00108                 int ox2 = ox + ow,      oy2 = oy + oh;
00109 
00110                 if (ox < x) x = ox;
00111                 else if (ox2 > x2) x2= ox2;
00112 
00113                 if (oy < y) y = ox;
00114                 else if (oy2 > y2) y2 = ox2;
00115 
00116                 w = x2 - x;
00117                 h = y2 - y;
00118         }
00119 
00120         // Union/Add this rect with another
00121         void    Union(const Rect& o)
00122         {
00123                 Union(o.x, o.y, o.w, o.h);
00124         }
00125 
00126         bool    Overlaps(const Rect& o) const
00127         {
00128                 if (x+w <= o.x || o.x+o.w <= x) return false;
00129                 if (y+h <= o.y || o.y+o.h <= y) return false;
00130                 return true;
00131         }
00132 
00133         // Operator +=
00134         Rect &operator += (const Rect& o)
00135         {
00136                 Union(o.x, o.y, o.w, o.h);
00137                 return *(this);
00138         }
00139 
00140         // Operator +
00141         Rect &operator + (const Rect& o) const
00142         {
00143                 Rect result(*this);
00144                 return (result+=o);
00145         }
00146 
00147         bool operator == (const Rect& o) const
00148         {
00149                 return x == o.x && y == o.y && w == o.w && h == o.h;
00150         }
00151 
00152 };
00153 
00154 }
00155 
00156 #endif // RECT_H_INCLUDED

Generated on Fri Jul 27 22:27:31 2007 for pentagram by  doxygen 1.4.7