A rectangle represented by two points in a 2D space. More...
#include <box_2d.hpp>
Public Types | |
typedef T | value_type |
The type of the values we store. | |
typedef coordinate_2d< value_type > | point_type |
The type of the coordinates of the points representing the corners. | |
typedef box_2d< value_type > | self_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. | |
self_type | join (const self_type &r) const |
Join two box_2ds. | |
bool | empty () const |
Tell if the box has a dimension equal to zero. | |
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_type > | size () 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_type & | operator+= (const point_type &vect) |
Translation. | |
self_type & | operator-= (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. |
A rectangle represented by two points in a 2D space.
Definition at line 46 of file box_2d.hpp.
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.
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.
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.
claw::math::box_2d< T >::box_2d | ( | ) |
claw::math::box_2d< T >::box_2d | ( | const self_type & | that ) |
Copy constructor.
that | Box to copy from. |
Definition at line 49 of file box_2d.tpp.
: first_point(that.first_point), second_point(that.second_point) { } // box_2d::box_2d() [copy constructor]
claw::math::box_2d< T >::box_2d | ( | const rectangle< value_type > & | that ) |
Constructor from a rectangle.
that | Rectangle to copy from. |
Definition at line 61 of file box_2d.tpp.
: first_point(that.position), second_point(that.right(), that.bottom()) { } // box_2d::box_2d() [constructor from rectangle]
claw::math::box_2d< T >::box_2d | ( | const point_type & | p1, |
const point_type & | p2 | ||
) |
Constructor from two points.
p1 | The first point. |
p2 | The second point. |
Definition at line 75 of file box_2d.tpp.
: first_point(p1), second_point(p2) { } // box_2d::box_2d() [constructor from coordinates]
claw::math::box_2d< T >::box_2d | ( | const value_type & | x1, |
const value_type & | y1, | ||
const value_type & | x2, | ||
const value_type & | y2 | ||
) |
Constructor with initialization.
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.
: first_point(x1, y1), second_point(x2, y2) { } // box_2d::box_2d() [constructor with values]
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::area | ( | ) | const |
Calculate the box's area.
Definition at line 148 of file box_2d.tpp.
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::bottom | ( | ) | const |
Get the y-coordinate of the bottom edge.
Definition at line 250 of file box_2d.tpp.
Referenced by claw::math::box_2d< T >::intersects(), claw::math::box_2d< T >::join(), claw::math::box_2d< T >::operator==(), and claw::math::box_2d< T >::y_intersection().
{ return (first_point.y < second_point.y) ? first_point.y : second_point.y; } // box_2d::bottom()
void claw::math::box_2d< T >::bottom | ( | const value_type & | p ) |
Move the bottom edge at a given position.
p | The position. |
Definition at line 336 of file box_2d.tpp.
claw::math::box_2d< T >::point_type claw::math::box_2d< T >::bottom_left | ( | ) | const |
Get the coordinate of the bottom-left corner.
Definition at line 303 of file box_2d.tpp.
{ return point_type(left(), bottom()); } // box_2d::bottom_left()
void claw::math::box_2d< T >::bottom_left | ( | const coordinate_2d< value_type > & | p ) |
Move the bottom-left corner at a given position.
p | The position. |
Definition at line 396 of file box_2d.tpp.
References claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.
claw::math::box_2d< T >::point_type claw::math::box_2d< T >::bottom_right | ( | ) | const |
Get the coordinate of the bottom-right corner.
Definition at line 314 of file box_2d.tpp.
{ return point_type(right(), bottom()); } // box_2d::bottom_right()
void claw::math::box_2d< T >::bottom_right | ( | const coordinate_2d< value_type > & | p ) |
Move the bottom-right corner at a given position.
p | The position. |
Definition at line 409 of file box_2d.tpp.
References claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.
claw::math::box_2d< U > claw::math::box_2d< T >::cast_value_type_to | ( | ) | const |
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.
{ return claw::math::box_2d<U> ( first_point.cast_value_type_to<U>(), second_point.cast_value_type_to<U>() ); } // box_2d::cast_value_type_to()
bool claw::math::box_2d< T >::empty | ( | ) | const |
Tell if the box has a dimension equal to zero.
Definition at line 230 of file box_2d.tpp.
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::height | ( | ) | const |
Return box' height.
Definition at line 543 of file box_2d.tpp.
{ if (first_point.y > second_point.y) return first_point.y - second_point.y; else return second_point.y - first_point.y; } // box_2d::height()
bool claw::math::box_2d< T >::includes | ( | const coordinate_2d< value_type > & | p ) | const |
Tell if a point is in a box.
p | The supposed included point. |
Definition at line 160 of file box_2d.tpp.
References claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.
bool claw::math::box_2d< T >::includes | ( | const self_type & | r ) | const |
Tell if a box_2d is in a box_2d.
r | The supposed included box_2d. |
Definition at line 172 of file box_2d.tpp.
References claw::math::box_2d< T >::first_point, and claw::math::box_2d< T >::second_point.
claw::math::box_2d< T > claw::math::box_2d< T >::intersection | ( | const self_type & | r ) | const |
Intersection of two box_2ds.
r | The supposed intersecting box_2d. |
Definition at line 196 of file box_2d.tpp.
References CLAW_PRECOND.
{ CLAW_PRECOND( intersects(r) ); self_type result; if ( intersects(r) ) { x_intersection(r, result); y_intersection(r, result); } return result; } // box_2d::intersection()
bool claw::math::box_2d< T >::intersects | ( | const self_type & | r ) | const |
Tell if there is an intersection of two boxes.
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().
claw::math::box_2d< T > claw::math::box_2d< T >::join | ( | const self_type & | r ) | const |
Join two box_2ds.
r | The box to join with. |
Definition at line 218 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().
void claw::math::box_2d< T >::left | ( | const value_type & | p ) |
Move the left edge at a given position.
p | The position. |
Definition at line 347 of file box_2d.tpp.
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::left | ( | ) | const |
Get the x-coordinate of the left edge.
Definition at line 260 of file box_2d.tpp.
Referenced by claw::math::box_2d< T >::intersects(), claw::math::box_2d< T >::join(), claw::math::box_2d< T >::operator==(), and claw::math::box_2d< T >::x_intersection().
{ return (first_point.x < second_point.x) ? first_point.x : second_point.x; } // box_2d::left()
bool claw::math::box_2d< T >::operator!= | ( | const self_type & | that ) | const |
Difference operator.
that | Box to compare to. |
Definition at line 468 of file box_2d.tpp.
{ return !( *this == that ); } // box_2d::operator!=()
claw::math::box_2d< T > claw::math::box_2d< T >::operator+ | ( | const point_type & | vect ) | const |
Translation.
vect | The vector to add to points. |
Definition at line 480 of file box_2d.tpp.
{ return self_type( first_point + vect, second_point + vect ); } // box_2d::operator+()
claw::math::box_2d< T > & claw::math::box_2d< T >::operator+= | ( | const point_type & | vect ) |
Translation.
vect | The vector to add to points. |
Definition at line 504 of file box_2d.tpp.
{ first_point += vect; second_point += vect; } // box_2d::operator+=()
claw::math::box_2d< T > claw::math::box_2d< T >::operator- | ( | const point_type & | vect ) | const |
Translation.
vect | The vector to substract to points. |
Definition at line 492 of file box_2d.tpp.
{ return self_type( first_point - vect, second_point - vect ); } // box_2d::operator-()
claw::math::box_2d< T > & claw::math::box_2d< T >::operator-= | ( | const point_type & | vect ) |
Translation.
vect | The vector to substract to points. |
Definition at line 517 of file box_2d.tpp.
{ first_point -= vect; second_point -= vect; } // box_2d::operator-=()
bool claw::math::box_2d< T >::operator== | ( | const self_type & | that ) | const |
Equality operator.
that | Box to compare to. |
Definition at line 456 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().
void claw::math::box_2d< T >::right | ( | const value_type & | p ) |
Move the right edge at a given position.
p | The position. |
Definition at line 358 of file box_2d.tpp.
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::right | ( | ) | const |
Get the x-coordinate of the right edge.
Definition at line 270 of file box_2d.tpp.
Referenced by claw::math::box_2d< T >::intersects(), claw::math::box_2d< T >::join(), claw::math::box_2d< T >::operator==(), and claw::math::box_2d< T >::x_intersection().
{ return (first_point.x > second_point.x) ? first_point.x : second_point.x; } // box_2d::right()
void claw::math::box_2d< T >::set | ( | const value_type & | x1, |
const value_type & | y1, | ||
const value_type & | x2, | ||
const value_type & | y2 | ||
) |
Set the coordinates of the two points.
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.
References claw::math::box_2d< T >::set().
Referenced by claw::math::box_2d< T >::set().
{ first_point.set(x1, y1); second_point.set(x2, y2); } // box_2d::set()
void claw::math::box_2d< T >::shift_x | ( | const value_type & | d ) |
Shift the position of the box on the x-axis.
d | The movement length. |
Definition at line 421 of file box_2d.tpp.
{ first_point.x += d; second_point.x += d; } // box_2d::shift_x()
void claw::math::box_2d< T >::shift_y | ( | const value_type & | d ) |
Shift the position of the box on the y-axis.
d | The movement length. |
Definition at line 433 of file box_2d.tpp.
{ first_point.y += d; second_point.y += d; } // box_2d::shift_y()
claw::math::coordinate_2d< typename claw::math::box_2d< T >::value_type > claw::math::box_2d< T >::size | ( | ) | const |
Get the size of the box_2d.
Definition at line 445 of file box_2d.tpp.
{ return claw::math::coordinate_2d<value_type>(width(), height()); } // box_2d::size()
void claw::math::box_2d< T >::top | ( | const value_type & | p ) |
Move the top edge at a given position.
p | The position. |
Definition at line 325 of file box_2d.tpp.
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::top | ( | ) | const |
Get the y-coordinate of the top edge.
Definition at line 240 of file box_2d.tpp.
Referenced by claw::math::box_2d< T >::intersects(), claw::math::box_2d< T >::join(), claw::math::box_2d< T >::operator==(), and claw::math::box_2d< T >::y_intersection().
{ return (first_point.y > second_point.y) ? first_point.y : second_point.y; } // box_2d::top()
claw::math::box_2d< T >::point_type claw::math::box_2d< T >::top_left | ( | ) | const |
Get the coordinate of the top-left corner.
Definition at line 281 of file box_2d.tpp.
{ return point_type(left(), top()); } // box_2d::top_left()
void claw::math::box_2d< T >::top_left | ( | const coordinate_2d< value_type > & | p ) |
Move the top-left corner at a given position.
p | The position. |
Definition at line 370 of file box_2d.tpp.
References claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.
claw::math::box_2d< T >::point_type claw::math::box_2d< T >::top_right | ( | ) | const |
Get the coordinate of the top-right corner.
Definition at line 292 of file box_2d.tpp.
{ return point_type(right(), top()); } // box_2d::top_right()
void claw::math::box_2d< T >::top_right | ( | const coordinate_2d< value_type > & | p ) |
Move the top-right corner at a given position.
p | The position. |
Definition at line 383 of file box_2d.tpp.
References claw::math::coordinate_2d< T >::x, and claw::math::coordinate_2d< T >::y.
claw::math::box_2d< T >::value_type claw::math::box_2d< T >::width | ( | ) | const |
Return box' width.
Definition at line 529 of file box_2d.tpp.
{ if (first_point.x > second_point.x) return first_point.x - second_point.x; else return second_point.x - first_point.x; } // box_2d::width()
void claw::math::box_2d< T >::x_intersection | ( | const self_type & | r, |
self_type & | result | ||
) | const [private] |
X-intersection of two box_2ds.
Definition at line 559 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, and claw::math::coordinate_2d< T >::x.
void claw::math::box_2d< T >::y_intersection | ( | const self_type & | r, |
self_type & | result | ||
) | const [private] |
Y-intersection of two box_2ds.
Definition at line 573 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(), and claw::math::coordinate_2d< T >::y.
point_type claw::math::box_2d< T >::first_point |
The first of the two points, representing one corner.
Definition at line 120 of file box_2d.hpp.
Referenced by claw::math::rectangle< T >::includes(), claw::math::box_2d< T >::includes(), claw::math::box_2d< T >::x_intersection(), and claw::math::box_2d< T >::y_intersection().
point_type claw::math::box_2d< T >::second_point |
The second of the two points, representing an other corner.
Definition at line 123 of file box_2d.hpp.
Referenced by claw::math::rectangle< T >::includes(), claw::math::box_2d< T >::includes(), claw::math::box_2d< T >::x_intersection(), and claw::math::box_2d< T >::y_intersection().