Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
00224
00225
00229 template<class T>
00230 bool claw::math::box_2d<T>::empty() const
00231 {
00232 return (width() == 0) || (height() == 0);
00233 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }