Rect.h

Go to the documentation of this file.
00001 //**********************************************************************
00002 //
00003 //  REGCOM: Regimental Command
00004 //  Copyright (C) 2008 Randi J. Relander
00005 //    <rjrelander@users.sourceforge.net>
00006 //
00007 //  This program is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU General Public License
00009 //  as published by the Free Software Foundation; either version 3
00010 //  of the License, or (at your option) any later version.
00011 //  
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //  
00017 //  You should have received a copy of the GNU General Public
00018 //  License along with this program.  If not, see 
00019 //  <http://www.gnu.org/licenses/>.
00020 //  
00021 //**********************************************************************
00022 
00023 #ifndef REGCOM_GUI_RECT_H
00024 #define REGCOM_GUI_RECT_H
00025 
00026 // PROJECT INCLUDES
00027 #include "regcom/gui/Point.h"
00028 
00029 // NAMESPACES
00030 namespace regcom {
00031 namespace gui {
00032 
00034 //  CLASS: Rect
00040 class Rect
00041 {
00042 public:
00043 
00044     // PUBLIC METHODS
00045 
00047     Rect() {}
00048 
00050     virtual ~Rect() {}
00051 
00053     Rect(int x, int y, int w, int h)
00054     {
00055         this->x = x;
00056         this->y = y;
00057         this->w = w;
00058         this->h = h;
00059     }
00060 
00062     Rect(const Point& a, const Point& b)
00063     {
00064         x = a.GetX();
00065         y = a.GetY();
00066         
00067         w = b.GetX() - a.GetX();
00068         h = b.GetY() - a.GetY();
00069     }
00070 
00072     Point UpperLeft() const { return Point(x,y); }
00073 
00075     Point LowerRight() const { return Point(x+w,y+h); }
00076 
00078     bool Contains(const Point& point) const
00079     {
00080         return (point.GetX() >= x) && (point.GetX() <= x + w) && 
00081             (point.GetY() >= y) && (point.GetY() <= y + h);
00082     }
00083 
00085     bool Contains(const Rect& rect) const
00086     {
00087         return Contains(rect.UpperLeft()) &&
00088             Contains(rect.LowerRight());
00089     }
00090 
00092     Rect Intersection(const Rect& rect) const
00093     {
00094         int x0 = x > rect.x ? x : rect.x;
00095         int y0 = y > rect.y ? y : rect.y;
00096 
00097         int x1 = x + w < rect.x + rect.w ? x + w : rect.x + rect.w;
00098         int y1 = y + h < rect.y + rect.h ? y + h : rect.y + rect.h;
00099 
00100         return Rect(Point(x0,y0),Point(x1,y1));
00101     }
00102 
00104     Rect Union(const Rect& rect) const
00105     {
00106         int x0 = x < rect.x ? x : rect.x;
00107         int y0 = y < rect.y ? y : rect.y;
00108         
00109         int x1 = x + w > rect.x + rect.w ? x + w : rect.x + rect.w;
00110         int y1 = y + h > rect.y + rect.h ? y + h : rect.y + rect.h;
00111 
00112         return Rect(Point(x0,y0),Point(x1,y1));
00113     }
00114 
00116     bool IsValid() const { return (w >= 0) && (h >= 0); }
00117 
00118 public:
00119 
00120     // MEMBER VARIABLES
00121 
00123     int x;
00124     
00126     int y;
00127     
00129     int w;
00130     
00132     int h;
00133 };
00134 
00135 } // namespace gui
00136 } // namespace regcom
00137 
00138 #endif