claw::math::box_2d< T > Class Template Reference

A rectangle represented by two points in a 2D space. More...

#include <box_2d.hpp>

List of all members.

Public Types

typedef T value_type
 The type of the values we store.
typedef coordinate_2d< value_typepoint_type
 The type of the coordinates of the points representing the corners.
typedef box_2d< value_typeself_type
 The type of the current class.

Public Member Functions

 box_2d ()
 Constructor.
 box_2d (const self_type &that)
 Copy constructor.
 box_2d (const rectangle< value_type > &that)
 Constructor from a rectangle.
 box_2d (const point_type &p1, const point_type &p2)
 Constructor from two points.
 box_2d (const value_type &x1, const value_type &y1, const value_type &x2, const value_type &y2)
 Constructor with initialization.
void set (const value_type &x1, const value_type &y1, const value_type &x2, const value_type &y2)
 Set the coordinates of the two points.
template<typename U >
box_2d< U > cast_value_type_to () const
 Get a copy of the box by converting its members to a given type.
value_type area () const
 Calculate the box's area.
bool includes (const coordinate_2d< value_type > &p) const
 Tell if a point is in a box.
bool includes (const self_type &r) const
 Tell if a box_2d is in a box_2d.
bool intersects (const self_type &r) const
 Tell if there is an intersection of two boxes.
self_type intersection (const self_type &r) const
 Intersection of two box_2ds.
value_type top () const
 Get the y-coordinate of the top edge.
value_type bottom () const
 Get the y-coordinate of the bottom edge.
value_type left () const
 Get the x-coordinate of the left edge.
value_type right () const
 Get the x-coordinate of the right edge.
point_type top_left () const
 Get the coordinate of the top-left corner.
point_type top_right () const
 Get the coordinate of the top-right corner.
point_type bottom_left () const
 Get the coordinate of the bottom-left corner.
point_type bottom_right () const
 Get the coordinate of the bottom-right corner.
void top (const value_type &p)
 Move the top edge at a given position.
void bottom (const value_type &p)
 Move the bottom edge at a given position.
void left (const value_type &p)
 Move the left edge at a given position.
void right (const value_type &p)
 Move the right edge at a given position.
void top_left (const coordinate_2d< value_type > &p)
 Move the top-left corner at a given position.
void top_right (const coordinate_2d< value_type > &p)
 Move the top-right corner at a given position.
void bottom_left (const coordinate_2d< value_type > &p)
 Move the bottom-left corner at a given position.
void bottom_right (const coordinate_2d< value_type > &p)
 Move the bottom-right corner at a given position.
void shift_x (const value_type &d)
 Shift the position of the box on the x-axis.
void shift_y (const value_type &d)
 Shift the position of the box on the y-axis.
value_type width () const
 Return box' width.
value_type height () const
 Return box' height.
coordinate_2d< value_typesize () const
 Get the size of the box_2d.
bool operator== (const self_type &vect) const
 Equality operator.
bool operator!= (const self_type &vect) const
 Difference operator.
self_type operator+ (const point_type &vect) const
 Translation.
self_type operator- (const point_type &vect) const
 Translation.
self_typeoperator+= (const point_type &vect)
 Translation.
self_typeoperator-= (const point_type &vect)
 Translation.

Public Attributes

point_type first_point
 The first of the two points, representing one corner.
point_type second_point
 The second of the two points, representing an other corner.

Private Member Functions

void x_intersection (const self_type &r, self_type &result) const
 X-intersection of two box_2ds.
void y_intersection (const self_type &r, self_type &result) const
 Y-intersection of two box_2ds.

Detailed Description

template<class T>
class claw::math::box_2d< T >

A rectangle represented by two points in a 2D space.

Author:
Julien Jorge

Definition at line 46 of file box_2d.hpp.


Member Typedef Documentation

template<class T>
typedef coordinate_2d<value_type> claw::math::box_2d< T >::point_type

The type of the coordinates of the points representing the corners.

Definition at line 54 of file box_2d.hpp.

template<class T>
typedef box_2d<value_type> claw::math::box_2d< T >::self_type

The type of the current class.

Definition at line 57 of file box_2d.hpp.

template<class T>
typedef T claw::math::box_2d< T >::value_type

The type of the values we store.

Definition at line 50 of file box_2d.hpp.


Constructor & Destructor Documentation

template<class T >
claw::math::box_2d< T >::box_2d (  )  [inline]

Constructor.

Definition at line 38 of file box_2d.tpp.

00039 {
00040 
00041 } // box_2d::box_2d() [constructor]

template<class T >
claw::math::box_2d< T >::box_2d ( const self_type that  )  [inline]

Copy constructor.

Parameters:
that Box to copy from.

Definition at line 49 of file box_2d.tpp.

00050   : first_point(that.first_point), second_point(that.second_point)
00051 {
00052 
00053 } // box_2d::box_2d() [copy constructor]

template<class T >
claw::math::box_2d< T >::box_2d ( const rectangle< value_type > &  that  )  [inline]

Constructor from a rectangle.

Parameters:
that Rectangle to copy from.

Definition at line 61 of file box_2d.tpp.

00062   : first_point(that.position),
00063     second_point(that.right(), that.bottom())
00064 {
00065 
00066 } // box_2d::box_2d() [constructor from rectangle]

template<class T >
claw::math::box_2d< T >::box_2d ( const point_type p1,
const point_type p2 
) [inline]

Constructor from two points.

Parameters:
p1 The first point.
p2 The second point.

Definition at line 75 of file box_2d.tpp.

00076   : first_point(p1), second_point(p2)
00077 {
00078 
00079 } // box_2d::box_2d() [constructor from coordinates]

template<class T >
claw::math::box_2d< T >::box_2d ( const value_type x1,
const value_type y1,
const value_type x2,
const value_type y2 
) [inline]

Constructor with initialization.

Parameters:
x1 X-coordinate of the first point.
y1 Y-coordinate of the first point.
x2 X-coordinate of the second point.
y2 Y-coordinate of the second point.

Definition at line 90 of file box_2d.tpp.

00092   : first_point(x1, y1), second_point(x2, y2)
00093 {
00094 
00095 } // box_2d::box_2d() [constructor with values]


Member Function Documentation

template<class T >
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::area (  )  const [inline]

Calculate the box's area.

Definition at line 148 of file box_2d.tpp.

References claw::math::box_2d< T >::height(), and claw::math::box_2d< T >::width().

00149 {
00150   return width() * height();
00151 } // box_2d::area()

template<class T >
void claw::math::box_2d< T >::bottom ( const value_type p  )  [inline]

Move the bottom edge at a given position.

Parameters:
p The position.

Definition at line 312 of file box_2d.tpp.

References claw::math::box_2d< T >::bottom(), and claw::math::box_2d< T >::shift_y().

00313 {
00314   shift_y(p - bottom());
00315 } // box_2d::bottom()

template<class T >
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::bottom (  )  const [inline]
template<class T >
void claw::math::box_2d< T >::bottom_left ( const coordinate_2d< value_type > &  p  )  [inline]

Move the bottom-left corner at a given position.

Parameters:
p The position.

Definition at line 372 of file box_2d.tpp.

References claw::math::box_2d< T >::bottom(), claw::math::box_2d< T >::left(), claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

00373 {
00374   bottom(p.y);
00375   left(p.x);
00376 } // box_2d::bottom_left()

template<class T >
claw::math::box_2d< T >::point_type claw::math::box_2d< T >::bottom_left (  )  const [inline]

Get the coordinate of the bottom-left corner.

Definition at line 279 of file box_2d.tpp.

References claw::math::box_2d< T >::bottom(), and claw::math::box_2d< T >::left().

00280 {
00281   return point_type(left(), bottom());
00282 } // box_2d::bottom_left()

template<class T >
void claw::math::box_2d< T >::bottom_right ( const coordinate_2d< value_type > &  p  )  [inline]

Move the bottom-right corner at a given position.

Parameters:
p The position.

Definition at line 385 of file box_2d.tpp.

References claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

00386 {
00387   bottom(p.y);
00388   right(p.x);
00389 } // box_2d::bottom_right()

template<class T >
claw::math::box_2d< T >::point_type claw::math::box_2d< T >::bottom_right (  )  const [inline]

Get the coordinate of the bottom-right corner.

Definition at line 290 of file box_2d.tpp.

References claw::math::box_2d< T >::bottom(), and claw::math::box_2d< T >::right().

00291 {
00292   return point_type(right(), bottom());
00293 } // box_2d::bottom_right()

template<class T >
template<typename U >
claw::math::box_2d< U > claw::math::box_2d< T >::cast_value_type_to (  )  const [inline]

Get a copy of the box by converting its members to a given type.

Consider the following code:

box_2d<float> a;

...

box_2d<int> b(a);

The copy constructor will be called, and your compiler should print some warnings in your console. These warnings have a meaning, so we don't wan't to make them disapear by adding explicit type conversion inside the box_2d class nor adding a cast operator that will be used silently by the compiler.

If you really want to convert the type, this method will explicitly cast the member variables.

Definition at line 136 of file box_2d.tpp.

References claw::math::coordinate_2d< T >::cast_value_type_to(), claw::math::box_2d< T >::first_point, and claw::math::box_2d< T >::second_point.

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()

template<class T >
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::height (  )  const [inline]

Return box' height.

Definition at line 521 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, claw::math::box_2d< T >::second_point, and claw::math::coordinate_2d< T >::y.

Referenced by claw::math::box_2d< T >::area(), and claw::math::box_2d< T >::size().

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()

template<class T >
bool claw::math::box_2d< T >::includes ( const self_type r  )  const [inline]

Tell if a box_2d is in a box_2d.

Parameters:
r The supposed included box_2d.

Definition at line 172 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, claw::math::box_2d< T >::includes(), and claw::math::box_2d< T >::second_point.

00173 {
00174   return includes(r.first_point) && includes(r.second_point);
00175 } // box_2d::includes()

template<class T >
bool claw::math::box_2d< T >::includes ( const coordinate_2d< value_type > &  p  )  const [inline]

Tell if a point is in a box.

Parameters:
p The supposed included point.

Definition at line 160 of file box_2d.tpp.

References claw::math::box_2d< T >::bottom(), claw::math::box_2d< T >::left(), claw::math::box_2d< T >::right(), claw::math::box_2d< T >::top(), claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

Referenced by claw::math::box_2d< T >::includes().

00161 {
00162   return (left() <= p.x) && (right() >= p.x)
00163     && (bottom() <= p.y) && (top() >= p.y);
00164 } // box_2d::includes()

template<class T >
claw::math::box_2d< T > claw::math::box_2d< T >::intersection ( const self_type r  )  const [inline]

Intersection of two box_2ds.

Parameters:
r The supposed intersecting box_2d.

Definition at line 196 of file box_2d.tpp.

References CLAW_PRECOND, claw::math::box_2d< T >::intersects(), claw::math::box_2d< T >::x_intersection(), and claw::math::box_2d< T >::y_intersection().

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()

template<class T >
bool claw::math::box_2d< T >::intersects ( const self_type r  )  const [inline]

Tell if there is an intersection of two boxes.

Parameters:
r The supposed intersecting box.

Definition at line 183 of file box_2d.tpp.

References claw::math::box_2d< T >::bottom(), claw::math::box_2d< T >::left(), claw::math::box_2d< T >::right(), and claw::math::box_2d< T >::top().

Referenced by claw::math::box_2d< T >::intersection().

00184 {
00185   return (right() >= r.left()) && (r.right() >= left()) 
00186     && (top() >= r.bottom()) && (r.top() >= bottom());
00187 } // box_2d::intersects()

template<class T >
void claw::math::box_2d< T >::left ( const value_type p  )  [inline]

Move the left edge at a given position.

Parameters:
p The position.

Definition at line 323 of file box_2d.tpp.

References claw::math::box_2d< T >::left(), and claw::math::box_2d< T >::shift_x().

00324 {
00325   shift_x(p - left());
00326 } // box_2d::left()

template<class T >
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::left (  )  const [inline]
template<class T >
bool claw::math::box_2d< T >::operator!= ( const self_type that  )  const [inline]

Difference operator.

Parameters:
that Box to compare to.

Definition at line 446 of file box_2d.tpp.

00447 {
00448   return !( *this == that );
00449 } // box_2d::operator!=()

template<class T >
claw::math::box_2d< T > claw::math::box_2d< T >::operator+ ( const point_type vect  )  const [inline]

Translation.

Parameters:
vect The vector to add to points.

Definition at line 458 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, and claw::math::box_2d< T >::second_point.

00459 {
00460   return self_type( first_point + vect, second_point + vect );
00461 } // box_2d::operator+()

template<class T >
claw::math::box_2d< T > & claw::math::box_2d< T >::operator+= ( const point_type vect  )  [inline]

Translation.

Parameters:
vect The vector to add to points.

Definition at line 482 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, and claw::math::box_2d< T >::second_point.

00483 {
00484   first_point += vect;
00485   second_point += vect;
00486 } // box_2d::operator+=()

template<class T >
claw::math::box_2d< T > claw::math::box_2d< T >::operator- ( const point_type vect  )  const [inline]

Translation.

Parameters:
vect The vector to substract to points.

Definition at line 470 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, and claw::math::box_2d< T >::second_point.

00471 {
00472   return self_type( first_point - vect, second_point - vect );
00473 } // box_2d::operator-()

template<class T >
claw::math::box_2d< T > & claw::math::box_2d< T >::operator-= ( const point_type vect  )  [inline]

Translation.

Parameters:
vect The vector to substract to points.

Definition at line 495 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, and claw::math::box_2d< T >::second_point.

00496 {
00497   first_point -= vect;
00498   second_point -= vect;
00499 } // box_2d::operator-=()

template<class T >
bool claw::math::box_2d< T >::operator== ( const self_type that  )  const [inline]

Equality operator.

Parameters:
that Box to compare to.

Definition at line 432 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, and claw::math::box_2d< T >::second_point.

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==()

template<class T >
void claw::math::box_2d< T >::right ( const value_type p  )  [inline]

Move the right edge at a given position.

Parameters:
p The position.

Definition at line 334 of file box_2d.tpp.

References claw::math::box_2d< T >::right(), and claw::math::box_2d< T >::shift_x().

00335 {
00336   shift_x(p - right());
00337 } // box_2d::right()

template<class T >
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::right (  )  const [inline]
template<class T >
void claw::math::box_2d< T >::set ( const value_type x1,
const value_type y1,
const value_type x2,
const value_type y2 
) [inline]

Set the coordinates of the two points.

Parameters:
x1 X-coordinate of the first point.
y1 Y-coordinate of the first point.
x2 X-coordinate of the second point.
y2 Y-coordinate of the second point.

Definition at line 107 of file box_2d.tpp.

00109 {
00110   first_point.set(x1, y1);
00111   second_point.set(x2, y2);
00112 } // box_2d::set()

template<class T >
void claw::math::box_2d< T >::shift_x ( const value_type d  )  [inline]

Shift the position of the box on the x-axis.

Parameters:
d The movement length.

Definition at line 397 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, claw::math::box_2d< T >::second_point, and claw::math::coordinate_2d< T >::x.

Referenced by claw::math::box_2d< T >::left(), and claw::math::box_2d< T >::right().

00398 {
00399   first_point.x += d;
00400   second_point.x += d;
00401 } // box_2d::shift_x()

template<class T >
void claw::math::box_2d< T >::shift_y ( const value_type d  )  [inline]

Shift the position of the box on the y-axis.

Parameters:
d The movement length.

Definition at line 409 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, claw::math::box_2d< T >::second_point, and claw::math::coordinate_2d< T >::y.

Referenced by claw::math::box_2d< T >::bottom(), and claw::math::box_2d< T >::top().

00410 {
00411   first_point.y += d;
00412   second_point.y += d;
00413 } // box_2d::shift_y()

template<class T >
claw::math::coordinate_2d< typename claw::math::box_2d< T >::value_type > claw::math::box_2d< T >::size (  )  const [inline]

Get the size of the box_2d.

Definition at line 421 of file box_2d.tpp.

References claw::math::box_2d< T >::height(), and claw::math::box_2d< T >::width().

00422 {
00423   return claw::math::coordinate_2d<value_type>(width(), height());
00424 } // box_2d::size()

template<class T >
void claw::math::box_2d< T >::top ( const value_type p  )  [inline]

Move the top edge at a given position.

Parameters:
p The position.

Definition at line 301 of file box_2d.tpp.

References claw::math::box_2d< T >::shift_y(), and claw::math::box_2d< T >::top().

00302 {
00303   shift_y(p - top());
00304 } // box_2d::top()

template<class T >
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::top (  )  const [inline]
template<class T >
void claw::math::box_2d< T >::top_left ( const coordinate_2d< value_type > &  p  )  [inline]

Move the top-left corner at a given position.

Parameters:
p The position.

Definition at line 346 of file box_2d.tpp.

References claw::math::box_2d< T >::left(), claw::math::box_2d< T >::top(), claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

00347 {
00348   top(p.y);
00349   left(p.x);
00350 } // box_2d::top_left()

template<class T >
claw::math::box_2d< T >::point_type claw::math::box_2d< T >::top_left (  )  const [inline]

Get the coordinate of the top-left corner.

Definition at line 257 of file box_2d.tpp.

References claw::math::box_2d< T >::left(), and claw::math::box_2d< T >::top().

00258 {
00259   return point_type(left(), top());
00260 } // box_2d::top_left()

template<class T >
void claw::math::box_2d< T >::top_right ( const coordinate_2d< value_type > &  p  )  [inline]

Move the top-right corner at a given position.

Parameters:
p The position.

Definition at line 359 of file box_2d.tpp.

References claw::math::box_2d< T >::right(), claw::math::box_2d< T >::top(), claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

00360 {
00361   top(p.y);
00362   right(p.x);
00363 } // box_2d::top_right()

template<class T >
claw::math::box_2d< T >::point_type claw::math::box_2d< T >::top_right (  )  const [inline]

Get the coordinate of the top-right corner.

Definition at line 268 of file box_2d.tpp.

References claw::math::box_2d< T >::right(), and claw::math::box_2d< T >::top().

00269 {
00270   return point_type(right(), top());
00271 } // box_2d::top_right()

template<class T >
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::width (  )  const [inline]

Return box' width.

Definition at line 507 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, claw::math::box_2d< T >::second_point, and claw::math::coordinate_2d< T >::x.

Referenced by claw::math::box_2d< T >::area(), and claw::math::box_2d< T >::size().

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()

template<class T >
void claw::math::box_2d< T >::x_intersection ( const self_type r,
self_type result 
) const [inline, private]

X-intersection of two box_2ds.

Precondition:
There is an intersection between this and r.
Postcondition:
result's x and width fields are filled.

Definition at line 537 of file box_2d.tpp.

References claw::math::box_2d< T >::first_point, claw::math::box_2d< T >::left(), claw::math::box_2d< T >::right(), claw::math::box_2d< T >::second_point, claw::math::coordinate_2d< T >::x, and claw::math::box_2d< T >::x_intersection().

Referenced by claw::math::box_2d< T >::intersection(), and claw::math::box_2d< T >::x_intersection().

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()

template<class T >
void claw::math::box_2d< T >::y_intersection ( const self_type r,
self_type result 
) const [inline, private]

Y-intersection of two box_2ds.

Precondition:
There is an intersection between this and r.
Postcondition:
result's y and height fields are filled.

Definition at line 560 of file box_2d.tpp.

References claw::math::box_2d< T >::bottom(), claw::math::box_2d< T >::first_point, claw::math::box_2d< T >::second_point, claw::math::box_2d< T >::top(), claw::math::coordinate_2d< T >::y, and claw::math::box_2d< T >::y_intersection().

Referenced by claw::math::box_2d< T >::intersection(), and claw::math::box_2d< T >::y_intersection().

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()


Member Data Documentation

template<class T>
point_type claw::math::box_2d< T >::first_point
template<class T>
point_type claw::math::box_2d< T >::second_point

The documentation for this class was generated from the following files:

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