Claw 1.7.0
|
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 <claw/rectangle.hpp> 00031 #include <claw/assert.hpp> 00032 00033 /*----------------------------------------------------------------------------*/ 00037 template<class T> 00038 claw::math::box_2d<T>::box_2d() 00039 { 00040 00041 } // box_2d::box_2d() [constructor] 00042 00043 /*----------------------------------------------------------------------------*/ 00048 template<class T> 00049 claw::math::box_2d<T>::box_2d( const self_type& that ) 00050 : first_point(that.first_point), second_point(that.second_point) 00051 { 00052 00053 } // box_2d::box_2d() [copy constructor] 00054 00055 /*----------------------------------------------------------------------------*/ 00060 template<class T> 00061 claw::math::box_2d<T>::box_2d( const rectangle<value_type>& that ) 00062 : first_point(that.position), 00063 second_point(that.right(), that.bottom()) 00064 { 00065 00066 } // box_2d::box_2d() [constructor from rectangle] 00067 00068 /*----------------------------------------------------------------------------*/ 00074 template<class T> 00075 claw::math::box_2d<T>::box_2d( const point_type& p1, const point_type& p2 ) 00076 : first_point(p1), second_point(p2) 00077 { 00078 00079 } // box_2d::box_2d() [constructor from coordinates] 00080 00081 /*----------------------------------------------------------------------------*/ 00089 template<class T> 00090 claw::math::box_2d<T>::box_2d( const value_type& x1, const value_type& y1, 00091 const value_type& x2, const value_type& y2 ) 00092 : first_point(x1, y1), second_point(x2, y2) 00093 { 00094 00095 } // box_2d::box_2d() [constructor with values] 00096 00097 /*----------------------------------------------------------------------------*/ 00105 template<class T> 00106 void claw::math::box_2d<T>::set 00107 ( const value_type& x1, const value_type& y1, const value_type& x2, 00108 const value_type& y2 ) 00109 { 00110 first_point.set(x1, y1); 00111 second_point.set(x2, y2); 00112 } // box_2d::set() 00113 00114 /*----------------------------------------------------------------------------*/ 00134 template<class T> 00135 template<typename U> 00136 claw::math::box_2d<U> claw::math::box_2d<T>::cast_value_type_to() const 00137 { 00138 return claw::math::box_2d<U> 00139 ( first_point.cast_value_type_to<U>(), 00140 second_point.cast_value_type_to<U>() ); 00141 } // box_2d::cast_value_type_to() 00142 00143 /*----------------------------------------------------------------------------*/ 00147 template<class T> 00148 typename claw::math::box_2d<T>::value_type claw::math::box_2d<T>::area() const 00149 { 00150 return width() * height(); 00151 } // box_2d::area() 00152 00153 /*----------------------------------------------------------------------------*/ 00158 template<class T> 00159 bool 00160 claw::math::box_2d<T>::includes( const coordinate_2d<value_type>& p ) const 00161 { 00162 return (left() <= p.x) && (right() >= p.x) 00163 && (bottom() <= p.y) && (top() >= p.y); 00164 } // box_2d::includes() 00165 00166 /*----------------------------------------------------------------------------*/ 00171 template<class T> 00172 bool claw::math::box_2d<T>::includes( const self_type& r ) const 00173 { 00174 return includes(r.first_point) && includes(r.second_point); 00175 } // box_2d::includes() 00176 00177 /*----------------------------------------------------------------------------*/ 00182 template<class T> 00183 bool claw::math::box_2d<T>::intersects( const self_type& r ) const 00184 { 00185 return (right() >= r.left()) && (r.right() >= left()) 00186 && (top() >= r.bottom()) && (r.top() >= bottom()); 00187 } // box_2d::intersects() 00188 00189 /*----------------------------------------------------------------------------*/ 00194 template<class T> 00195 claw::math::box_2d<T> 00196 claw::math::box_2d<T>::intersection( const self_type& r ) const 00197 { 00198 CLAW_PRECOND( intersects(r) ); 00199 00200 self_type result; 00201 00202 if ( intersects(r) ) 00203 { 00204 x_intersection(r, result); 00205 y_intersection(r, result); 00206 } 00207 00208 return result; 00209 } // box_2d::intersection() 00210 00211 /*----------------------------------------------------------------------------*/ 00217 template<class T> 00218 claw::math::box_2d<T> claw::math::box_2d<T>::join( const self_type& r ) const 00219 { 00220 return self_type 00221 ( std::min(r.left(), left()), std::min(r.bottom(), bottom()), 00222 std::max(r.right(), right()), std::max(r.top(), top()) ); 00223 } // box_2d::join() 00224 00225 /*----------------------------------------------------------------------------*/ 00229 template<class T> 00230 bool claw::math::box_2d<T>::empty() const 00231 { 00232 return (width() == 0) || (height() == 0); 00233 } // box_2d::empty() 00234 00235 /*----------------------------------------------------------------------------*/ 00239 template<class T> 00240 typename claw::math::box_2d<T>::value_type claw::math::box_2d<T>::top() const 00241 { 00242 return (first_point.y > second_point.y) ? first_point.y : second_point.y; 00243 } // box_2d::top() 00244 00245 /*----------------------------------------------------------------------------*/ 00249 template<class T> 00250 typename claw::math::box_2d<T>::value_type claw::math::box_2d<T>::bottom() const 00251 { 00252 return (first_point.y < second_point.y) ? first_point.y : second_point.y; 00253 } // box_2d::bottom() 00254 00255 /*----------------------------------------------------------------------------*/ 00259 template<class T> 00260 typename claw::math::box_2d<T>::value_type claw::math::box_2d<T>::left() const 00261 { 00262 return (first_point.x < second_point.x) ? first_point.x : second_point.x; 00263 } // box_2d::left() 00264 00265 /*----------------------------------------------------------------------------*/ 00269 template<class T> 00270 typename claw::math::box_2d<T>::value_type claw::math::box_2d<T>::right() const 00271 { 00272 return (first_point.x > second_point.x) ? first_point.x : second_point.x; 00273 } // box_2d::right() 00274 00275 /*----------------------------------------------------------------------------*/ 00279 template<class T> 00280 typename claw::math::box_2d<T>::point_type 00281 claw::math::box_2d<T>::top_left() const 00282 { 00283 return point_type(left(), top()); 00284 } // box_2d::top_left() 00285 00286 /*----------------------------------------------------------------------------*/ 00290 template<class T> 00291 typename claw::math::box_2d<T>::point_type 00292 claw::math::box_2d<T>::top_right() const 00293 { 00294 return point_type(right(), top()); 00295 } // box_2d::top_right() 00296 00297 /*----------------------------------------------------------------------------*/ 00301 template<class T> 00302 typename claw::math::box_2d<T>::point_type 00303 claw::math::box_2d<T>::bottom_left() const 00304 { 00305 return point_type(left(), bottom()); 00306 } // box_2d::bottom_left() 00307 00308 /*----------------------------------------------------------------------------*/ 00312 template<class T> 00313 typename claw::math::box_2d<T>::point_type 00314 claw::math::box_2d<T>::bottom_right() const 00315 { 00316 return point_type(right(), bottom()); 00317 } // box_2d::bottom_right() 00318 00319 /*----------------------------------------------------------------------------*/ 00324 template<class T> 00325 void claw::math::box_2d<T>::top( const value_type& p ) 00326 { 00327 shift_y(p - top()); 00328 } // box_2d::top() 00329 00330 /*----------------------------------------------------------------------------*/ 00335 template<class T> 00336 void claw::math::box_2d<T>::bottom( const value_type& p ) 00337 { 00338 shift_y(p - bottom()); 00339 } // box_2d::bottom() 00340 00341 /*----------------------------------------------------------------------------*/ 00346 template<class T> 00347 void claw::math::box_2d<T>::left( const value_type& p ) 00348 { 00349 shift_x(p - left()); 00350 } // box_2d::left() 00351 00352 /*----------------------------------------------------------------------------*/ 00357 template<class T> 00358 void claw::math::box_2d<T>::right( const value_type& p ) 00359 { 00360 shift_x(p - right()); 00361 } // box_2d::right() 00362 00363 /*----------------------------------------------------------------------------*/ 00368 template<class T> 00369 void 00370 claw::math::box_2d<T>::top_left( const coordinate_2d<value_type>& p ) 00371 { 00372 top(p.y); 00373 left(p.x); 00374 } // box_2d::top_left() 00375 00376 /*----------------------------------------------------------------------------*/ 00381 template<class T> 00382 void 00383 claw::math::box_2d<T>::top_right( const coordinate_2d<value_type>& p ) 00384 { 00385 top(p.y); 00386 right(p.x); 00387 } // box_2d::top_right() 00388 00389 /*----------------------------------------------------------------------------*/ 00394 template<class T> 00395 void 00396 claw::math::box_2d<T>::bottom_left( const coordinate_2d<value_type>& p ) 00397 { 00398 bottom(p.y); 00399 left(p.x); 00400 } // box_2d::bottom_left() 00401 00402 /*----------------------------------------------------------------------------*/ 00407 template<class T> 00408 void claw::math::box_2d<T>::bottom_right 00409 ( const coordinate_2d<value_type>& p ) 00410 { 00411 bottom(p.y); 00412 right(p.x); 00413 } // box_2d::bottom_right() 00414 00415 /*----------------------------------------------------------------------------*/ 00420 template<class T> 00421 void claw::math::box_2d<T>::shift_x( const value_type& d ) 00422 { 00423 first_point.x += d; 00424 second_point.x += d; 00425 } // box_2d::shift_x() 00426 00427 /*----------------------------------------------------------------------------*/ 00432 template<class T> 00433 void claw::math::box_2d<T>::shift_y( const value_type& d ) 00434 { 00435 first_point.y += d; 00436 second_point.y += d; 00437 } // box_2d::shift_y() 00438 00439 /*----------------------------------------------------------------------------*/ 00443 template<class T> 00444 claw::math::coordinate_2d< typename claw::math::box_2d<T>::value_type > 00445 claw::math::box_2d<T>::size() const 00446 { 00447 return claw::math::coordinate_2d<value_type>(width(), height()); 00448 } // box_2d::size() 00449 00450 /*----------------------------------------------------------------------------*/ 00455 template<class T> 00456 bool claw::math::box_2d<T>::operator==(const self_type& that) const 00457 { 00458 return (left() == that.left()) && (right() == that.right()) 00459 && (top() == that.top()) && (bottom() == that.bottom()); 00460 } // box_2d::operator==() 00461 00462 /*----------------------------------------------------------------------------*/ 00467 template<class T> 00468 bool claw::math::box_2d<T>::operator!=(const self_type& that) const 00469 { 00470 return !( *this == that ); 00471 } // box_2d::operator!=() 00472 00473 /*----------------------------------------------------------------------------*/ 00478 template<class T> 00479 claw::math::box_2d<T> 00480 claw::math::box_2d<T>::operator+(const point_type& vect) const 00481 { 00482 return self_type( first_point + vect, second_point + vect ); 00483 } // box_2d::operator+() 00484 00485 /*----------------------------------------------------------------------------*/ 00490 template<class T> 00491 claw::math::box_2d<T> 00492 claw::math::box_2d<T>::operator-(const point_type& vect) const 00493 { 00494 return self_type( first_point - vect, second_point - vect ); 00495 } // box_2d::operator-() 00496 00497 /*----------------------------------------------------------------------------*/ 00502 template<class T> 00503 claw::math::box_2d<T>& 00504 claw::math::box_2d<T>::operator+=(const point_type& vect) 00505 { 00506 first_point += vect; 00507 second_point += vect; 00508 } // box_2d::operator+=() 00509 00510 /*----------------------------------------------------------------------------*/ 00515 template<class T> 00516 claw::math::box_2d<T>& 00517 claw::math::box_2d<T>::operator-=(const point_type& vect) 00518 { 00519 first_point -= vect; 00520 second_point -= vect; 00521 } // box_2d::operator-=() 00522 00523 /*----------------------------------------------------------------------------*/ 00527 template<class T> 00528 typename claw::math::box_2d<T>::value_type 00529 claw::math::box_2d<T>::width() const 00530 { 00531 if (first_point.x > second_point.x) 00532 return first_point.x - second_point.x; 00533 else 00534 return second_point.x - first_point.x; 00535 } // box_2d::width() 00536 00537 /*----------------------------------------------------------------------------*/ 00541 template<class T> 00542 typename claw::math::box_2d<T>::value_type 00543 claw::math::box_2d<T>::height() const 00544 { 00545 if (first_point.y > second_point.y) 00546 return first_point.y - second_point.y; 00547 else 00548 return second_point.y - first_point.y; 00549 } // box_2d::height() 00550 00551 /*----------------------------------------------------------------------------*/ 00557 template<class T> 00558 void claw::math::box_2d<T>::x_intersection 00559 ( const self_type& r, self_type& result ) const 00560 { 00561 result.first_point.x = std::max(left(), r.left()); 00562 result.second_point.x = std::min(right(), r.right()); 00563 } // box_2d::x_intersection() 00564 00565 /*----------------------------------------------------------------------------*/ 00571 template<class T> 00572 void claw::math::box_2d<T>::y_intersection 00573 ( const self_type& r, self_type& result ) const 00574 { 00575 result.first_point.y = std::max(bottom(), r.bottom()); 00576 result.second_point.y = std::min(top(), r.top()); 00577 } // box_2d::y_intersection()