CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
|
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-2010 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@yahoo.fr 00024 */ 00031 /*----------------------------------------------------------------------------*/ 00035 template<class T> 00036 claw::math::rectangle<T>::rectangle() 00037 { 00038 00039 } // rectangle::rectangle() [constructor] 00040 00041 /*----------------------------------------------------------------------------*/ 00046 template<class T> 00047 template<class U> 00048 claw::math::rectangle<T>::rectangle( const rectangle<U>& that ) 00049 : position(that.position), width(that.width), height(that.height) 00050 { 00051 00052 } // rectangle::rectangle() [copy constructor] 00053 00054 /*----------------------------------------------------------------------------*/ 00059 template<class T> 00060 template<class U> 00061 claw::math::rectangle<T>::rectangle( const box_2d<U>& that ) 00062 : position(that.left(), that.top()), width(that.width()), 00063 height(that.height()) 00064 { 00065 00066 } // rectangle::rectangle() [copy constructor] 00067 00068 /*----------------------------------------------------------------------------*/ 00076 template<class T> 00077 claw::math::rectangle<T>::rectangle 00078 ( const value_type& _x, const value_type& _y, 00079 const value_type& _width, const value_type& _height ) 00080 : position(_x, _y), width(_width), height(_height) 00081 { 00082 00083 } // rectangle::rectangle() [constructor with values] 00084 00085 /*----------------------------------------------------------------------------*/ 00092 template<class T> 00093 template<typename U> 00094 claw::math::rectangle<T>::rectangle 00095 ( const coordinate_2d<U>& pos, const value_type& _width, 00096 const value_type& _height ) 00097 : position(pos), width(_width), height(_height) 00098 { 00099 00100 } // rectangle::rectangle() [constructor from position and size] 00101 00102 /*----------------------------------------------------------------------------*/ 00108 template<class T> 00109 template<typename U> 00110 claw::math::rectangle<T>::rectangle 00111 ( const coordinate_2d<U>& pos, const coordinate_2d<U>& size ) 00112 : position(pos), width(size.x), height(size.y) 00113 { 00114 00115 } // rectangle::rectangle() [constructor from position and size] 00116 00117 /*----------------------------------------------------------------------------*/ 00137 template<class T> 00138 template<typename U> 00139 claw::math::rectangle<U> claw::math::rectangle<T>::cast_value_type_to() const 00140 { 00141 return claw::math::rectangle<U> 00142 ( position.cast_value_type_to<U>(), (U)width, (U)height ); 00143 } // rectangle::cast_value_type_to() 00144 00145 /*----------------------------------------------------------------------------*/ 00149 template<class T> 00150 typename claw::math::rectangle<T>::value_type 00151 claw::math::rectangle<T>::area() const 00152 { 00153 return width * height; 00154 } // rectangle::area() 00155 00156 /*----------------------------------------------------------------------------*/ 00161 template<class T> 00162 bool 00163 claw::math::rectangle<T>::includes( const coordinate_2d<value_type>& p ) const 00164 { 00165 return (position.x <= p.x) && (right() >= p.x) 00166 && (position.y <= p.y) && (bottom() >= p.y); 00167 } // rectangle::includes() 00168 00169 /*----------------------------------------------------------------------------*/ 00174 template<class T> 00175 bool claw::math::rectangle<T>::includes( const self_type& r ) const 00176 { 00177 box_2d<value_type> his_box(r); 00178 00179 return includes(his_box.first_point) && includes(his_box.second_point); 00180 } // rectangle::includes() [rectangle] 00181 00182 /*----------------------------------------------------------------------------*/ 00187 template<class T> 00188 bool claw::math::rectangle<T>::intersects( const self_type& r ) const 00189 { 00190 return (right() >= r.position.x) 00191 && (r.right() >= position.x) 00192 && (bottom() >= r.position.y) 00193 && (r.bottom() >= position.y); 00194 } // rectangle::intersects() 00195 00196 /*----------------------------------------------------------------------------*/ 00201 template<class T> 00202 claw::math::rectangle<T> 00203 claw::math::rectangle<T>::intersection( const self_type& r ) const 00204 { 00205 self_type result; 00206 00207 if ( intersects(r) ) 00208 { 00209 x_intersection(r, result); 00210 y_intersection(r, result); 00211 } 00212 00213 return result; 00214 } // rectangle::intersection() 00215 00216 /*----------------------------------------------------------------------------*/ 00224 template<class T> 00225 void claw::math::rectangle<T>::set 00226 ( const value_type& new_x, const value_type& new_y, 00227 const value_type& new_width, const value_type& new_height ) 00228 { 00229 position.x = new_x; 00230 position.y = new_y; 00231 width = new_width; 00232 height = new_height; 00233 } // rectangle::set() 00234 00235 /*----------------------------------------------------------------------------*/ 00239 template<class T> 00240 typename claw::math::rectangle<T>::value_type 00241 claw::math::rectangle<T>::left() const 00242 { 00243 return position.x; 00244 } // rectangle::left() 00245 00246 /*----------------------------------------------------------------------------*/ 00250 template<class T> 00251 typename claw::math::rectangle<T>::value_type 00252 claw::math::rectangle<T>::right() const 00253 { 00254 return position.x + width; 00255 } // rectangle::right() 00256 00257 /*----------------------------------------------------------------------------*/ 00261 template<class T> 00262 typename claw::math::rectangle<T>::value_type 00263 claw::math::rectangle<T>::bottom() const 00264 { 00265 return position.y + height; 00266 } // rectangle::bottom() 00267 00268 /*----------------------------------------------------------------------------*/ 00272 template<class T> 00273 typename claw::math::rectangle<T>::value_type 00274 claw::math::rectangle<T>::top() const 00275 { 00276 return position.y; 00277 } // rectangle::top() 00278 00279 /*----------------------------------------------------------------------------*/ 00283 template<class T> 00284 claw::math::coordinate_2d< typename claw::math::rectangle<T>::value_type > 00285 claw::math::rectangle<T>::size() const 00286 { 00287 return claw::math::coordinate_2d<value_type>(width, height); 00288 } // rectangle::size() 00289 00290 /*----------------------------------------------------------------------------*/ 00296 template<class T> 00297 void claw::math::rectangle<T>::x_intersection 00298 ( const self_type& r, self_type& result ) const 00299 { 00300 if (position.x <= r.position.x) 00301 { 00302 result.position.x = r.position.x; 00303 00304 if (right() >= r.right()) 00305 result.width = r.width; 00306 else 00307 result.width = right() - r.position.x; 00308 } 00309 else 00310 r.x_intersection(*this, result); 00311 00312 } // rectangle::x_intersection() 00313 00314 /*----------------------------------------------------------------------------*/ 00320 template<class T> 00321 void claw::math::rectangle<T>::y_intersection 00322 ( const self_type& r, self_type& result ) const 00323 { 00324 if (position.y <= r.position.y) 00325 { 00326 result.position.y = r.position.y; 00327 00328 if (bottom() >= r.bottom()) 00329 result.height = r.height; 00330 else 00331 result.height = bottom() - r.position.y; 00332 } 00333 else 00334 r.y_intersection(*this, result); 00335 00336 } // rectangle::y_intersection()