box_2d.tpp

Go to the documentation of this file.
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-2008 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 */
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 /*----------------------------------------------------------------------------*/
00215 template<class T>
00216 typename claw::math::box_2d<T>::value_type claw::math::box_2d<T>::top() const
00217 {
00218   return (first_point.y > second_point.y) ? first_point.y : second_point.y;
00219 } // box_2d::top()
00220 
00221 /*----------------------------------------------------------------------------*/
00225 template<class T>
00226 typename claw::math::box_2d<T>::value_type claw::math::box_2d<T>::bottom() const
00227 {
00228   return (first_point.y < second_point.y) ? first_point.y : second_point.y;
00229 } // box_2d::bottom()
00230 
00231 /*----------------------------------------------------------------------------*/
00235 template<class T>
00236 typename claw::math::box_2d<T>::value_type claw::math::box_2d<T>::left() const
00237 {
00238   return (first_point.x < second_point.x) ? first_point.x : second_point.x;
00239 } // box_2d::left()
00240 
00241 /*----------------------------------------------------------------------------*/
00245 template<class T>
00246 typename claw::math::box_2d<T>::value_type claw::math::box_2d<T>::right() const
00247 {
00248   return (first_point.x > second_point.x) ? first_point.x : second_point.x;
00249 } // box_2d::right()
00250 
00251 /*----------------------------------------------------------------------------*/
00255 template<class T>
00256 typename claw::math::box_2d<T>::point_type
00257 claw::math::box_2d<T>::top_left() const
00258 {
00259   return point_type(left(), top());
00260 } // box_2d::top_left()
00261 
00262 /*----------------------------------------------------------------------------*/
00266 template<class T>
00267 typename claw::math::box_2d<T>::point_type
00268 claw::math::box_2d<T>::top_right() const
00269 {
00270   return point_type(right(), top());
00271 } // box_2d::top_right()
00272 
00273 /*----------------------------------------------------------------------------*/
00277 template<class T>
00278 typename claw::math::box_2d<T>::point_type
00279 claw::math::box_2d<T>::bottom_left() const
00280 {
00281   return point_type(left(), bottom());
00282 } // box_2d::bottom_left()
00283 
00284 /*----------------------------------------------------------------------------*/
00288 template<class T>
00289 typename claw::math::box_2d<T>::point_type
00290 claw::math::box_2d<T>::bottom_right() const
00291 {
00292   return point_type(right(), bottom());
00293 } // box_2d::bottom_right()
00294 
00295 /*----------------------------------------------------------------------------*/
00300 template<class T>
00301 void claw::math::box_2d<T>::top( const value_type& p )
00302 {
00303   shift_y(p - top());
00304 } // box_2d::top()
00305 
00306 /*----------------------------------------------------------------------------*/
00311 template<class T>
00312 void claw::math::box_2d<T>::bottom( const value_type& p )
00313 {
00314   shift_y(p - bottom());
00315 } // box_2d::bottom()
00316 
00317 /*----------------------------------------------------------------------------*/
00322 template<class T>
00323 void claw::math::box_2d<T>::left( const value_type& p )
00324 {
00325   shift_x(p - left());
00326 } // box_2d::left()
00327 
00328 /*----------------------------------------------------------------------------*/
00333 template<class T>
00334 void claw::math::box_2d<T>::right( const value_type& p )
00335 {
00336   shift_x(p - right());
00337 } // box_2d::right()
00338 
00339 /*----------------------------------------------------------------------------*/
00344 template<class T>
00345 void
00346 claw::math::box_2d<T>::top_left( const coordinate_2d<value_type>& p )
00347 {
00348   top(p.y);
00349   left(p.x);
00350 } // box_2d::top_left()
00351 
00352 /*----------------------------------------------------------------------------*/
00357 template<class T>
00358 void
00359 claw::math::box_2d<T>::top_right( const coordinate_2d<value_type>& p )
00360 {
00361   top(p.y);
00362   right(p.x);
00363 } // box_2d::top_right()
00364 
00365 /*----------------------------------------------------------------------------*/
00370 template<class T>
00371 void
00372 claw::math::box_2d<T>::bottom_left( const coordinate_2d<value_type>& p )
00373 {
00374   bottom(p.y);
00375   left(p.x);
00376 } // box_2d::bottom_left()
00377 
00378 /*----------------------------------------------------------------------------*/
00383 template<class T>
00384 void claw::math::box_2d<T>::bottom_right
00385 ( const coordinate_2d<value_type>& p )
00386 {
00387   bottom(p.y);
00388   right(p.x);
00389 } // box_2d::bottom_right()
00390 
00391 /*----------------------------------------------------------------------------*/
00396 template<class T>
00397 void claw::math::box_2d<T>::shift_x( const value_type& d )
00398 {
00399   first_point.x += d;
00400   second_point.x += d;
00401 } // box_2d::shift_x()
00402 
00403 /*----------------------------------------------------------------------------*/
00408 template<class T>
00409 void claw::math::box_2d<T>::shift_y( const value_type& d )
00410 {
00411   first_point.y += d;
00412   second_point.y += d;
00413 } // box_2d::shift_y()
00414 
00415 /*----------------------------------------------------------------------------*/
00419 template<class T>
00420 claw::math::coordinate_2d< typename claw::math::box_2d<T>::value_type >
00421 claw::math::box_2d<T>::size() const
00422 {
00423   return claw::math::coordinate_2d<value_type>(width(), height());
00424 } // box_2d::size()
00425 
00426 /*----------------------------------------------------------------------------*/
00431 template<class T>
00432 bool claw::math::box_2d<T>::operator==(const self_type& that) const
00433 {
00434   return (first_point == that.first_point)
00435     && (second_point == that.second_point)
00436     || (first_point == that.second_point)
00437     && (second_point == that.first_point);
00438 } // box_2d::operator==()
00439 
00440 /*----------------------------------------------------------------------------*/
00445 template<class T>
00446 bool claw::math::box_2d<T>::operator!=(const self_type& that) const
00447 {
00448   return !( *this == that );
00449 } // box_2d::operator!=()
00450 
00451 /*----------------------------------------------------------------------------*/
00456 template<class T>
00457 claw::math::box_2d<T>
00458 claw::math::box_2d<T>::operator+(const point_type& vect) const
00459 {
00460   return self_type( first_point + vect, second_point + vect );
00461 } // box_2d::operator+()
00462 
00463 /*----------------------------------------------------------------------------*/
00468 template<class T>
00469 claw::math::box_2d<T>
00470 claw::math::box_2d<T>::operator-(const point_type& vect) const
00471 {
00472   return self_type( first_point - vect, second_point - vect );
00473 } // box_2d::operator-()
00474 
00475 /*----------------------------------------------------------------------------*/
00480 template<class T>
00481 claw::math::box_2d<T>&
00482 claw::math::box_2d<T>::operator+=(const point_type& vect)
00483 {
00484   first_point += vect;
00485   second_point += vect;
00486 } // box_2d::operator+=()
00487 
00488 /*----------------------------------------------------------------------------*/
00493 template<class T>
00494 claw::math::box_2d<T>&
00495 claw::math::box_2d<T>::operator-=(const point_type& vect)
00496 {
00497   first_point -= vect;
00498   second_point -= vect;
00499 } // box_2d::operator-=()
00500 
00501 /*----------------------------------------------------------------------------*/
00505 template<class T>
00506 typename claw::math::box_2d<T>::value_type
00507 claw::math::box_2d<T>::width() const
00508 {
00509   if (first_point.x > second_point.x)
00510     return first_point.x - second_point.x;
00511   else
00512     return second_point.x - first_point.x;
00513 } // box_2d::width()
00514 
00515 /*----------------------------------------------------------------------------*/
00519 template<class T>
00520 typename claw::math::box_2d<T>::value_type
00521 claw::math::box_2d<T>::height() const
00522 {
00523   if (first_point.y > second_point.y)
00524     return first_point.y - second_point.y;
00525   else
00526     return second_point.y - first_point.y;
00527 } // box_2d::height()
00528 
00529 /*----------------------------------------------------------------------------*/
00535 template<class T>
00536 void claw::math::box_2d<T>::x_intersection
00537 ( const self_type& r, self_type& result ) const
00538 {
00539   if (left() <= r.left())
00540     {
00541       result.first_point.x = r.left();
00542 
00543       if (right() >= r.right())
00544         result.second_point.x = r.right();
00545       else
00546         result.second_point.x = right();
00547     }
00548   else
00549     r.x_intersection(*this, result);
00550 } // box_2d::x_intersection()
00551 
00552 /*----------------------------------------------------------------------------*/
00558 template<class T>
00559 void claw::math::box_2d<T>::y_intersection
00560 ( const self_type& r, self_type& result ) const
00561 {
00562   if (bottom() <= r.bottom())
00563     {
00564       result.first_point.y = r.bottom();
00565 
00566       if (top() >= r.top())
00567         result.second_point.y = r.top();
00568       else
00569         result.second_point.y = top();
00570     }
00571   else
00572     r.y_intersection(*this, result);
00573 } // box_2d::y_intersection()

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1