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

A class representing a rectangle by his x,y coordinates, width and height. More...

#include <rectangle.hpp>

List of all members.

Public Types

typedef T value_type
 The type of the values we store.
typedef rectangle< value_typeself_type
 The type of the current class.

Public Member Functions

 rectangle ()
 Constructor.
template<typename U >
 rectangle (const rectangle< U > &that)
 Copy constructor.
template<typename U >
 rectangle (const box_2d< U > &that)
 Constructor from a box.
 rectangle (const value_type &_x, const value_type &_y, const value_type &_width, const value_type &_height)
 Constructor with initialization.
template<typename U >
 rectangle (const coordinate_2d< U > &pos, const value_type &_width, const value_type &_height)
 Constructor with initialization.
template<typename U >
 rectangle (const coordinate_2d< U > &pos, const coordinate_2d< U > &size)
 Constructor with initialization.
template<typename U >
rectangle< U > cast_value_type_to () const
 Get a copy of the rectangle by converting its members to a given type.
value_type area () const
 Calculate the rectangle's area.
bool includes (const coordinate_2d< value_type > &p) const
 Tell if a point is in a rectangle.
bool includes (const self_type &r) const
 Tell if a rectangle is in a rectangle.
bool intersects (const self_type &r) const
 Tell if there is an intersection of two rectangles.
self_type intersection (const self_type &r) const
 Intersection of two rectangles.
void set (const value_type &new_x, const value_type &new_y, const value_type &new_width, const value_type &new_height)
 set new position and size to the rectangle.
value_type left () const
 Get the x-coordinate of the left edge.
value_type right () const
 Get the x-coordinate of the right edge.
value_type bottom () const
 Get the y-coordinate of the bottom edge.
value_type top () const
 Get the y-coordinate of the top edge.
coordinate_2d< value_typesize () const
 Get the size of the rectangle.

Public Attributes

coordinate_2d< value_typeposition
 value_typeop left coordinates.
value_type width
 Width.
value_type height
 Height.

Private Member Functions

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

Detailed Description

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

A class representing a rectangle by his x,y coordinates, width and height.

This class considers that the y-axis increases from the top to the bottom (like a screen).

Author:
Julien Jorge

Definition at line 51 of file rectangle.hpp.


Member Typedef Documentation

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

The type of the current class.

Definition at line 58 of file rectangle.hpp.

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

The type of the values we store.

Definition at line 55 of file rectangle.hpp.


Constructor & Destructor Documentation

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

Constructor.

Definition at line 36 of file rectangle.tpp.

00037 {
00038 
00039 } // rectangle::rectangle() [constructor]

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

Copy constructor.

Parameters:
that Rectangle to copy from.

Definition at line 48 of file rectangle.tpp.

00049   : position(that.position), width(that.width), height(that.height)
00050 {
00051 
00052 } // rectangle::rectangle() [copy constructor]

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

Constructor from a box.

Parameters:
that The box to copy from.

Definition at line 61 of file rectangle.tpp.

00062   : position(that.left(), that.top()), width(that.width()),
00063     height(that.height())
00064 {
00065 
00066 } // rectangle::rectangle() [copy constructor]

template<class T >
claw::math::rectangle< T >::rectangle ( const value_type _x,
const value_type _y,
const value_type _width,
const value_type _height 
) [inline]

Constructor with initialization.

Parameters:
_x Rectangle's X-coordinate.
_y Rectangle's Y-coordinate.
_width Rectangle's width.
_height Rectangle's height.

Definition at line 78 of file rectangle.tpp.

00080   : position(_x, _y), width(_width), height(_height)
00081 {
00082 
00083 } // rectangle::rectangle() [constructor with values]

template<class T >
template<typename U >
claw::math::rectangle< T >::rectangle ( const coordinate_2d< U > &  pos,
const value_type _width,
const value_type _height 
) [inline]

Constructor with initialization.

Parameters:
pos The position of the rectangle.
_width Rectangle's width.
_height Rectangle's height.

Definition at line 95 of file rectangle.tpp.

00097   : position(pos), width(_width), height(_height)
00098 {
00099   
00100 } // rectangle::rectangle() [constructor from position and size]

template<class T >
template<typename U >
claw::math::rectangle< T >::rectangle ( const coordinate_2d< U > &  pos,
const coordinate_2d< U > &  size 
) [inline]

Constructor with initialization.

Parameters:
pos The position of the rectangle.
size The size of the rectangle.

Definition at line 111 of file rectangle.tpp.

00112   : position(pos), width(size.x), height(size.y)
00113 {
00114   
00115 } // rectangle::rectangle() [constructor from position and size]


Member Function Documentation

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

Calculate the rectangle's area.

Definition at line 151 of file rectangle.tpp.

References claw::math::rectangle< T >::height, and claw::math::rectangle< T >::width.

00152 {
00153   return width * height;
00154 } // rectangle::area()

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

Get the y-coordinate of the bottom edge.

Definition at line 263 of file rectangle.tpp.

References claw::math::rectangle< T >::height, claw::math::rectangle< T >::position, and claw::math::coordinate_2d< T >::y.

Referenced by claw::math::rectangle< T >::includes(), claw::math::rectangle< T >::intersects(), and claw::math::rectangle< T >::y_intersection().

00264 {
00265   return position.y + height;
00266 } // rectangle::bottom()

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

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

Consider the following code:

rectangle<float> a;

...

rectangle<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 rectangle 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 139 of file rectangle.tpp.

References claw::math::coordinate_2d< T >::cast_value_type_to(), claw::math::rectangle< T >::height, claw::math::rectangle< T >::position, and claw::math::rectangle< T >::width.

00140 {
00141   return claw::math::rectangle<U>
00142     ( position.cast_value_type_to<U>(), (U)width, (U)height );
00143 } // rectangle::cast_value_type_to()

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

Tell if a rectangle is in a rectangle.

Parameters:
r The supposed included rectangle.

Definition at line 175 of file rectangle.tpp.

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

00176 {
00177   box_2d<value_type> his_box(r);
00178 
00179   return includes(his_box.first_point) && includes(his_box.second_point);
00180 } // rectangle::includes() [rectangle]

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

Tell if a point is in a rectangle.

Parameters:
p The supposed included point.

Definition at line 163 of file rectangle.tpp.

References claw::math::rectangle< T >::bottom(), claw::math::rectangle< T >::position, claw::math::rectangle< T >::right(), claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

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

00164 {
00165   return (position.x <= p.x) && (right() >= p.x)
00166     && (position.y <= p.y) && (bottom() >= p.y);
00167 } // rectangle::includes()

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

Intersection of two rectangles.

Parameters:
r The supposed intersecting rectangle.

Definition at line 203 of file rectangle.tpp.

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

Referenced by claw::graphic::image::partial_copy().

00204 {
00205   self_type result;
00206 
00207   if ( intersects(r) )
00208     {
00209       x_intersection(r, result);
00210       y_intersection(r, result);
00211     }
00212 
00213   return result;
00214 } // rectangle::intersection()

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

Tell if there is an intersection of two rectangles.

Parameters:
r The supposed intersecting rectangle.

Definition at line 188 of file rectangle.tpp.

References claw::math::rectangle< T >::bottom(), claw::math::rectangle< T >::position, claw::math::rectangle< T >::right(), claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.

Referenced by claw::math::rectangle< T >::intersection(), and claw::graphic::image::partial_copy().

00189 {
00190   return (right() >= r.position.x)
00191     && (r.right() >= position.x) 
00192     && (bottom() >= r.position.y)
00193     && (r.bottom() >= position.y);
00194 } // rectangle::intersects()

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

Get the x-coordinate of the left edge.

Definition at line 241 of file rectangle.tpp.

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

00242 {
00243   return position.x;
00244 } // rectangle::left()

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

Get the x-coordinate of the right edge.

Definition at line 252 of file rectangle.tpp.

References claw::math::rectangle< T >::position, claw::math::rectangle< T >::width, and claw::math::coordinate_2d< T >::x.

Referenced by claw::math::rectangle< T >::includes(), claw::math::rectangle< T >::intersects(), and claw::math::rectangle< T >::x_intersection().

00253 {
00254   return position.x + width;
00255 } // rectangle::right()

template<class T >
void claw::math::rectangle< T >::set ( const value_type new_x,
const value_type new_y,
const value_type new_width,
const value_type new_height 
) [inline]

set new position and size to the rectangle.

Parameters:
new_x New x-coordinate.
new_y New y-coordinate.
new_width New width.
new_height New height.

Definition at line 226 of file rectangle.tpp.

00228 {
00229   position.x = new_x;
00230   position.y = new_y;
00231   width = new_width;
00232   height = new_height;
00233 } // rectangle::set()

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

Get the size of the rectangle.

Definition at line 285 of file rectangle.tpp.

References claw::math::rectangle< T >::height, and claw::math::rectangle< T >::width.

00286 {
00287   return claw::math::coordinate_2d<value_type>(width, height);
00288 } // rectangle::size()

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

Get the y-coordinate of the top edge.

Definition at line 274 of file rectangle.tpp.

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

00275 {
00276   return position.y;
00277 } // rectangle::top()

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

X-intersection of two rectangles.

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

Definition at line 298 of file rectangle.tpp.

References claw::math::rectangle< T >::position, claw::math::rectangle< T >::right(), claw::math::rectangle< T >::width, claw::math::coordinate_2d< T >::x, and claw::math::rectangle< T >::x_intersection().

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

00299 {
00300   if (position.x <= r.position.x)
00301     {
00302       result.position.x = r.position.x;
00303 
00304       if (right() >= r.right())
00305         result.width = r.width;
00306       else
00307         result.width = right() - r.position.x;
00308     }
00309   else
00310     r.x_intersection(*this, result);
00311 
00312 } // rectangle::x_intersection()

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

Y-intersection of two rectangles.

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

Definition at line 322 of file rectangle.tpp.

References claw::math::rectangle< T >::bottom(), claw::math::rectangle< T >::height, claw::math::rectangle< T >::position, claw::math::coordinate_2d< T >::y, and claw::math::rectangle< T >::y_intersection().

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

00323 {
00324   if (position.y <= r.position.y)
00325     {
00326       result.position.y = r.position.y;
00327 
00328       if (bottom() >= r.bottom())
00329         result.height = r.height;
00330       else
00331         result.height = bottom() - r.position.y;
00332     }
00333   else
00334     r.y_intersection(*this, result);
00335 
00336 } // rectangle::y_intersection()


Member Data Documentation

template<class T>
value_type claw::math::rectangle< T >::height
template<class T>
value_type claw::math::rectangle< T >::width

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