ofRectangle
##Description
ofRectangle is a simple container for describing the position and size of a 2D rectangle. The ofRectangle class provides a data structure and a collection of useful helper methods for manipulating rectangles.
Since the width and height variables can be negative, the x/y-position is not guaranteed to represent the upper right hand corner. For example, two visually identical rectangles can be represented in the following ways:
ofRectangle myRect(0, 0, 100, 100);
or
ofRectangle myRect(100, 100, -100, -100);
While both representations will yield the same visual results in all openFrameworks renderers, the results of some method operations that modify x, y, width and height (such as scaling) produce mathematically correct, but visually different results for each of the above representations.
To avoid this ambiguity, users should prefer "standardized" rectangles. "Standardized" rectangles are rectangles whose width
alignTo( ... )
void alignTo(const glm::vec3 &targetPoint, ofAlignHorz thisHorzAnchor=OF_ALIGN_HORZ_CENTER, ofAlignVert thisVertAnchor=OF_ALIGN_VERT_CENTER)Align this ofRectangle to an glm::vec3 in both x- and y dimentions.
Aligns the position of the ofRectangle to the given point using an ofAlignHorz constant and an ofAlignVert constant. If neither constant is passed in, this will align the center of the rectangle.
Warning: The z-component of the passed glm::vec3 is ignored.
Parameters:
targetPoint The target glm::vec3 to align to.
thisHorzAnchor The horizontal alignment anchor.
thisVertAnchor The vertical alignment anchor.
ofAlignHorz Constants:
OF_ALIGN_HORZ_IGNORE: Does not perform any horizontal alignment.OF_ALIGN_HORZ_LEFT: Uses the left edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_RIGHT: Uses the right edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_CENTER: Uses the center of the rectangle to horizontally anchor the alignment.
ofAlignVert Constants:
OF_ALIGN_VERT_IGNORE: Does not perform any vertical alignment.OF_ALIGN_VERT_TOP: Uses the upper edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_BOTTOM: Uses the bottom edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_CENTER: Uses the center of the rectangle to vertically anchor the alignment.
For a working example of how to use ofAlignVert and ofAlignHorz, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory.
alignTo( ... )
void alignTo(const glm::vec2 &targetPoint, ofAlignHorz thisHorzAnchor=OF_ALIGN_HORZ_CENTER, ofAlignVert thisVertAnchor=OF_ALIGN_VERT_CENTER)ofAlignHorz Constants:
OF_ALIGN_HORZ_IGNORE: Does not perform any horizontal alignment.OF_ALIGN_HORZ_LEFT: Uses the left edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_RIGHT: Uses the right edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_CENTER: Uses the center of the rectangle to horizontally anchor the alignment.
ofAlignVert Constants:
OF_ALIGN_VERT_IGNORE: Does not perform any vertical alignment.OF_ALIGN_VERT_TOP: Uses the upper edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_BOTTOM: Uses the bottom edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_CENTER: Uses the center of the rectangle to vertically anchor the alignment.
For a working example of how to use ofAlignVert and ofAlignHorz, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory.
alignTo( ... )
void alignTo(const ofRectangle &targetRect, ofAlignHorz sharedHorzAnchor=OF_ALIGN_HORZ_CENTER, ofAlignVert sharedVertAnchor=OF_ALIGN_VERT_CENTER)Align this ofRectangle to an glm::vec3 in both x- and y dimentions using a shared anchor.
Aligns the position of the ofRectangle to that of the passed-in ofRectangle. Can take an ofAlignHorz constant and an ofAlignVert constant.
If neither constant is passed in, this will align the centers of the rectangles.
If constants are passed in, it will use those constants for both rectangles.
Parameters:
targetRect The target ofRectangle to align this rectangle to.
sharedHorzAnchor The alignment anchor for both rectangles.
sharedVertAnchor The alignment anchor for both rectangles.
ofAlignHorz Constants:
OF_ALIGN_HORZ_IGNORE: Does not perform any horizontal alignment.OF_ALIGN_HORZ_LEFT: Uses the left edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_RIGHT: Uses the right edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_CENTER: Uses the center of the rectangle to horizontally anchor the alignment.
ofAlignVert Constants:
OF_ALIGN_VERT_IGNORE: Does not perform any vertical alignment.OF_ALIGN_VERT_TOP: Uses the upper edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_BOTTOM: Uses the bottom edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_CENTER: Uses the center of the rectangle to vertically anchor the alignment.
alignTo( ... )
void alignTo(const ofRectangle &targetRect, ofAlignHorz targetHorzAnchor, ofAlignVert targetVertAnchor, ofAlignHorz thisHorzAnchor, ofAlignVert thisVertAnchor)Vertically align this ofRectangle to another target ofRectangle.
Aligns the position of the ofRectangle to that of the passed-in ofRectangle. Takes two ofAlignHorz constants and two ofAlignVert constants.
Will align the chosen anchors in the ofRectangle with the chosen anchors in the passed-in ofRectangle.
Parameters:
targetRect The target ofRectangle to align this rectangle to.
targetHorzAnchor The vertical alignment anchor for the target rectangle.
targetVertAnchor The horizontal alignment anchor for the target rectangle.
thisHorzAnchor The horizontal alignment anchor for this rectangle.
thisVertAnchor The vertical alignment anchor for this rectangle.
ofAlignHorz Constants:
OF_ALIGN_HORZ_IGNORE: Does not perform any horizontal alignment.OF_ALIGN_HORZ_LEFT: Uses the left edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_RIGHT: Uses the right edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_CENTER: Uses the center of the rectangle to horizontally anchor the alignment.
ofAlignVert Constants:
OF_ALIGN_VERT_IGNORE: Does not perform any vertical alignment.OF_ALIGN_VERT_TOP: Uses the upper edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_BOTTOM: Uses the bottom edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_CENTER: Uses the center of the rectangle to vertically anchor the alignment.
alignToHorz( ... )
void alignToHorz(const ofRectangle &targetRect, ofAlignHorz sharedAnchor=OF_ALIGN_HORZ_CENTER)Horizontally align two rectangles to another using one anchor for both.
Aligns the horizontal position of the ofRectangle to that of the passed-in ofRectangle using an ofAlignHorz constant as anchor.
If a constant is not passed in, this will align the horizontal centers of the rectangles.
If a constant is passed in, it will use that constant for both rectangles.
Parameters:
targetRect The target rectangle to align this rectangle to.
sharedAnchor The common edge of the rectangles to align.
ofAlignHorz Constants:
OF_ALIGN_HORZ_IGNORE: Does not perform any horizontal alignment.OF_ALIGN_HORZ_LEFT: Uses the left edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_RIGHT: Uses the right edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_CENTER: Uses the center of the rectangle to horizontally anchor the alignment.
For a working example of how to use ofAlignHorz, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory.
alignToHorz( ... )
void alignToHorz(const ofRectangle &targetRect, ofAlignHorz targetHorzAnchor, ofAlignHorz thisHorzAnchor)Horizontally align two rectangles to another using two anchors.
Aligns the horizontal position of the ofRectangle to that of the passed-in ofRectangle. Takes two ofAlignHorz constants as anchors.
Will align the chosen anchor in the ofRectangle with the chosen anchor in the passed-in ofRectangle.
Parameters:
targetRect The target rectangle to align this rectangle to.
targetHorzAnchor The edge of the other rectangle to align.
thisHorzAnchor The edge of this rectangle to align.
ofAlignHorz Constants:
OF_ALIGN_HORZ_IGNORE: Does not perform any horizontal alignment.OF_ALIGN_HORZ_LEFT: Uses the left edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_RIGHT: Uses the right edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_CENTER: Uses the center of the rectangle to horizontally anchor the alignment.
For a working example of how to use ofAlignHorz, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory.
alignToHorz( ... )
void alignToHorz(const float &targetX, ofAlignHorz thisHorzAnchor=OF_ALIGN_HORZ_CENTER)Horizontally align a rectangle using a position and anchor edge.
Aligns the horizontal position of the ofRectangle to the given x- position using an ofAlignHorz constant as anchor. If a constant is not passed in this will align the center of the rectangle.
Parameters:
targetX The x-position to align this rectangle to.
thisHorzAnchor The edge of this rectangle to align.
ofAlignHorz Constants:
OF_ALIGN_HORZ_IGNORE: Does not perform any horizontal alignment.OF_ALIGN_HORZ_LEFT: Uses the left edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_RIGHT: Uses the right edge of the rectangle to horizontally anchor the alignment.OF_ALIGN_HORZ_CENTER: Uses the center of the rectangle to horizontally anchor the alignment.
For a working example of how to use ofAlignHorz, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory.
alignToVert( ... )
void alignToVert(const ofRectangle &targetRect, ofAlignVert sharedAnchor=OF_ALIGN_VERT_CENTER)Vertically align two rectangles to another using one anchor for both.
Aligns the vertical position of the ofRectangle to that of the passed-in ofRectangle using an ofAlignVert constant as anchor.
If a constant is not passed in, this will align the vertical centers of the rectangles.
If a constant is passed in, it will use that constant for both rectangles.
Parameters:
targetRect The target ofRectangle to align this rectangle to.
sharedAnchor The alignment position for both rectangles to use.
ofAlignVert Constants:
OF_ALIGN_VERT_IGNORE: Does not perform any vertical alignment.OF_ALIGN_VERT_TOP: Uses the upper edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_BOTTOM: Uses the bottom edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_CENTER: Uses the center of the rectangle to vertically anchor the alignment.
For a working example of how to use ofAlignVert, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory
alignToVert( ... )
void alignToVert(const ofRectangle &targetRect, ofAlignVert targetVertAnchor, ofAlignVert thisVertAnchor)Vertically align two rectangles to another using two anchors.
Will align the chosen anchor in the ofRectangle with the chosen anchor in the passed-in ofRectangle.
Parameters:
targetRect The target ofRectangle to align this rectangle to.
targetVertAnchor The alignment anchor for this rectangle.
thisVertAnchor The alignment anchor for the other rectangle.
ofAlignVert Constants:
OF_ALIGN_VERT_IGNORE: Does not perform any vertical alignment.OF_ALIGN_VERT_TOP: Uses the upper edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_BOTTOM: Uses the bottom edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_CENTER: Uses the center of the rectangle to vertically anchor the alignment.
For a working example of how to use ofAlignVert, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory
alignToVert( ... )
void alignToVert(const float &targetY, ofAlignVert sharedAnchor=OF_ALIGN_VERT_CENTER)Vertically align a rectangle using a position and anchor edge.
Aligns the vertical position of the ofRectangle to the given y- position using an ofAlignVert constant as anchor. If a constant is not passed in this will align the center of the rectangle.
Parameters:
targetY The target y-position to align this rectangle to.
sharedAnchor The alignment position of this ofRectangle to use.
ofAlignVert Constants:
OF_ALIGN_VERT_IGNORE: Does not perform any vertical alignment.OF_ALIGN_VERT_TOP: Uses the upper edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_BOTTOM: Uses the bottom edge of the rectangle to vertically anchor the alignment.OF_ALIGN_VERT_CENTER: Uses the center of the rectangle to vertically anchor the alignment.
For a working example of how to use ofAlignVert, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory
getArea( )
float getArea()Get the area of the ofRectangle.
This is the product of the width and height of the recatngle.
Returns: The area of the rectangle as float.
Example:
// Create a rectangle that is 100 units wide and 200 units tall
ofRectangle myRect(0,0,100,200);
// Get the area of that rectangle
float areaValue = myRect.getArea();
// areaValue will be 20000.0.
getAspectRatio( )
float getAspectRatio()Gets the ratio of width to height of the ofRectangle.
Returns: The aspect ratio of the rectangle.
Example:
// Create a rectangle that is 100 units wide and 200 units tall
ofRectangle myRect(0,0,100,200);
// Get the aspect ratio of that rectangle
float aspectRatioValue = myRect.getAspectRatio();
// aspectRatioValue will be 0.5.
getBottom( )
float getBottom()Gets the y position of the bottom edge of the ofRectangle as float.
See also: getMaxY()
Returns: The y position of the bottom edge of the rectangle.
Equivalent to ofRectangle::getMaxY().
getBottomLeft( )
glm::vec3 getBottomLeft()Get the bottom-left coordinates of the ofRectangle as glm::vec3.
Returns: The bottom-left coordinates of the rectangle.
getBottomRight( )
glm::vec3 getBottomRight()Get the bottom-right coordinates of the ofRectangle as glm::vec3.
See also: getMax()
Returns: The bottom-right coordinates of the rectangle.
Equivalent to ofRectangle::getMax().
getCenter( )
glm::vec3 getCenter()Get the coordiantes of the ofRectangle's center as glm::vec3.
Warning: The z component of the returned glm::vec3 will always be 0.
Returns: The x and y coordinates of the center of the rectangle (z = 0).
getHeight( )
float getHeight()Gets the height of the ofRectangle as float.
Returns: The height of the rectangle.
getHorzAnchor( ... )
float getHorzAnchor(ofAlignHorz anchor)A convenience method that returns the value of one of
the horizontal edges of the ofRectangle using the ofAlignHorz enum.
Parameters:
anchor The anchor position to query.
Returns: the value of the referenced anchor position.
Possible anchor values are:
OF_ALIGN_HORZ_IGNORE: returns 0.0OF_ALIGN_HORZ_LEFT: returns the position of the left edge, equivalent to ofRectangle::geLeft().OF_ALIGN_HORZ_RIGHT: returns the position of the right edge, equivalent to ofRectangle::getRight().OF_ALIGN_HORZ_CENTER: returns the x-position of the center of the ofRectangle.
Any other anchor value will return 0.0.
getIntersection( ... )
ofRectangle getIntersection(const ofRectangle &rect)Get the intersecting area between this rectangle and another.
This method will attempt to return the area of overlap between this rectangle and the passed rectangle.
If the two rectangles do not overlap, it will return an "empty" rectangle located (0, 0) with 0 width and 0 height.
If the two rectangles only share an edge this will return a rectangle positioned on that edge:
If the shared edge is vertical, the rectangle will have zero width, otherwise it will have zero height.
Parameters:
rect The rectangle to intersect.
Returns: A new ofRectangle representing the intersecting area or an empty rectangle (0, 0, 0, 0) if there is no intersection.
getLeft( )
float getLeft()Gets the x position of the left edge of the ofRectangle as float.
See also: getMinX()
Returns: The x position of the left edge of the rectangle.
Equivalent to ofRectangle::getMinX().
getMax( )
glm::vec3 getMax()Get the maximum x and y coordinates of the ofRectangle as glm::vec3.
Returns: The maximum x and y coordinates of the rectangle.
Returns the (x,y) coordinates of the corner of the rectangle with the largest x and largest y position as an ofPoint.
Equivalent to ofRectangle::getBottomRight().
getMaxX( )
float getMaxX()Get the largest x position of the ofRectangle as float.
Returns: The largest x position of the rectangle.
Returns the position of the larger of the two sides of the rectangle perpendicular to the x axis as a float.
Equivalent to ofRectangle::getRight().
getMaxY( )
float getMaxY()Get the largest y position of the ofRectangle as float.
Returns: The largest y position of the rectangle.
Equivalent to ofRectangle::getBottom().
getMin( )
glm::vec3 getMin()Get the minimum x and y coordinates of the ofRectangle as glm::vec3.
Returns: The minimum x and y coordinates of the rectangle.
Equivalent to ofRectangle::getTopLeft().
getMinX( )
float getMinX()Get the smallest x position of the ofRectangle as float.
Returns: The smallest xposition of the rectangle.
Equivalent to ofRectangle::getLeft().
getMinY( )
float getMinY()Get the smallest y position of the ofRectangle as float.
Returns: The smallest y position of the rectangle.
Equivalent to ofRectangle::getTop().
getPerimeter( )
float getPerimeter()Gets the perimeter of the ofRectangle.
This is the sum of the lengths of the sides.
Returns: The perimeter of the rectangle as float.
Example:
// Create a rectangle that is 100 units wide and 200 units tall
ofRectangle myRect(0,0,100,200);
// Get the perimeter of that rectangle
float perimeterValue = myRect.getPerimeter();
// perimeterValue will be 600.0.
getPosition( )
const glm::vec3 & getPosition()Get the ofRectangle's position.
Warning: The z component of the returned glm::vec3 is undefined.
Returns: The rectangle's position.
Returns the (x,y) position of the ofRectangle as an ofPoint.
Equivalent to ofRectangle::getPositionRef().
getRight( )
float getRight()Gets the x position of the right edge of the ofRectangle as float.
See also: getMaxX()
Returns: The y position of the right edge of the rectangle.
Equivalent to ofRectangle::getMaxX().
getStandardized( )
ofRectangle getStandardized()Get the standardized representation of this rectangle.
"Standardized" rectangles are rectangles whose width and height are positive. For more information about standardized rectangles, see the discussion of the standardize() method.
Returns: the Standardized version of this ofRectangle.
getTop( )
float getTop()Gets the y position of the top edge of the ofRectangle as float.
See also: getMinY()
Returns: The y position of the top edge of the rectangle.
Equivalent to ofRectangle::getMinY().
getTopLeft( )
glm::vec3 getTopLeft()Get the top-left coordinates of the ofRectangle as glm::vec3.
See also: getMin()
Returns: The top-left coordinates of the rectangle.
Equivalent to ofRectangle::getMin().
getTopRight( )
glm::vec3 getTopRight()Get the top-right coordinates of the ofRectangle as glm::vec3.
Returns: The top-right coordinates of the rectangle.
getUnion( ... )
ofRectangle getUnion(const ofRectangle &rect)Get the union area between this rectangle and anohter.
See also: getIntersection(const ofRectangle& rect) const
Parameters:
rect The rectangle to unite with.
Returns: A new ofRectangle whose area contains both the area of the this rectangle and the passed rectangle..
Return a new ofRectangle whose area contains both the area of the ofRectangle and that of the passed ofRectangle.
getVertAnchor( ... )
float getVertAnchor(ofAlignVert anchor)A convenience method that returns the value of one of the vertical edges of the ofRectangle using the ofAlignVert enum.
Parameters:
anchor The anchor position to query.
Returns: the value of the referenced anchor position.
Possible anchor values are:
OF_ALIGN_VERT_IGNORE: returns 0.0OF_ALIGN_VERT_TOP: returns the position of the upper edge, equivalent to ofRectangle::getTop().OF_ALIGN_VERT_BOTTOM: returns the position of the bottom edge, equivalent to ofRectangle::getBottom().OF_ALIGN_VERT_CENTER: returns the y-position of the center of the ofRectangle.
Any other anchor value will return 0.0.
getWidth( )
float getWidth()Gets the width of the ofRectangle as float.
Returns: The width of the rectangle.
getX( )
float getX()Gets the x position of the ofRectangle as float.
Returns: The x position of the rectangle.
getY( )
float getY()Gets the y position of the ofRectangle as float.
Returns: the y position of the rectangle.
growToInclude( ... )
void growToInclude(const glm::vec3 &p)Grow the ofRectangle to include the given point.
This will potentially change the width, height, x-position, and y- position of the ofRectangle. If the point is already within the rectangle, this rectangle will remain unchanged.
Warning: The z-component of the passed glm::vec3 is ignored.
Parameters:
p The position to include.
growToInclude( ... )
void growToInclude(const glm::vec2 &p)growToInclude( ... )
void growToInclude(const glm::vec3 &p0, const glm::vec3 &p1)Grow the ofRectangle to include the given line segment.
This will potentially change the width, height, x-position, and y-position of the ofRectangle. If the points are already within the rectangle, this rectangle will remain unchanged.
Warning: The z-components of the passed glm::vec3s are ignored.
Parameters:
p0 The first point to include.
p1 The second point to include.
Modify the ofRectangle so that both the ofPoints given are enclosed within the rectangle.
growToInclude( ... )
void growToInclude(const glm::vec2 &p0, const glm::vec2 &p1)Modify the ofRectangle so that both the ofPoints given are enclosed within the rectangle.
growToInclude( ... )
void growToInclude(const ofRectangle &rect)Grow the ofRectangle to include the given ofRectangle.
This will potentially change the width, height, x-position, and y- position of the ofRectangle. If the rectangle is already within the rectangle, this rectangle will remain unchanged.
Parameters:
rect The rectangle to include.
Expands the ofRectangle so that the ofRectangle passed into the method is entirely enclosed by the current ofRectangle.
growToInclude( ... )
void growToInclude(float px, float py)Grow the ofRectangle to include the given (x, y) coordinates.
This will potentially change the width, height, x-position, and y- position of the ofRectangle. If the points are already within the rectangle, this rectangle will remain unchanged.
Parameters:
px The x-coordinate to include.
py The y-coordiante to include.
inside( ... )
bool inside(const glm::vec3 &p)Determines if the glm::vec3 is within the ofRectangle.
Note that points on the edge of the ofRectangle are not considered within the rectangle and will return false.
Warning: The z-component of the passed glm::vec3 is ignored.
Parameters:
p The point to test.
Returns: true if the point p is inside this ofRectangle.
Coordinates (x, y) are considered inside the rectangle if:
`x
inside( ... )
bool inside(const glm::vec2 &p)Coordinates (x, y) are considered inside the rectangle if:
`x
inside( ... )
bool inside(const glm::vec3 &p0, const glm::vec3 &p1)Determines if both of the passed glm::vec3s are within the ofRectangle or not.
Note that points on the edge of the ofRectangle are not considered within the rectangle and will return false.
Warning: The z-components of the passed glm::vec3s are ignored.
Parameters:
p0 The first point to test.
p1 The second point to test.
Returns: true if both points are inside the rectangle.
This can be used to test if a line segment is inside the rectangle.
inside( ... )
bool inside(const glm::vec2 &p0, const glm::vec2 &p1)This can be used to test if a line segment is inside the rectangle.
inside( ... )
bool inside(const ofRectangle &rect)Determines if another ofRectangle is completely within the ofRectangle.
Note that rectangles that share an edge with the ofRectangle are not considered within the rectangle and will return false.
Parameters:
rect The ofRectangle to test.
Returns: true if all four corners of the rect are within this rectangle.
inside( ... )
bool inside(float px, float py)Determines if the coordinates (x, y) are within the ofRectangle.
Note that coordinates on the edge of the ofRectangle are not considered within the rectangle and will return false.
Parameters:
px The x-coordinate to test.
py The y-coordinate to test.
Returns: true if px and py are inside this ofRectangle.
Coordinates (x, y) are considered inside the rectangle if:
`x
intersects( ... )
bool intersects(const glm::vec3 &p0, const glm::vec3 &p1)Determines if a line segment intersects with the ofRectangle.
Warning: The z-components of the passed glm::vec3s are ignored.
Parameters:
p0 The first point to test.
p1 The second point to test.
Returns: true if the line segment defined by the two passed glm::vec3s
either crosses the perimeter of the ofRectangle or is completely
contained within.
This method can be used to check if any part of the line segment intersects with the rectangle.
intersects( ... )
bool intersects(const glm::vec2 &p0, const glm::vec2 &p1)This method can be used to check if any part of the line segment intersects with the rectangle.
intersects( ... )
bool intersects(const ofRectangle &rect)Determines if another rectangle intersects with this rectangle.
Rectangles that only share an edge and do not intersect otherwise are not considered to intersect and will return false.
Parameters:
rect The rectangle to test.
Returns: true if the area contained within the ofRectangle overlaps
with the area contained within the passed ofRectangle.
Returns true if the area contained within the ofRectangle overlaps with the area contained within the passed ofRectangle, returns false otherwise.
Rectangles that only share an edge and do not intersect otherwise are not considered to intersect and will return false.
isEmpty( )
bool isEmpty()Determines if the ofRectangle's area is zero.
See also: getArea().
Returns: true if both the width == 0 and height == 0, false if either is non-zero.
isStandardized( )
bool isStandardized()isZero( )
bool isZero()map( ... )
glm::vec2 map(const glm::vec2 &coeff)Maps a normalized coordinate into this rectangle
Normalized coordinates range from [0,1] inclusive. It is used to define a ratio between the coordinates and a unitary rectangle. This ratio is mapped into this rectangle to scale to real values. If normalized coordinates are out of bounds, output will be scaled accordingly.
Parameters:
coeff Normalized coordinate to map to this rectangle
Returns: The mapped coordinate
map( ... )
ofRectangle map(const ofRectangle &coeff)Maps a normalized coordinate into this rectangle
Normalized rectangles' position, width and height range from [0,1] inclusive. It is used to define a ratio between the rectangle and a unitary rectangle. This ratio is mapped into this rectangle to scale to real values. If normalized rectangle is out of bounds, output will be scaled accordingly.
Parameters:
coeff Normalized rectangle to map to this rectangle
Returns: The mapped coordinate
mapClamp( ... )
glm::vec2 mapClamp(const glm::vec2 &coeff)Maps a normalized coordinate into this rectangle, clamping if out of bounds
Normalized coordinates range from [0,1] inclusive. It is used to define a ratio between the coordinates and a unitary rectangle. This ratio is mapped into this rectangle to scale to real values. If normalized coordinates are out of bounds, output will be clamped to this rectangle's position, width and height.
Parameters:
coeff Normalized coordinate to map to this rectangle
Returns: The mapped coordinate, clamped
mapClamp( ... )
ofRectangle mapClamp(const ofRectangle &coeff)Maps a normalized rectangle to this rectangle, clamping if out of bounds
Normalized rectangles' position, width and height range from [0,1] inclusive. It is used to define a ratio between the rectangle and a unitary rectangle. This ratio is mapped into this rectangle to scale to real values. If normalized rectangle is out of bounds, output will be clamped to this rectangle's position, width and height.
Parameters:
coeff Normalized rectangle to map to this rectangle
Returns: The mapped rectangle
ofRectangle( ... )
ofRectangle(const glm::vec3 &p, float w, float h)ofRectangle( ... )
ofRectangle(const glm::vec2 &p, float w, float h)ofRectangle( ... )
ofRectangle(const glm::vec3 &p0, const glm::vec3 &p1)Construct a rectangle by defining two corners.
Warning: The z-components of the passed glm::vec3s are ignored.
Parameters:
p0 An glm::vec3 representing the upper left hand corner.
p1 An glm::vec3 representing the lower right hand corner.
ofRectangle( ... )
ofRectangle(const glm::vec2 &p0, const glm::vec2 &p1)ofRectangle( ... )
ofRectangle(const ofRectangle &rect)Construct a rectangle by copying another rectangle.
Parameters:
rect The rectangle to copy.
ofRectangle( )
ofRectangle()Construct a rectangle with zero width and zero height at 0, 0.
ofRectangle( ... )
ofRectangle(float px, float py, float w, float h)operator!=( ... )
bool operator!=(const ofRectangle &rect)If the two ofRectangles differ in x, y, width, or height, they are considered unequal.
Parameters:
rect The rectangle to compare.
Returns: True if the rectangles are not equal.
operator+( ... )
ofRectangle operator+(const glm::vec3 &p)Returns a new ofRectangle where the x and y positions of the rectangle are offset by the (x, y) coordinates of the glm::vec3.
Parameters:
p The point to translate.
Returns: The translated ofRectangle.
operator+( ... )
ofRectangle operator+(const glm::vec2 &p)operator-( ... )
ofRectangle operator-(const glm::vec3 &p)Returns a new ofRectangle where the x and y-positions of the rectangle are offset by the (x, y) coordinates of the glm::vec3.
Parameters:
p The point to translate.
Returns: The translated ofRectangle.
operator-( ... )
ofRectangle operator-(const glm::vec2 &p)operator=( ... )
ofRectangle & operator=(const ofRectangle &rect)Assignment operator.
Parameters:
rect The rectangle to assign.
Returns: A reference to this rectangle.
operator==( ... )
bool operator==(const ofRectangle &rect)If both ofRectangles have the same x, y, width, and height, they are considered equal.
Parameters:
rect The rectangle to compare.
Returns: True if the rectangles are equal.
scale( ... )
void scale(const glm::vec3 &s)Scale the rectangle.
Scaling will scale the width and the height, but will not change the position.
Warning: The z-component of the passed glm::vec3 is ignored.
Parameters:
s The scaling factor.
Scales the width and height of the ofRectangle by multiplying the current width and height with the (x,y) coordinates of the passed-in ofPoint.
This is equivalent to calling:
ofRectangle myRect(0, 0, 100, 100);
ofPoint myScaler(2, 2);
myRect.scaleWidth(myScaler.x);
myRect.scaleHeight(myScaler.y);
scale( ... )
void scale(const glm::vec2 &s)Scales the width and height of the ofRectangle by multiplying the current width and height with the (x,y) coordinates of the passed-in ofPoint.
This is equivalent to calling:
ofRectangle myRect(0, 0, 100, 100);
ofPoint myScaler(2, 2);
myRect.scaleWidth(myScaler.x);
myRect.scaleHeight(myScaler.y);
scale( ... )
void scale(float s)Scale the rectangle.
Scaling will scale the width and the height, but will not change the position.
Parameters:
s The scaling factor.
Uniformly scales the ofRectangle by multiplying both the current width and current height with the passed-in float.
This is equivalent to calling:
ofRectangle myRect(0, 0, 100, 100);
float s = 2;
myRect.scaleWidth(s);
myRect.scaleHeight(s);
scale( ... )
void scale(float sX, float sY)Scale the rectangle.
Scaling will scale the width and the height, but will not change the position.
Parameters:
sX The width-scaling factor.
sY The height-scaling factor.
Scales the width and height of the ofRectangle by multiplying the current width and height with the passed-in floats.
This is equivalent to calling:
ofRectangle myRect(0, 0, 100, 100);
float sX = 2;
float sY = 2;
myRect.scaleWidth(sX);
myRect.scaleHeight(sY);
scaleFromCenter( ... )
void scaleFromCenter(const glm::vec3 &s)Scales both the width and height the ofRectangle from its center.
The center point of the rectangle will remain fixed and the width, height, x, and y will be adjusted.
Warning: The z-component of the passed glm::vec3 is ignored.
Parameters:
s The scaling factor.
Scales both the width and height the ofRectangle from its center point. The center point of the rectangle will remain fixed and the width, height, x, and y will be adjusted.
This is equivalent to calling:
ofRectangle myRect(0, 0, 100, 100);
ofPoint myScaler(2, 2);
myRect.scaleFromCenter(myScaler.x, myScaler.y);
scaleFromCenter( ... )
void scaleFromCenter(const glm::vec2 &s)Scales both the width and height the ofRectangle from its center point. The center point of the rectangle will remain fixed and the width, height, x, and y will be adjusted.
This is equivalent to calling:
ofRectangle myRect(0, 0, 100, 100);
ofPoint myScaler(2, 2);
myRect.scaleFromCenter(myScaler.x, myScaler.y);
scaleFromCenter( ... )
void scaleFromCenter(float s)Scales both the width and height the ofRectangle from its center.
The center point of the rectangle will remain fixed and the width, height, x, and y will be adjusted.
Parameters:
s The scaling factor.
Uniformly scales the ofRectangle from its center point. The center point of the rectangle will remain fixed and the width, height, x, and y will be adjusted.
scaleFromCenter( ... )
void scaleFromCenter(float sX, float sY)Scales both the width and height the ofRectangle from its center.
The center point of the rectangle will remain fixed and the width, height, x, and y will be adjusted.
Parameters:
sX the width-scaling factor.
sY the height-scaling factor.
Scales both the width and height the ofRectangle from its center point. The center point of the rectangle will remain fixed and the width, height, x, and y will be adjusted.
scaleHeight( ... )
void scaleHeight(float sY)Scale the height of the rectangle.
This will scale the height but will not change the position and width.
Parameters:
sY The height-scaling factor.
Scales the height of the ofRectangle by multiplying the current height with the passed-in float.
scaleTo( ... )
void scaleTo(const ofRectangle &targetRect, ofScaleMode scaleMode=OF_SCALEMODE_FIT)Scale the rectanle using a target ofRectangle and ofScaleMode.
Adjusts the ofRectangle to match the provided ofRectangle using the provided ofScaleMode.
Parameters:
targetRect The Target Rectangle to scale to.
scaleMode The scale mode to use when scaling.
Adjusts the ofRectangle to match the passed-in ofRectangle using the passed-in ofScaleMode.
Assuming the ofRectangle as the Subject and the passed-in ofRectangle as the Target:
OF_SCALEMODE_FIT: This centers the Subject rectangle within the Target rectangle, and resizes the Subject rectangle to completely fit within the Target Rectangle.
- It fits the Subject rectangle inside the target rectangle.
- It preserves Subject's aspect ratio.
- The Subject's final area <= the Target's area.
- The Subject's center == the Target's center.
OF_SCALEMODE_FILL: This centers the Subject rectangle within the Target rectangle, and resizes the Subject rectangle to completely encompass the Target Rectangle.
- It fills the Target rectangle with the Subject rectangle.
- It preserves the Subject's aspect ratio.
- The Subject's Area
scaleTo( ... )
void scaleTo(const ofRectangle &targetRect, ofAspectRatioMode subjectAspectRatioMode, ofAlignHorz modelHorzAnchor, ofAlignVert modelVertAnchor, ofAlignHorz subjectHorzAnchor, ofAlignVert subjectVertAnchor)Scale the rectangle using a target ofRectangle and parameters.
Adjusts the ofRectangle to match the passed-in ofRectangle. It will use the ofAspectRatioMode to scale the ofRectangle, and will use the use the alignment anchor parameters to position the rectangle.
Parameters:
targetRect The Target Rectangle to scale to.
subjectAspectRatioMode Aspect ratio scaling mode.
modelHorzAnchor The target horizontal alignment method.
modelVertAnchor The target vertical alignment method.
subjectHorzAnchor The subject horizontal alignment method.
subjectVertAnchor The subject vertical alignment method.
Adjusts the ofRectangle to match the passed-in ofRectangle. It will use the ofAspectRatioMode to scale the ofRectangle, and will use the use the alignment anchor parameters to position the rectangle.
Assuming the ofRectangle as the Subject and the passed-in ofRectangle as the Target:
ofAspectRatioMode Options:
OF_ASPECT_RATIO_IGNORE : Sets the Subject rectangle's width and height to match those of the Target. OF_ASPECT_RATIO_KEEP : Resizes the Subject rectangle to completely fit within the Target rectangle. OF_ASPECT_RATIO_KEEP_BY_EXPANDING : Resizes the Subject rectangle to completely enclose the Target rectangle.
ofAlignHorz Options:
OF_ALIGN_HORZ_IGNORE : Does not perform any horizontal alignment. OF_ALIGN_HORZ_LEFT : Uses the left edge of the rectangle to horizontally anchor the alignment. OF_ALIGN_HORZ_RIGHT : Uses the right edge of the rectangle to horizontally anchor the alignment. OF_ALIGN_HORZ_CENTER : Uses the center of the rectangle to horizontally anchor the alignment.
ofAlignVert Options:
OF_ALIGN_VERT_IGNORE : Does not perform any vertical alignment. OF_ALIGN_VERT_TOP : Uses the upper edge of the rectangle to vertically anchor the alignment. OF_ALIGN_VERT_BOTTOM : Uses the bottom edge of the rectangle to vertically anchor the alignment. OF_ALIGN_VERT_CENTER : Uses the center of the rectangle to vertically anchor the alignment.
For a working example of how to use ofAspectRatioMode, ofAlignVert, and ofAlignHorz, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory.
scaleTo( ... )
void scaleTo(const ofRectangle &targetRect, ofAspectRatioMode subjectAspectRatioMode, ofAlignHorz sharedHorzAnchor=OF_ALIGN_HORZ_CENTER, ofAlignVert sharedVertAnchor=OF_ALIGN_VERT_CENTER)Scale the rectangle using a target ofRectangle and parameters.
Scales the ofRectangle to match the target ofRectangle. It will use the ofAspectRatioMode to scale the ofRectangle and will use the alignment anchor parameters to position the rectangle.
Parameters:
targetRect The Target Rectangle to scale to.
subjectAspectRatioMode Aspect ratio scaling mode.
sharedHorzAnchor The horizontal alignment method.
sharedVertAnchor The vertical alignment method.
Adjusts the ofRectangle to match the passed-in ofRectangle. It will use the ofAspectRatioMode to scale the ofRectangle, and will use the alignment anchor parameters to position the rectangle.
This is a convenient method when both the target and subject rectangles are using the same alignment anchors. For a more complete explanation, see
void scaleTo(const ofRectangle& targetRect,
ofAspectRatioMode subjectAspectRatioMode,
ofAlignHorz modelHorzAnchor,
ofAlignVert modelVertAnchor,
ofAlignHorz subjectHorzAnchor,
ofAlignVert subjectVertAnchor);
For a working example of how to use ofAspectRatioMode, ofAlignVert, and ofAlignHorz, see the graphics/rectangleAlignmentAndScaling/ example within the examples directory.
scaleWidth( ... )
void scaleWidth(float sX)Scale the width of the rectangle.
This will scale the width but will not change the position or height.
Parameters:
sX The width-scaling factor.
Scales the width of the ofRectangle by multiplying the current width with the passed-in float.
set( ... )
void set(const glm::vec3 &p, float w, float h)Sets the (x,y) coordinates, width, and height of the ofRectangle.
set( ... )
void set(const glm::vec2 &p, float w, float h)Sets the (x,y) coordinates, width, and height of the ofRectangle.
set( ... )
void set(const glm::vec3 &p0, const glm::vec3 &p1)Set the position and size of the rectangle using corners.
Warning: The z-components of the passed glm::vec3s are ignored.
Parameters:
p0 The glm::vec3 representing the upper left hand corner.
p1 The glm::vec3 representing the lower right hand corner.
Sets the x position, y position, width, and height of the ofRectangle to enclose the line segment defined by the passed ofPoints.
The rectangle will always be in standardized form, regardless of the order of the ofPoints passed in.
set( ... )
void set(const glm::vec2 &p0, const glm::vec2 &p1)Sets the x position, y position, width, and height of the ofRectangle to enclose the line segment defined by the passed ofPoints.
The rectangle will always be in standardized form, regardless of the order of the ofPoints passed in.
set( ... )
void set(const ofRectangle &rect)Set the position and size by copying them from anohter rectangle.
Parameters:
rect The rectangle to copy.
Sets the x position, y position, width, and height of the ofRectangle to match those of the passed ofRectangle.
set( ... )
void set(float px, float py, float w, float h)Sets the x position, y position, width, and height of the ofRectangle.
setFromCenter( ... )
void setFromCenter(const glm::vec3 &p, float w, float h)Set position and size of the rectangle from the center.
The center of the rectangle is defined and the width and height grow out around the center.
Parameters:
p The position of the rectangle's center as an glm::vec3.
w The width of the rectangle.
h The height of the rectangle.
Sets the position, width, and height of the ofRectangle. The (x,y) coordinates passed into the function determine the center point of the ofRectangle.
setFromCenter( ... )
void setFromCenter(const glm::vec2 &p, float w, float h)Sets the position, width, and height of the ofRectangle. The (x,y) coordinates passed into the function determine the center point of the ofRectangle.
setFromCenter( ... )
void setFromCenter(float px, float py, float w, float h)Set position and size of the rectangle from the center.
The center of the rectangle is defined and the width and height grow out around the center.
Parameters:
px The x-position of the rectangle's center.
py The y-position of the rectangle's center.
w The width of the rectangle.
h The height of the rectangle.
Sets the position, width, and height of the ofRectangle. The x and y position passed into the function determine the center point of the ofRectangle.
setHeight( ... )
void setHeight(float h)Set the height of the rectangle.
Parameters:
h The new height.
Sets the height of the ofRectangle.
setPosition( ... )
void setPosition(const glm::vec3 &p)Set the position of the rectangle.
Warning: The z-component of the passed glm::vec3 is ignored.
Parameters:
p The position as an glm::vec3.
Sets the (x,y) position of the ofRectangle by passing in x and y as an ofPoint.
setPosition( ... )
void setPosition(float px, float py)Set the position of the rectangle.
Parameters:
px The new x-position.
py The new y-position.
Sets the (x,y) position of the ofRectangle by passing in x and y as floats.
setSize( ... )
void setSize(float w, float h)Set the size of the rectangle.
Parameters:
w The new width.
h The new height.
setWidth( ... )
void setWidth(float w)Set the width of the rectangle.
Parameters:
w The new width.
Sets the width of the ofRectangle.
setX( ... )
void setX(float px)Set the x-position of the rectangle.
Parameters:
px The new x-position.
Sets the x position of the ofRectangle.
setY( ... )
void setY(float py)Set the y-position of the rectangle.
Parameters:
py The new y-position.
Sets the y position of the ofRectangle.
standardize( )
void standardize()ofRectangle is a simple container for describing the position and size of 2D rectangles. Like many rectangle data structures found in other frameworks and graphics libraries, member width and height variables can take negative values. Additionally, x / y position and width / height are publicly accessible, resulting in great programming freedom. Consequently, two visually identical rectangles can be represented in the following ways:
ofRectangle myRect(0,0,100,100);
// OR
ofRectangle myRect(100,100,-100,-100);
While both representations will yield the same visual results in all openFrameworks renderers, the results of some method operations that modify x / y / width / height (such as scaling) produce mathematically correct, but visually different results for each of the above representations.
translate( ... )
void translate(const glm::vec3 &dp)Translate the rectangle's position by an x and y amount.
Warning: The z-component of the passed glm::vec3 is ignored.
Parameters:
dp The amount to translate as an glm::vec3.
Offsets the position of the ofRectangle in the both the x and y dimensions by passing in an ofPoint.
translate( ... )
void translate(const glm::vec2 &dp)Offsets the position of the ofRectangle in the both the x and y dimensions by passing in an ofPoint.
translate( ... )
void translate(float dx, float dy)Translate the rectangle's position by an x and y amount.
Parameters:
dx The amount to translate in the x direction.
dy The amount to translate in the y direction.
Offsets the position of the ofRectangle in the both the x and y dimensions by passing in two floats.
translateX( ... )
void translateX(float dx)Translate the x-position of the rectangle.
Parameters:
dx The amount to translate on the x-axis.
Offsets the position of the ofRectangle in the x dimension by a given amount.
translateY( ... )
void translateY(float dy)Translate the y-position of the rectangle.
Parameters:
dy The amount to translate on the y-axis.
Offsets the position of the ofRectangle in the y dimension by a given amount.
~ofRectangle( )
~ofRectangle()Destroy the rectangle.