Package toxi.geom
Class Rect
java.lang.Object
toxi.geom.Rect
- All Implemented Interfaces:
Shape2D
- Direct Known Subclasses:
PointQuadtree
-
Field Summary
-
Constructor Summary
ConstructorDescriptionRect()
Constructs an empty rectangle at point 0,0 with no dimensions.Rect
(float x, float y, float width, float height) Constructs a new rectangle using a point and dimensionsRect
(ReadonlyVec2D p1, ReadonlyVec2D p2) Constructs a new rectangle defined by the two points given. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks if the given point is within the rectangle's bounds.copy()
Creates a copy of this rectangleboolean
Returns true if the Object o is of type Rect and all of the data members of o are equal to the corresponding data members in this rectangle.static final Rect
fromCenterExtent
(ReadonlyVec2D center, Vec2D extent) Factory method, constructs a new rectangle from a center point and extent vector.final float
getArea()
Computes the area of the shape.final float
Computes the aspect ratio of the rect as width over height.float
final Vec2D
Computes the bounding circle of the shape.static final Rect
getBoundingRect
(List<? extends Vec2D> points) Factory method, computes & returns the bounding rect for the given list of points.Only provided because the Rect class implementsShape2D
.final Vec2D
Returns the centroid of the rectangle.final float
Computes the shape's circumference.final Vec2D
Returns a vector containing the width and height of the rectangle.getEdge
(int id) Returns one of the rectangles edges asLine2D
.getEdges()
Returns a list of the shape's perimeter edges.float
getLeft()
Computes the normalized position of the given point within this rectangle, so that a point at the top-left corner becomes {0,0} and bottom-right {1,1}.Creates a random point within the rectangle.float
getRight()
float
getTop()
final Vec2D
Inverse operation ofgetMappedPointInRect(Vec2D)
.int
hashCode()
Returns a hash code value based on the data values in this object.final Rect
Creates a new rectangle by forming the intersection of this rectangle and the given other rect.boolean
intersectsCircle
(Vec2D c, float r) intersectsRay
(Ray2D ray, float minDist, float maxDist) Checks if the rectangle intersects with the given ray and if so computes the first intersection point.boolean
Checks if this rectangle intersects/overlaps the given one.scale
(float s) final Rect
set
(float x, float y, float w, float h) Sets new bounds for this rectangle.final Rect
final Rect
setDimension
(Vec2D dim) final Rect
setPosition
(Vec2D pos) Creates aPolygon2D
instance of the rect.toPolygon2D
(float radius, int res) Turns this rectangle into a rounded rectangle shapedPolygon2D
instance with the given corner radius.toString()
translate
(float dx, float dy) translate
(ReadonlyVec2D offset) final Rect
Deprecated.final Rect
Creates a new rectangle by forming an union of this rectangle and the given other Rect.
-
Field Details
-
x
public float x -
y
public float y -
width
public float width -
height
public float height
-
-
Constructor Details
-
Rect
public Rect()Constructs an empty rectangle at point 0,0 with no dimensions. -
Rect
public Rect(float x, float y, float width, float height) Constructs a new rectangle using a point and dimensions- Parameters:
x
- x of top lefty
- y of top leftwidth
-height
-
-
Rect
Constructs a new rectangle defined by the two points given.- Parameters:
p1
-p2
-
-
-
Method Details
-
fromCenterExtent
Factory method, constructs a new rectangle from a center point and extent vector.- Parameters:
center
-extent
-- Returns:
- new rect
-
getBoundingRect
Factory method, computes & returns the bounding rect for the given list of points.- Parameters:
points
-- Returns:
- bounding rect
- Since:
- 0021
-
containsPoint
Checks if the given point is within the rectangle's bounds.- Specified by:
containsPoint
in interfaceShape2D
- Parameters:
p
- point to check- Returns:
- true, if point is contained
-
copy
Creates a copy of this rectangle- Returns:
- new instance
-
equals
Returns true if the Object o is of type Rect and all of the data members of o are equal to the corresponding data members in this rectangle. -
getArea
public final float getArea()Description copied from interface:Shape2D
Computes the area of the shape. -
getAspect
public final float getAspect()Computes the aspect ratio of the rect as width over height.- Returns:
- aspect ratio
-
getBottom
public float getBottom() -
getBottomLeft
-
getBottomRight
-
getBoundingCircle
Description copied from interface:Shape2D
Computes the bounding circle of the shape.- Specified by:
getBoundingCircle
in interfaceShape2D
- Returns:
- circle
-
getBounds
Only provided because the Rect class implementsShape2D
. The bounding rect of a rectangle is itself. -
getCentroid
Returns the centroid of the rectangle.- Returns:
- centroid vector
-
getCircumference
public final float getCircumference()Description copied from interface:Shape2D
Computes the shape's circumference.- Specified by:
getCircumference
in interfaceShape2D
- Returns:
- circumference
-
getDimensions
Returns a vector containing the width and height of the rectangle.- Returns:
- dimension vector
-
getEdge
Returns one of the rectangles edges asLine2D
. The edge IDs are:- 0 - top
- 1 - right
- 2 - bottom
- 3 - left
- Parameters:
id
- edge ID- Returns:
- edge as Line2D
-
getEdges
Description copied from interface:Shape2D
Returns a list of the shape's perimeter edges. -
getLeft
public float getLeft() -
getMappedPointInRect
Computes the normalized position of the given point within this rectangle, so that a point at the top-left corner becomes {0,0} and bottom-right {1,1}. The original point is not modified. Together withgetUnmappedPointInRect(Vec2D)
this function can be used to map a point from one rectangle to another.- Parameters:
p
- point to be mapped- Returns:
- mapped Vec2D
-
getRandomPoint
Creates a random point within the rectangle.- Specified by:
getRandomPoint
in interfaceShape2D
- Returns:
- Vec2D
-
getRight
public float getRight() -
getTop
public float getTop() -
getTopLeft
-
getTopRight
-
getUnmappedPointInRect
Inverse operation ofgetMappedPointInRect(Vec2D)
. Given a normalized point it computes the position within this rectangle, so that a point at {0,0} becomes the top-left corner and {1,1} bottom-right. The original point is not modified. Together withgetUnmappedPointInRect(Vec2D)
this function can be used to map a point from one rectangle to another.- Parameters:
p
- point to be mapped- Returns:
- mapped Vec2D
-
growToContainPoint
-
hashCode
public int hashCode()Returns a hash code value based on the data values in this object. Two different Rect objects with identical data values (i.e., Rect.equals returns true) will return the same hash code value. Two objects with different data members may return the same hash value, although this is not likely. -
intersectionRectWith
Creates a new rectangle by forming the intersection of this rectangle and the given other rect. The resulting bounds will be the rectangle of the overlay area or null if the rects do not intersect.- Parameters:
r
- intersection partner rect- Returns:
- new Rect or null
-
intersectsCircle
-
intersectsRay
Checks if the rectangle intersects with the given ray and if so computes the first intersection point. The method takes a min/max distance interval along the ray in which the intersection must occur.- Parameters:
ray
- intersection rayminDist
- minimum distancemaxDist
- max distance- Returns:
- intersection point or null if no intersection in the given interval
-
intersectsRect
Checks if this rectangle intersects/overlaps the given one.- Parameters:
r
- another rect- Returns:
- true, if intersecting
-
scale
-
set
Sets new bounds for this rectangle.- Parameters:
x
- x of top lefty
- y of top rightw
- widthh
- height- Returns:
- itself
-
set
-
setDimension
-
setPosition
-
toPolygon2D
Creates aPolygon2D
instance of the rect.- Specified by:
toPolygon2D
in interfaceShape2D
- Returns:
- rect as polygon
-
toPolygon2D
Turns this rectangle into a rounded rectangle shapedPolygon2D
instance with the given corner radius. The number of corner vertices to be used, can be specified as well.- Parameters:
radius
- corner radiusres
- number of vertices per corner- Returns:
- rounded rect as polygon
-
toString
-
translate
-
translate
-
union
Deprecated.useunionRectWith(Rect)
instead. Also note, thatunionRectWith(Rect)
does NOT modify the original Rect anymore, but produces a new Rect instance.- Parameters:
r
-- Returns:
- new Rect
-
unionRectWith
Creates a new rectangle by forming an union of this rectangle and the given other Rect. The resulting bounds will be inclusive of both.- Parameters:
r
-- Returns:
- new Rect
-
unionRectWith(Rect)
instead.