Claw
1.7.3
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2011 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien.jorge@gamned.org 00024 */ 00030 #include <algorithm> 00031 00032 /*----------------------------------------------------------------------------*/ 00036 template<class T> 00037 claw::math::rectangle<T>::rectangle() 00038 { 00039 00040 } // rectangle::rectangle() [constructor] 00041 00042 /*----------------------------------------------------------------------------*/ 00047 template<class T> 00048 template<class U> 00049 claw::math::rectangle<T>::rectangle( const rectangle<U>& that ) 00050 : position(that.position), width(that.width), height(that.height) 00051 { 00052 00053 } // rectangle::rectangle() [copy constructor] 00054 00055 /*----------------------------------------------------------------------------*/ 00060 template<class T> 00061 template<class U> 00062 claw::math::rectangle<T>::rectangle( const box_2d<U>& that ) 00063 : position(that.left(), that.top()), width(that.width()), 00064 height(that.height()) 00065 { 00066 00067 } // rectangle::rectangle() [copy constructor] 00068 00069 /*----------------------------------------------------------------------------*/ 00077 template<class T> 00078 claw::math::rectangle<T>::rectangle 00079 ( const value_type& _x, const value_type& _y, 00080 const value_type& _width, const value_type& _height ) 00081 : position(_x, _y), width(_width), height(_height) 00082 { 00083 00084 } // rectangle::rectangle() [constructor with values] 00085 00086 /*----------------------------------------------------------------------------*/ 00093 template<class T> 00094 template<typename U> 00095 claw::math::rectangle<T>::rectangle 00096 ( const coordinate_2d<U>& pos, const value_type& _width, 00097 const value_type& _height ) 00098 : position(pos), width(_width), height(_height) 00099 { 00100 00101 } // rectangle::rectangle() [constructor from position and size] 00102 00103 /*----------------------------------------------------------------------------*/ 00109 template<class T> 00110 template<typename U> 00111 claw::math::rectangle<T>::rectangle 00112 ( const coordinate_2d<U>& pos, const coordinate_2d<U>& size ) 00113 : position(pos), width(size.x), height(size.y) 00114 { 00115 00116 } // rectangle::rectangle() [constructor from position and size] 00117 00118 /*----------------------------------------------------------------------------*/ 00138 template<class T> 00139 template<typename U> 00140 claw::math::rectangle<U> claw::math::rectangle<T>::cast_value_type_to() const 00141 { 00142 return claw::math::rectangle<U> 00143 ( position.cast_value_type_to<U>(), (U)width, (U)height ); 00144 } // rectangle::cast_value_type_to() 00145 00146 /*----------------------------------------------------------------------------*/ 00151 template<class T> 00152 bool claw::math::rectangle<T>::operator==( const self_type& that ) const 00153 { 00154 return (position == that.position) && (width == that.width) 00155 && (height == that.height); 00156 } // rectangle::operator==() 00157 00158 /*----------------------------------------------------------------------------*/ 00163 template<class T> 00164 bool claw::math::rectangle<T>::operator!=( const self_type& that ) const 00165 { 00166 return !(*this == that); 00167 } // rectangle::operator!=() 00168 00169 /*----------------------------------------------------------------------------*/ 00173 template<class T> 00174 typename claw::math::rectangle<T>::value_type 00175 claw::math::rectangle<T>::area() const 00176 { 00177 return width * height; 00178 } // rectangle::area() 00179 00180 /*----------------------------------------------------------------------------*/ 00185 template<class T> 00186 bool 00187 claw::math::rectangle<T>::includes( const coordinate_2d<value_type>& p ) const 00188 { 00189 return (position.x <= p.x) && (right() >= p.x) 00190 && (position.y <= p.y) && (bottom() >= p.y); 00191 } // rectangle::includes() 00192 00193 /*----------------------------------------------------------------------------*/ 00198 template<class T> 00199 bool claw::math::rectangle<T>::includes( const self_type& r ) const 00200 { 00201 box_2d<value_type> his_box(r); 00202 00203 return includes(his_box.first_point) && includes(his_box.second_point); 00204 } // rectangle::includes() [rectangle] 00205 00206 /*----------------------------------------------------------------------------*/ 00211 template<class T> 00212 bool claw::math::rectangle<T>::intersects( const self_type& r ) const 00213 { 00214 return (right() >= r.position.x) 00215 && (r.right() >= position.x) 00216 && (bottom() >= r.position.y) 00217 && (r.bottom() >= position.y); 00218 } // rectangle::intersects() 00219 00220 /*----------------------------------------------------------------------------*/ 00225 template<class T> 00226 claw::math::rectangle<T> 00227 claw::math::rectangle<T>::intersection( const self_type& r ) const 00228 { 00229 self_type result; 00230 00231 if ( intersects(r) ) 00232 { 00233 x_intersection(r, result); 00234 y_intersection(r, result); 00235 } 00236 00237 return result; 00238 } // rectangle::intersection() 00239 00240 /*----------------------------------------------------------------------------*/ 00246 template<class T> 00247 claw::math::rectangle<T> 00248 claw::math::rectangle<T>::join( const self_type& r ) const 00249 { 00250 const T result_left = std::min( left(), r.left() ); 00251 const T result_top = std::min( top(), r.top() ); 00252 const T result_bottom = std::max( bottom(), r.bottom() ); 00253 const T result_right = std::max( right(), r.right() ); 00254 00255 return self_type 00256 ( result_left, result_top, result_right - result_left, 00257 result_bottom - result_top ); 00258 } // rectangle::join() 00259 00260 /*----------------------------------------------------------------------------*/ 00268 template<class T> 00269 void claw::math::rectangle<T>::set 00270 ( const value_type& new_x, const value_type& new_y, 00271 const value_type& new_width, const value_type& new_height ) 00272 { 00273 position.x = new_x; 00274 position.y = new_y; 00275 width = new_width; 00276 height = new_height; 00277 } // rectangle::set() 00278 00279 /*----------------------------------------------------------------------------*/ 00283 template<class T> 00284 typename claw::math::rectangle<T>::value_type 00285 claw::math::rectangle<T>::left() const 00286 { 00287 return position.x; 00288 } // rectangle::left() 00289 00290 /*----------------------------------------------------------------------------*/ 00294 template<class T> 00295 typename claw::math::rectangle<T>::value_type 00296 claw::math::rectangle<T>::right() const 00297 { 00298 return position.x + width; 00299 } // rectangle::right() 00300 00301 /*----------------------------------------------------------------------------*/ 00305 template<class T> 00306 typename claw::math::rectangle<T>::value_type 00307 claw::math::rectangle<T>::bottom() const 00308 { 00309 return position.y + height; 00310 } // rectangle::bottom() 00311 00312 /*----------------------------------------------------------------------------*/ 00316 template<class T> 00317 typename claw::math::rectangle<T>::value_type 00318 claw::math::rectangle<T>::top() const 00319 { 00320 return position.y; 00321 } // rectangle::top() 00322 00323 /*----------------------------------------------------------------------------*/ 00327 template<class T> 00328 claw::math::coordinate_2d< typename claw::math::rectangle<T>::value_type > 00329 claw::math::rectangle<T>::size() const 00330 { 00331 return claw::math::coordinate_2d<value_type>(width, height); 00332 } // rectangle::size() 00333 00334 /*----------------------------------------------------------------------------*/ 00340 template<class T> 00341 void claw::math::rectangle<T>::x_intersection 00342 ( const self_type& r, self_type& result ) const 00343 { 00344 if (position.x <= r.position.x) 00345 { 00346 result.position.x = r.position.x; 00347 00348 if (right() >= r.right()) 00349 result.width = r.width; 00350 else 00351 result.width = right() - r.position.x; 00352 } 00353 else 00354 r.x_intersection(*this, result); 00355 00356 } // rectangle::x_intersection() 00357 00358 /*----------------------------------------------------------------------------*/ 00364 template<class T> 00365 void claw::math::rectangle<T>::y_intersection 00366 ( const self_type& r, self_type& result ) const 00367 { 00368 if (position.y <= r.position.y) 00369 { 00370 result.position.y = r.position.y; 00371 00372 if (bottom() >= r.bottom()) 00373 result.height = r.height; 00374 else 00375 result.height = bottom() - r.position.y; 00376 } 00377 else 00378 r.y_intersection(*this, result); 00379 00380 } // rectangle::y_intersection()