00001
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifndef RCSC_GEOM_RECT2D_H
00033 #define RCSC_GEOM_RECT2D_H
00034
00035 #include <rcsc/geom/size_2d.h>
00036 #include <rcsc/geom/line_2d.h>
00037 #include <rcsc/geom/vector_2d.h>
00038
00039 namespace rcsc {
00040
00041 class Ray2D;
00042 class Segment2D;
00043
00057 class Rect2D {
00058 private:
00060 Vector2D M_top_left;
00061
00063 Size2D M_size;
00064
00065 public:
00069 Rect2D()
00070 : M_top_left( 0.0, 0.0 )
00071 , M_size( 0.0, 0.0 )
00072 { }
00073
00081 Rect2D( const double & left_x,
00082 const double & top_y,
00083 const double & length,
00084 const double & width )
00085 : M_top_left( left_x, top_y )
00086 , M_size( length, width )
00087 { }
00088
00095 Rect2D( const Vector2D & top_left,
00096 const double & length,
00097 const double & width )
00098 : M_top_left( top_left )
00099 , M_size( length, width )
00100 { }
00101
00107 Rect2D( const Vector2D & top_left,
00108 const Size2D & size )
00109 : M_top_left( top_left )
00110 , M_size( size )
00111 { }
00112
00121 Rect2D( const Vector2D & top_left,
00122 const Vector2D & bottom_right )
00123 : M_top_left( top_left )
00124 , M_size( bottom_right.x - top_left.x,
00125 bottom_right.y - top_left.y )
00126 {
00127 if ( bottom_right.x - top_left.x < 0.0 )
00128 {
00129 M_top_left.x = bottom_right.x;
00130 }
00131 if ( bottom_right.y - top_left.y < 0.0 )
00132 {
00133 M_top_left.y = bottom_right.y;
00134 }
00135 }
00136
00143 static
00144 Rect2D from_center( const Vector2D & center,
00145 const double & length,
00146 const double & width )
00147 {
00148 return Rect2D( center.x - length*0.5,
00149 center.y - width*0.5,
00150 length,
00151 width );
00152 }
00153
00161 static
00162 Rect2D from_center( const double & center_x,
00163 const double & center_y,
00164 const double & length,
00165 const double & width )
00166 {
00167 return Rect2D( center_x - length*0.5,
00168 center_y - width*0.5,
00169 length,
00170 width );
00171 }
00172
00178 static
00179 Rect2D from_corners( const Vector2D & top_left,
00180 const Vector2D & bottom_right )
00181 {
00182 return Rect2D( top_left, bottom_right );
00183 }
00184
00192 const
00193 Rect2D & assign( const double & left_x,
00194 const double & top_y,
00195 const double & length,
00196 const double & width )
00197 {
00198 M_top_left.assign( left_x, top_y );
00199 M_size.assign( length, width );
00200 return *this;
00201 }
00202
00210 const
00211 Rect2D & assign( const Vector2D & top_left,
00212 const double & length,
00213 const double & width )
00214 {
00215 M_top_left = top_left;
00216 M_size.assign( length, width );
00217 return *this;
00218 }
00219
00226 const
00227 Rect2D & assign( const Vector2D & top_left,
00228 const Size2D & size )
00229 {
00230 M_top_left = top_left;
00231 M_size = size;
00232 return *this;
00233 }
00234
00241 const
00242 Rect2D & setTopLeft( const double & x,
00243 const double & y )
00244 {
00245 M_top_left.assign( x, y );
00246 return *this;
00247 }
00248
00254 const
00255 Rect2D & setTopLeft( const Vector2D & point )
00256 {
00257 M_top_left = point;
00258 return *this;
00259 }
00260
00267 const
00268 Rect2D & setCenter( const Vector2D & point )
00269 {
00270 M_top_left.assign( point.x - M_size.length() * 0.5,
00271 point.y - M_size.width() * 0.5 );
00272 return *this;
00273 }
00274
00280 const
00281 Rect2D & setLength( const double & length )
00282 {
00283 M_size.setLength( length );
00284 return *this;
00285 }
00286
00292 const
00293 Rect2D & setWidth( const double & width )
00294 {
00295 M_size.setWidth( width );
00296 return *this;
00297 }
00298
00305 const
00306 Rect2D & setSize( const double & length,
00307 const double & width )
00308 {
00309 M_size.assign( length, width );
00310 return *this;
00311 }
00312
00318 const
00319 Rect2D & setSize( const Size2D & size )
00320 {
00321 M_size = size;
00322 return *this;
00323 }
00324
00330 bool contains( const Vector2D & point ) const
00331 {
00332 return ( left() <= point.x
00333 && point.x <= right()
00334 && top() <= point.y
00335 && point.y <= bottom() );
00336 }
00337
00342 const
00343 double & left() const
00344 {
00345 return M_top_left.x;
00346 }
00347
00352 double right() const
00353 {
00354 return left() + size().length();
00355 }
00356
00361 const
00362 double & top() const
00363 {
00364 return M_top_left.y;
00365 }
00366
00371 double bottom() const
00372 {
00373 return top() + size().width();
00374 }
00375
00380 double minX() const
00381 {
00382 return left();
00383 }
00384
00389 double maxX() const
00390 {
00391 return right();
00392 }
00393
00398 double minY() const
00399 {
00400 return top();
00401 }
00402
00407 double maxY() const
00408 {
00409 return bottom();
00410 }
00411
00416 const
00417 Size2D & size() const
00418 {
00419 return M_size;
00420 }
00421
00426 Vector2D center() const
00427 {
00428 return Vector2D( ( left() + right() ) * 0.5,
00429 ( top() + bottom() ) * 0.5 );
00430 }
00431
00436 const
00437 Vector2D & topLeft() const
00438 {
00439 return M_top_left;
00440 }
00441
00446 Vector2D topRight() const
00447 {
00448 return Vector2D( right(), top() );
00449 }
00450
00455 Vector2D bottomLeft() const
00456 {
00457 return Vector2D( left(), bottom() );
00458 }
00459
00464 Vector2D bottomRight() const
00465 {
00466 return Vector2D( right(), bottom() );
00467 }
00468
00473 Line2D leftEdge() const
00474 {
00475 return Line2D( topLeft(), bottomLeft() );
00476 }
00477
00482 Line2D rightEdge() const
00483 {
00484 return Line2D( topRight(), bottomRight() );
00485 }
00486
00491 Line2D topEdge() const
00492 {
00493 return Line2D( topLeft(), topRight() );
00494 }
00495
00500 Line2D bottomEdge() const
00501 {
00502 return Line2D( bottomLeft(), bottomRight() );
00503 }
00504
00512 int intersection( const Line2D & line,
00513 Vector2D * sol1,
00514 Vector2D * sol2 ) const;
00515
00523 int intersection( const Ray2D & ray,
00524 Vector2D * sol1,
00525 Vector2D * sol2 ) const;
00526
00534 int intersection( const Segment2D & segment,
00535 Vector2D * sol1,
00536 Vector2D * sol2 ) const;
00537
00538 };
00539
00540 }
00541
00542 #endif