Package com.google.common.geometry
Class R2Rect
java.lang.Object
com.google.common.geometry.R2Rect
- All Implemented Interfaces:
Serializable
An R2Rect represents a closed axis-aligned rectangle in the (x,y) plane. This class is mutable to
allow iteratively constructing bounds via e.g.
addPoint(R2Vector)
.- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionR2Rect()
Creates an empty R2Rect.R2Rect
(R1Interval x, R1Interval y) Constructs a rectangle from the given intervals in x and y.Copy constructor.Constructs a rectangle from the given lower-left and upper-right points. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Increase the size of the bounding rectangle to include the given point.void
Expand the rectangle to include the given other rectangle.boolean
approxEquals
(R2Rect other) Returns true if the x- and y-intervals of the two rectangles are the same up to the given tolerance.boolean
approxEquals
(R2Rect other, double maxError) Returns true if the given rectangles are equal to withinmaxError
.Return the closest point in this rectangle to the given point "p".boolean
Returns true if and only if this rectangle contains the given other rectangle.boolean
Returns true if this rectangle contains the given point.static R2Rect
empty()
Returns a new instance of the canonical empty rectangle.boolean
Returns true if two rectangles contains the same set of points.(package private) void
expand
(double margin) Expands this rectangle on both axes by the given margin.expanded
(double margin) Returns a rectangle that has been expanded on both sides by the given margin.Return a rectangle that has been expanded on each side in the x-direction by margin.x(), and on each side in the y-direction by margin.y().static R2Rect
fromCenterSize
(R2Vector center, R2Vector size) Returns a new rectangle from a center point and size in each dimension.static R2Rect
Returns a rectangle containing a single point.static R2Rect
fromPointPair
(R2Vector p1, R2Vector p2) Returns the minimal bounding rectangle containing the two given points.Returns the center of this rectangle in (x,y)-space.getInterval
(R2Rect.Axis axis) Returns the interval for the given axis, which must not be null.getSize()
Return the width and height of this rectangle in (x,y)-space.getVertex
(int k) Returns the kth vertex of this rectangle (k = 0,1,2,3) in CCW order.getVertex
(int i, int j) Returns the vertex in direction "i" along the x-axis (0=left, 1=right) and direction "j" along the y-axis (0=down, 1=up).int
hashCode()
Returns a simple convolution hashcodes from the x and y internals.hi()
Returns the point in this rectangle with the maximum x and y values.boolean
interiorContains
(R2Rect other) Returns true if and only if the interior of this rectangle contains all points of the given other rectangle (including its boundary).boolean
Returns true if and only if the given point is contained in the interior of the region (i.e.boolean
interiorIntersects
(R2Rect other) Return true if and only if the interior of this rectangle intersects any point (including the boundary) of the given other rectangle.intersection
(R2Rect other) Returns the smallest rectangle containing the intersection of this rectangle and the given rectangle.boolean
intersects
(R2Rect other) Returns true if this rectangle and the given other rectangle have any points in common.boolean
isEmpty()
Return true if this rectangle is empty, i.e.boolean
isValid()
Returns true if this rectangle is valid, which essentially just means that if the bound for either axis is empty then both must be.lo()
Returns the point in this rectangle with the minimum x and y values.toString()
Returns a simple string representation of this rectangle's lower and upper corners.Returns the smallest rectangle containing the union of this rectangle and the given rectangle.x()
Returns the interval along the x-axis.y()
Returns the interval along the y-axis.
-
Field Details
-
x
-
y
-
-
Constructor Details
-
R2Rect
public R2Rect()Creates an empty R2Rect. -
R2Rect
Constructs a rectangle from the given lower-left and upper-right points. -
R2Rect
Constructs a rectangle from the given intervals in x and y. The two intervals must either be both empty or both non-empty. -
R2Rect
Copy constructor.
-
-
Method Details
-
empty
Returns a new instance of the canonical empty rectangle. Use isEmpty() to test for empty rectangles, since they have more than one representation. -
fromCenterSize
Returns a new rectangle from a center point and size in each dimension. Both components of size should be non-negative, i.e. this method cannot be used to create an empty rectangle. -
fromPoint
Returns a rectangle containing a single point. -
fromPointPair
Returns the minimal bounding rectangle containing the two given points. This is equivalent to starting with an empty rectangle and calling addPoint() twice. Note that it is different than the R2Rect(lo, hi) constructor, where the first point is always used as the lower-left corner of the resulting rectangle. -
x
Returns the interval along the x-axis. -
y
Returns the interval along the y-axis. -
lo
Returns the point in this rectangle with the minimum x and y values. -
hi
Returns the point in this rectangle with the maximum x and y values. -
isValid
public boolean isValid()Returns true if this rectangle is valid, which essentially just means that if the bound for either axis is empty then both must be. -
isEmpty
public boolean isEmpty()Return true if this rectangle is empty, i.e. it contains no points at all. -
getVertex
Returns the kth vertex of this rectangle (k = 0,1,2,3) in CCW order. Vertex 0 is in the lower-left corner. -
getVertex
Returns the vertex in direction "i" along the x-axis (0=left, 1=right) and direction "j" along the y-axis (0=down, 1=up). Equivalently, returns the vertex constructed by selecting endpoint "i" of the x-interval (0=lo, 1=hi) and vertex "j" of the y-interval. -
getCenter
Returns the center of this rectangle in (x,y)-space. -
getSize
Return the width and height of this rectangle in (x,y)-space. Empty rectangles have a negative width and height. -
getInterval
Returns the interval for the given axis, which must not be null. -
contains
Returns true if this rectangle contains the given point. Note that rectangles are closed regions, i.e. they contain their boundary. -
interiorContains
Returns true if and only if the given point is contained in the interior of the region (i.e. the region excluding its boundary). -
contains
Returns true if and only if this rectangle contains the given other rectangle. -
interiorContains
Returns true if and only if the interior of this rectangle contains all points of the given other rectangle (including its boundary). -
intersects
Returns true if this rectangle and the given other rectangle have any points in common. -
interiorIntersects
Return true if and only if the interior of this rectangle intersects any point (including the boundary) of the given other rectangle. -
addPoint
Increase the size of the bounding rectangle to include the given point. This rectangle is expanded by the minimum amount possible. -
addRect
Expand the rectangle to include the given other rectangle. This is the same as replacing the rectangle by the union of the two rectangles, but is somewhat more efficient. -
clampPoint
Return the closest point in this rectangle to the given point "p". This rectangle must be non-empty. -
expanded
Return a rectangle that has been expanded on each side in the x-direction by margin.x(), and on each side in the y-direction by margin.y(). If either margin is empty, then shrink the interval on the corresponding sides instead. The resulting rectangle may be empty. Any expansion of an empty rectangle remains empty. -
expanded
Returns a rectangle that has been expanded on both sides by the given margin. Any expansion of an empty rectangle remains empty. -
expand
void expand(double margin) Expands this rectangle on both axes by the given margin.Package private since only S2 classes are intended to mutate R2Rects for now.
-
union
Returns the smallest rectangle containing the union of this rectangle and the given rectangle. -
intersection
Returns the smallest rectangle containing the intersection of this rectangle and the given rectangle. -
hashCode
public int hashCode()Returns a simple convolution hashcodes from the x and y internals. -
equals
Returns true if two rectangles contains the same set of points. -
approxEquals
Returns true if the x- and y-intervals of the two rectangles are the same up to the given tolerance. SeeR1Interval
for details on approximate interval equality. -
approxEquals
Returns true if the given rectangles are equal to withinmaxError
. SeeR1Interval
for details on approximate interval equality. -
toString
Returns a simple string representation of this rectangle's lower and upper corners.
-