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
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }