Back to index

nux  3.0.0
Rect.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "NuxCore.h"
00024 #include "Point.h"
00025 #include "Size.h"
00026 #include "Rect.h"
00027 
00028 namespace nux
00029 {
00030   Rect::Rect()
00031   {
00032     x = 0;
00033     y = 0;
00034     width = 0;
00035     height = 0;
00036 
00037   }
00038 
00039   Rect::Rect (int x_, int y_, int width_, int height_)
00040   {
00041     x = x_;
00042     y = y_;
00043     width = Max<int> (0, int (width_) );
00044     height = Max<int> (0, int (height_) );
00045   }
00046 
00047   Rect::~Rect()
00048   {
00049 
00050   }
00051 
00052   Rect::Rect (const Rect &r)
00053   {
00054     x = r.x;
00055     y = r.y;
00056     width = r.width;
00057     height = r.height;
00058   }
00059 
00060   Rect &Rect::operator = (const Rect &r)
00061   {
00062     if (&r == this)
00063       return *this;
00064 
00065     x = r.x;
00066     y = r.y;
00067     width = r.width;
00068     height = r.height;
00069     return *this;
00070   }
00071 
00072   bool Rect::operator == (const Rect &r) const
00073   {
00074     if ( (x == r.x) && (y == r.y) && (width == r.width) && (height == r.height) )
00075     {
00076       return true;
00077     }
00078 
00079     return false;
00080   }
00081 
00082   bool Rect::operator != (const Rect &r) const
00083   {
00084     if ( (x == r.x) && (y == r.y) && (width == r.width) && (height == r.height) )
00085     {
00086       return false;
00087     }
00088 
00089     return true;
00090   }
00091 
00092   bool Rect::IsNull() const
00093   {
00094     return ((width == 0) || (height == 0));
00095   }
00096 
00097   void Rect::Set (int px, int py, int w, int h)
00098   {
00099     x = px;
00100     y = py;
00101     width = w;
00102     height = h;
00103   }
00104 
00105   void Rect::SetPosition (int px, int py)
00106   {
00107     x = px;
00108     y = py;
00109   }
00110 
00111   void Rect::SetSize (int w, int h)
00112   {
00113     width = w;
00114     height = h;
00115   }
00116 
00117   bool Rect::IsInside (const Point &p) const
00118   {
00119     return ( (x <= p.x) && (x + width > p.x) &&
00120              (y <= p.y) && (y + height > p.y) );
00121   }
00122 
00123   bool Rect::IsPointInside (int x_, int y_) const
00124   {
00125     return ( (x <= x_) && (x + width > x_) &&
00126              (y <= y_) && (y + height > y_) );
00127   }
00128 
00129   Rect Rect::Intersect (const Rect &r) const
00130   {
00131     // Get the corner points.
00132 
00133     bool intersect = ! ((r.x > x + width) ||
00134                       (r.x + r.width < x) ||
00135                       (r.y > y + height) ||
00136                       (r.y + r.height < y));
00137 
00138     if (intersect)
00139     {
00140       const Point &ul1 = Point (x, y);
00141       const Point &ul2 = Point (r.x, r.y);
00142       int xx = Max<int> (ul1.x, ul2.x);
00143       int yy = Max<int> (ul1.y, ul2.y);
00144       int ww = Min<int> (ul1.x + width,  ul2.x + r.width) - xx;
00145       int hh = Min<int> (ul1.y + height, ul2.y + r.height) - yy;
00146 
00147       return Rect (xx, yy, ww, hh);
00148     }
00149     else
00150     {
00151       // No intersection
00152       return Rect ();
00153     }
00154   }
00155 
00156   // expand the width by factor_x and the height by factor_y
00157   void Rect::Expand (int dx, int dy)
00158   {
00159     if (!IsNull() )
00160     {
00161       x -= dx;
00162       y -= dy;
00163       width  += 2 * dx;
00164       height += 2 * dy;
00165     }
00166   }
00167 
00168   // expand the width by factor_x and the height by factor_y
00169   Rect Rect::GetExpand (int dx, int dy) const
00170   {
00171     Rect r = Rect (x - dx, y - dy, width + 2 * dx, height + 2 * dy);
00172 
00173     if (r.IsNull() )
00174     {
00175       return Rect (0, 0, 0, 0);
00176     }
00177 
00178     return r;
00179   }
00180 }
00181