nux-1.16.0
|
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 void Rect::Set (int px, int py, int w, int h) 00093 { 00094 x = px; 00095 y = py; 00096 width = w; 00097 height = h; 00098 } 00099 00100 void Rect::SetPosition (int px, int py) 00101 { 00102 x = px; 00103 y = py; 00104 } 00105 00106 void Rect::SetSize (int w, int h) 00107 { 00108 width = w; 00109 height = h; 00110 } 00111 00112 bool Rect::IsInside (const Point &p) const 00113 { 00114 return ( (x <= p.x) && (x + width > p.x) && 00115 (y <= p.y) && (y + height > p.y) ); 00116 } 00117 00118 bool Rect::IsPointInside (int x_, int y_) const 00119 { 00120 return ( (x <= x_) && (x + width > x_) && 00121 (y <= y_) && (y + height > y_) ); 00122 } 00123 00124 Rect Rect::Intersect (const Rect &r) const 00125 { 00126 // Get the corner points. 00127 00128 bool intersect = ! ((r.x > x + width) || 00129 (r.x + r.width < x) || 00130 (r.y > y + height) || 00131 (r.y + r.height < y)); 00132 00133 if (intersect) 00134 { 00135 const Point &ul1 = Point (x, y); 00136 const Point &ul2 = Point (r.x, r.y); 00137 int xx = Max<int> (ul1.x, ul2.x); 00138 int yy = Max<int> (ul1.y, ul2.y); 00139 int ww = Min<int> (ul1.x + width, ul2.x + r.width) - xx; 00140 int hh = Min<int> (ul1.y + height, ul2.y + r.height) - yy; 00141 00142 return Rect (xx, yy, ww, hh); 00143 } 00144 else 00145 { 00146 // No intersection 00147 return Rect (); 00148 } 00149 } 00150 00151 // expand the width by factor_x and the height by factor_y 00152 void Rect::Expand (int dx, int dy) 00153 { 00154 if (!IsNull() ) 00155 { 00156 x -= dx; 00157 y -= dy; 00158 width += 2 * dx; 00159 height += 2 * dy; 00160 } 00161 } 00162 00163 // expand the width by factor_x and the height by factor_y 00164 Rect Rect::GetExpand (int dx, int dy) const 00165 { 00166 Rect r = Rect (x - dx, y - dy, width + 2 * dx, height + 2 * dy); 00167 00168 if (r.IsNull() ) 00169 { 00170 return Rect (0, 0, 0, 0); 00171 } 00172 00173 return r; 00174 } 00175 } 00176