Public Types | Public Member Functions | Public Attributes | Private Member Functions

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

Constructor.

Definition at line 36 of file rectangle.tpp.

{

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

Copy constructor.

Parameters:
thatRectangle to copy from.

Definition at line 48 of file rectangle.tpp.

  : position(that.position), width(that.width), height(that.height)
{

} // rectangle::rectangle() [copy constructor]
template<class T >
template<class U >
claw::math::rectangle< T >::rectangle ( const box_2d< U > &  that )

Constructor from a box.

Parameters:
thatThe box to copy from.

Definition at line 61 of file rectangle.tpp.

  : position(that.left(), that.top()), width(that.width()),
    height(that.height())
{

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

Constructor with initialization.

Parameters:
_xRectangle's X-coordinate.
_yRectangle's Y-coordinate.
_widthRectangle's width.
_heightRectangle's height.

Definition at line 78 of file rectangle.tpp.

  : position(_x, _y), width(_width), height(_height)
{

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

Constructor with initialization.

Parameters:
posThe position of the rectangle.
_widthRectangle's width.
_heightRectangle's height.

Definition at line 95 of file rectangle.tpp.

  : position(pos), width(_width), height(_height)
{
  
} // 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 
)

Constructor with initialization.

Parameters:
posThe position of the rectangle.
sizeThe size of the rectangle.

Definition at line 111 of file rectangle.tpp.

  : position(pos), width(size.x), height(size.y)
{
  
} // 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

Calculate the rectangle's area.

Definition at line 151 of file rectangle.tpp.

{
  return width * height;
} // rectangle::area()
template<class T >
claw::math::rectangle< T >::value_type claw::math::rectangle< T >::bottom (  ) const

Get the y-coordinate of the bottom edge.

Definition at line 263 of file rectangle.tpp.

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

{
  return position.y + height;
} // rectangle::bottom()
template<class T >
template<typename U >
claw::math::rectangle< U > claw::math::rectangle< T >::cast_value_type_to (  ) const

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 want 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.

{
  return claw::math::rectangle<U>
    ( position.cast_value_type_to<U>(), (U)width, (U)height );
} // rectangle::cast_value_type_to()
template<class T >
bool claw::math::rectangle< T >::includes ( const self_type r ) const

Tell if a rectangle is in a rectangle.

Parameters:
rThe supposed included rectangle.

Definition at line 175 of file rectangle.tpp.

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

{
  box_2d<value_type> his_box(r);

  return includes(his_box.first_point) && includes(his_box.second_point);
} // rectangle::includes() [rectangle]
template<class T >
bool claw::math::rectangle< T >::includes ( const coordinate_2d< value_type > &  p ) const

Tell if a point is in a rectangle.

Parameters:
pThe supposed included point.

Definition at line 163 of file rectangle.tpp.

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

{
  return (position.x <= p.x) && (right() >= p.x)
    && (position.y <= p.y) && (bottom() >= p.y);
} // rectangle::includes()
template<class T >
claw::math::rectangle< T > claw::math::rectangle< T >::intersection ( const self_type r ) const

Intersection of two rectangles.

Parameters:
rThe supposed intersecting rectangle.

Definition at line 203 of file rectangle.tpp.

Referenced by claw::graphic::image::fill(), claw::graphic::image::merge(), and claw::graphic::image::partial_copy().

{
  self_type result;

  if ( intersects(r) )
    {
      x_intersection(r, result);
      y_intersection(r, result);
    }

  return result;
} // rectangle::intersection()
template<class T >
bool claw::math::rectangle< T >::intersects ( const self_type r ) const

Tell if there is an intersection of two rectangles.

Parameters:
rThe 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::graphic::image::fill(), claw::graphic::image::merge(), and claw::graphic::image::partial_copy().

{
  return (right() >= r.position.x)
    && (r.right() >= position.x) 
    && (bottom() >= r.position.y)
    && (r.bottom() >= position.y);
} // rectangle::intersects()
template<class T >
claw::math::rectangle< T >::value_type claw::math::rectangle< T >::left (  ) const

Get the x-coordinate of the left edge.

Definition at line 241 of file rectangle.tpp.

{
  return position.x;
} // rectangle::left()
template<class T >
claw::math::rectangle< T >::value_type claw::math::rectangle< T >::right (  ) const

Get the x-coordinate of the right edge.

Definition at line 252 of file rectangle.tpp.

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

{
  return position.x + width;
} // 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 
)

set new position and size to the rectangle.

Parameters:
new_xNew x-coordinate.
new_yNew y-coordinate.
new_widthNew width.
new_heightNew height.

Definition at line 226 of file rectangle.tpp.

{
  position.x = new_x;
  position.y = new_y;
  width = new_width;
  height = new_height;
} // rectangle::set()
template<class T >
claw::math::coordinate_2d< typename claw::math::rectangle< T >::value_type > claw::math::rectangle< T >::size (  ) const

Get the size of the rectangle.

Definition at line 285 of file rectangle.tpp.

{
  return claw::math::coordinate_2d<value_type>(width, height);
} // rectangle::size()
template<class T >
claw::math::rectangle< T >::value_type claw::math::rectangle< T >::top (  ) const

Get the y-coordinate of the top edge.

Definition at line 274 of file rectangle.tpp.

{
  return position.y;
} // rectangle::top()
template<class T >
void claw::math::rectangle< T >::x_intersection ( const self_type r,
self_type result 
) const [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 >::x_intersection().

{
  if (position.x <= r.position.x)
    {
      result.position.x = r.position.x;

      if (right() >= r.right())
        result.width = r.width;
      else
        result.width = right() - r.position.x;
    }
  else
    r.x_intersection(*this, result);

} // rectangle::x_intersection()
template<class T >
void claw::math::rectangle< T >::y_intersection ( const self_type r,
self_type result 
) const [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 >::y_intersection().

{
  if (position.y <= r.position.y)
    {
      result.position.y = r.position.y;

      if (bottom() >= r.bottom())
        result.height = r.height;
      else
        result.height = bottom() - r.position.y;
    }
  else
    r.y_intersection(*this, result);

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