CUGL
Cornell University Game Library
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
cugl::Rect Class Reference

#include <CURect.h>

Public Member Functions

 Rect ()
 
 Rect (float x, float y, float width, float height)
 
 Rect (float *array)
 
 Rect (const Vec2 &pos, const Size &dimen)
 
 Rect (const Rect &other)
 
Rectoperator= (const Rect &other)
 
Rectoperator= (const float *array)
 
Rectset (float x, float y, float width, float height)
 
Rectset (const float *array)
 
Rectset (const Vec2 &pos, const Size &dimen)
 
Rectset (const Rect &other)
 
float getMinX () const
 
float getMidX () const
 
float getMaxX () const
 
float getMinY () const
 
float getMidY () const
 
float getMaxY () const
 
float isDegenerate () const
 
bool operator== (const Rect &rect) const
 
bool operator!= (const Rect &rect) const
 
bool equals (const Rect &rect, float variance=CU_MATH_EPSILON) const
 
bool operator<= (const Rect &rect) const
 
bool operator< (const Rect &rect) const
 
bool operator>= (const Rect &rect) const
 
bool operator> (const Rect &rect) const
 
bool inside (const Rect &rect) const
 
bool contains (const Rect &rect) const
 
bool touches (const Vec2 &point) const
 
bool contains (const Vec2 &point) const
 
bool contains (const Vec2 &center, float radius) const
 
bool doesIntersect (const Rect &rect) const
 
bool doesIntersect (const Vec2 &center, float radius) const
 
Rectmerge (const Rect &rect)
 
Rectintersect (const Rect &rect)
 
Rectexpand (float factor)
 
Rectexpand (const Vec2 &point)
 
Rect getMerge (const Rect &rect)
 
Rect getIntersection (const Rect &rect)
 
Rect getExpansion (float factor)
 
Rect getExpansion (const Vec2 &point)
 

Public Attributes

Vec2 origin
 
Size size
 

Static Public Attributes

static const Rect ZERO
 
static const Rect UNIT
 

Detailed Description

This class represents a rectangle area in 2D space.

It is generally safe to manipulate the fields directly.

Constructor & Destructor Documentation

cugl::Rect::Rect ( )
inline

Creates an empty Rect at the origin

cugl::Rect::Rect ( float  x,
float  y,
float  width,
float  height 
)
inline

Creates a rect with the given origin and dimensions.

Parameters
xThe x-coordinate of the bottom left corner
yThe y-coordinate of the bottom left corner
widthThe width of the rect
heightThe width of the rect
cugl::Rect::Rect ( float *  array)
inline

Creates a rect from the given float array.

Parameters
arrayAn array containing the attributes in the order origin, size.
cugl::Rect::Rect ( const Vec2 pos,
const Size dimen 
)
inline

Creates a rect with the given origin and dimensions.

Parameters
posThe position the bottom left corner
dimenThe size of the rect
cugl::Rect::Rect ( const Rect other)
inline

Creates a copy of the given rectangle

Parameters
otherThe rectangle to copy

Member Function Documentation

bool cugl::Rect::contains ( const Rect rect) const

Returns true if this rect can hold the given rect.

This method does not test for strict containment. To test for strict containment, use the comparison operator >.

Parameters
rectThe potentially smaller rect
Returns
True if this rect can hold the given rect.
bool cugl::Rect::contains ( const Vec2 point) const

Returns true if this rect contains the given point.

This method does not test for strict containment; it includes the boundardy. To test for strict containment, combine this with the method touches().

Parameters
pointThe point to test
Returns
True if this rect contains the given point.
bool cugl::Rect::contains ( const Vec2 center,
float  radius 
) const

Returns true if this rect contains the given circle.

This method does not test for strict containment; it includes the boundary of both the circle and the rectangle.

Parameters
centerThe center of the circle
radiusThe radius of the circle
Returns
True if this rect contains the given circle.
bool cugl::Rect::doesIntersect ( const Rect rect) const

Returns true if this rect intersects the other.

This method allows for intersections where the edges of the rects are touching. In this case, the size of the intersection is empty.

Parameters
rectThe rect to test
Returns
true if this rect intersects the other.
bool cugl::Rect::doesIntersect ( const Vec2 center,
float  radius 
) const

Returns true if this rect intersects the given circle.

This method allows for intersections where the edge of the rect simply touches the boundary of the circle.

Parameters
centerThe center of the circle
radiusThe radius of the circle
Returns
true if this rect intersects the given circle.
bool cugl::Rect::equals ( const Rect rect,
float  variance = CU_MATH_EPSILON 
) const
inline

Returns true if the rects are within tolerance of each other.

The tolerance bound is on attribute independently.

Parameters
rectThe rect to compare against.
varianceThe comparison tolerance.
Returns
true if the rects are within tolerance of each other.
Rect& cugl::Rect::expand ( float  factor)

Expands this rectangle uniformly from its center.

Each edge of the rectangle is adjusted factor away from the center point. As a result, this method changes both origin and size. The value factor can be negative, in which case the rect shrinks in size.

Parameters
factorThe amount to expand each edge from the center.
Returns
This rect, after the expansion.
Rect& cugl::Rect::expand ( const Vec2 point)

Expands this rectangle to the minimal one containing the given point.

If the rectangle already contains the point, it is unchanged.

Parameters
pointThe point to envelop
Returns
This rect, after the expansion.
Rect cugl::Rect::getExpansion ( float  factor)
inline

Returns a copy of this rect, expanded uniformly from its center.

Each edge of the rectangle is adjusted factor away from the center point. As a result, this method changes both origin and size. The value factor can be negative, in which case the rect shrinks in size.

Note: This does not modify the rect.

Parameters
factorThe amount to expand each edge from the center.
Returns
a copy of this rect, expanded uniformly from its center.
Rect cugl::Rect::getExpansion ( const Vec2 point)
inline

Returns a copy of this rectangle, expanded to contain the given point.

If the rectangle already contains the point, the rect is the same as the original.

Parameters
pointThe point to envelop
Returns
a copy of this rect, expanded to contain the given point.
Rect cugl::Rect::getIntersection ( const Rect rect)
inline

Returns the intersection of this rect and the other.

If there is no intersection, this method returns the zero rectangle.

Parameters
rectThe rect to intersect with this one.

Note: This does not modify the rect.

Returns
the intersection of this rect and the other.
float cugl::Rect::getMaxX ( ) const
inline

Returns the rightmost x-value of the rect.

Returns
the rightmost x-value of the rect.
float cugl::Rect::getMaxY ( ) const
inline

Returns the top y-value of the rect.

Returns
the top y-value of the rect.
Rect cugl::Rect::getMerge ( const Rect rect)
inline

Returns the union of this rect and the other.

Parameters
rectThe rect to union with this one.

Note: This does not modify the rect.

Returns
the union of this rect and the other.
float cugl::Rect::getMidX ( ) const
inline

Returns the center x-value of the rect.

Returns
the center x-value of the rect.
float cugl::Rect::getMidY ( ) const
inline

Returns the center y-value of the rect.

Returns
the center y-value of the rect.
float cugl::Rect::getMinX ( ) const
inline

Returns the leftmost x-value of the rect.

Returns
the leftmost x-value of the rect.
float cugl::Rect::getMinY ( ) const
inline

Returns the bottom y-value of the rect.

Returns
the bottom y-value of the rect.
bool cugl::Rect::inside ( const Rect rect) const

Returns true if this rect fits inside of the given rect.

This method does not test for strict containment. To test for strict containment, use the comparison operator <.

Parameters
rectThe potentially larger rect
Returns
True if this rect fits inside of the given rect.
Rect& cugl::Rect::intersect ( const Rect rect)

Computes the intersection of this rect and the other, assigning it in place.

If there is no intersection, this rect becomes the zero rectangle.

Parameters
rectThe rect to intersect with this one.
Returns
This rect, after the intersection operation.
float cugl::Rect::isDegenerate ( ) const
inline

Returns true if the rectangle has non-positive size.

Returns
true if the rectangle has non-positive size.
Rect& cugl::Rect::merge ( const Rect rect)

Computes the union of this rect and the other, assigning it in place.

Parameters
rectThe rect to union with this one.
Returns
This rect, after the union operation.
bool cugl::Rect::operator!= ( const Rect rect) const
inline

Returns true if the rects are not equal to each other.

This operator uses exact equality and may fail due to round-off error.

Parameters
rectThe rect to compare against.
Returns
true if the rects are not equal to each other.
bool cugl::Rect::operator< ( const Rect rect) const

Returns true if this rect properly fits inside of the given rect.

Proper containment means that no edges of the two rects touch.

This operator is provided for convenience. However, this does not provide a total order, making it unsafe for std::sort.

Parameters
rectThe potentially larger rect
Returns
True if this rect properly fits inside of the given rect.
bool cugl::Rect::operator<= ( const Rect rect) const
inline

Returns true if this rect fits inside of the given rect.

This operator is provided for convenience. However, this does not provide a total order, making it unsafe for std::sort.

Parameters
rectThe potentially larger rect
Returns
True if this rect fits inside of the given rect.
Rect& cugl::Rect::operator= ( const Rect other)
inline

Assigns this rect to be a copy of the given rectangle.

Parameters
otherThe rectangle to copy
Returns
This rectangle, after assignment
Rect& cugl::Rect::operator= ( const float *  array)
inline

Sets the elements of this rect from the values in the specified array.

Parameters
arrayAn array containing the elements in the order origin, size.
Returns
This rectangle, after assignment
bool cugl::Rect::operator== ( const Rect rect) const
inline

Returns true if the rects are equal to each other.

This operator uses exact equality and may fail due to round-off error.

Parameters
rectThe rect to compare against.
Returns
true if the rects are equal to each other.
bool cugl::Rect::operator> ( const Rect rect) const

Returns true if this rect can properly hold the given rect.

Proper containment means that no edges of the two rects touch.

This operator is provided for convenience. However, this does not provide a total order, making it unsafe for std::sort.

Parameters
rectThe potentially smaller rect
Returns
True if this rect can properly hold the given rect.
bool cugl::Rect::operator>= ( const Rect rect) const
inline

Returns true if this rect can hold the given rect.

This operator is provided for convenience. However, this does not provide a total order, making it unsafe for std::sort.

Parameters
rectThe potentially smaller rect
Returns
True if this rect can hold the given rect.
Rect& cugl::Rect::set ( float  x,
float  y,
float  width,
float  height 
)
inline

Assigns this rect to have the given position and dimensions.

Parameters
xThe x-coordinate of the bottom left corner
yThe y-coordinate of the bottom left corner
widthThe width of the rect
heightThe width of the rect
Returns
This rectangle, after assignment
Rect& cugl::Rect::set ( const float *  array)
inline

Sets the elements of this rect from the values in the specified array.

Parameters
arrayAn array containing the elements in the order origin, size.
Returns
This rectangle, after assignment
Rect& cugl::Rect::set ( const Vec2 pos,
const Size dimen 
)
inline

Assigns this rect to have the given position and dimensions.

Parameters
posThe position the bottom left corner
dimenThe size of the rect
Returns
This rectangle, after assignment
Rect& cugl::Rect::set ( const Rect other)
inline

Assigns this rect to be a copy of the given rectangle.

Parameters
otherThe rectangle to copy
Returns
This rectangle, after assignment
bool cugl::Rect::touches ( const Vec2 point) const

Returns true if the given point is on the boundar of this rect.

Combining this method with contains() allows you to test for strict containment.

Parameters
pointThe point to test
Returns
True if the given point is on the boundar of this rect.

Member Data Documentation

Vec2 cugl::Rect::origin

The bottom left point of rect.

Size cugl::Rect::size

The width and height of the rect.

const Rect cugl::Rect::UNIT
static

The unit square.

const Rect cugl::Rect::ZERO
static

The empty Rect.


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