Package org.geotools.geometry.util
Class XRectangle2D
- Object
-
- RectangularShape
-
- Rectangle2D
-
- XRectangle2D
-
- All Implemented Interfaces:
Shape
,Serializable
,Cloneable
public class XRectangle2D extends Rectangle2D implements Serializable
Serializable, high-performance double-precision rectangle. Instead of usingx
,y
,width
andheight
, this class store rectangle's coordinates into the following fields:xmin
,xmax
,ymin
etymax
. Methods likescontains
andintersects
are faster, which make this class more appropriate for using intensively inside a loop. Furthermore, this class work correctly with infinites and NaN values.- Since:
- 2.0
- Author:
- Martin Desruisseaux (IRD)
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class Rectangle2D
Rectangle2D.Double, Rectangle2D.Float
-
-
Field Summary
Fields Modifier and Type Field Description static Rectangle2D
INFINITY
An immutable instance of aRectangle2D
with bounds extending toward infinities.protected double
xmax
Maximal x coordinate.protected double
xmin
Minimal x coordinate.protected double
ymax
Maximal y coordinate.protected double
ymin
Minimal y coordinate.-
Fields inherited from class Rectangle2D
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP
-
-
Constructor Summary
Constructors Constructor Description XRectangle2D()
Construct a default rectangle.XRectangle2D(double x, double y, double width, double height)
Construct a rectangle with the specified location and dimension.XRectangle2D(Rectangle2D rect)
Construct a rectangle with the same coordinates than the supplied rectangle.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(double x, double y)
Adds a point, specified by the double precision argumentsx
andy
, to thisRectangle2D
.void
add(Rectangle2D rect)
Adds aRectangle2D
object to thisRectangle2D
.boolean
contains(double x, double y)
Tests if a specified coordinate is inside the boundary of thisRectangle2D
.boolean
contains(double x, double y, double width, double height)
Tests if the interior of thisRectangle2D
entirely contains the specified set of rectangular coordinates.boolean
contains(Rectangle2D rect)
Tests if the interior of this shape entirely contains the specified rectangle.static boolean
containsInclusive(Rectangle2D outter, Rectangle2D inner)
Tests if the interior of theinner
rectangle is contained in the interior and/or the edge of theoutter
rectangle.static XRectangle2D
createFromExtremums(double xmin, double ymin, double xmax, double ymax)
Create a rectangle using maximal x and y values rather than width and height.Rectangle2D
createIntersection(Rectangle2D rect)
Returns a newRectangle2D
object representing the intersection of thisRectangle2D
with the specifiedRectangle2D
.Rectangle2D
createUnion(Rectangle2D rect)
Returns a newRectangle2D
object representing the union of thisRectangle2D
with the specifiedRectangle2D
.static boolean
equalsEpsilon(Rectangle2D rect1, Rectangle2D rect2)
Returnstrue
if the two rectangles are equals up to an epsilon value.double
getCenterX()
Returns the X coordinate of the center of the rectangle.double
getCenterY()
Returns the Y coordinate of the center of the rectangle.double
getHeight()
Returns the height of the framing rectangle indouble
precision.double
getMaxX()
Returns the largest X coordinate of the rectangle.double
getMaxY()
Returns the largest Y coordinate of the rectangle.double
getMinX()
Returns the smallest X coordinate of the rectangle.double
getMinY()
Returns the smallest Y coordinate of the rectangle.double
getWidth()
Returns the width of the framing rectangle indouble
precision.double
getX()
Returns the X coordinate of the upper left corner of the framing rectangle indouble
precision.double
getY()
Returns the Y coordinate of the upper left corner of the framing rectangle indouble
precision.static boolean
intersectInclusive(Rectangle2D rect1, Rectangle2D rect2)
Tests if the interior and/or the edge of two rectangles intersect.static boolean
intersectInclusive(Shape shape, Rectangle2D rect)
Tests if the interior of theShape
intersects the interior of a specified rectangle.boolean
intersects(double x, double y, double width, double height)
Tests if the interior of thisRectangle2D
intersects the interior of a specified set of rectangular coordinates.boolean
intersects(Rectangle2D rect)
Tests if the interior of this shape intersects the interior of a specified rectangle.boolean
isEmpty()
Determines whether theRectangularShape
is empty.int
outcode(double x, double y)
Determines where the specified coordinates lie with respect to thisRectangle2D
.void
setRect(double x, double y, double width, double height)
Sets the location and size of thisRectangle2D
to the specified double values.void
setRect(Rectangle2D r)
Sets thisRectangle2D
to be the same as the specifiedRectangle2D
.String
toString()
Returns theString
representation of thisRectangle2D
.-
Methods inherited from class Rectangle2D
add, equals, getBounds2D, getPathIterator, getPathIterator, hashCode, intersect, intersectsLine, intersectsLine, outcode, setFrame, union
-
Methods inherited from class RectangularShape
clone, contains, getBounds, getFrame, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal
-
-
-
-
Field Detail
-
INFINITY
public static final Rectangle2D INFINITY
An immutable instance of aRectangle2D
with bounds extending toward infinities. ThegetMinX()
andgetMinY()
methods return alwaysDouble.NEGATIVE_INFINITY
, while thegetMaxX()
andgetMaxY()
methods return alwaysDouble.POSITIVE_INFINITY
. This rectangle can be used as argument in theXRectangle2D
constructor for initializing a newXRectangle2D
to infinite bounds.
-
xmin
protected double xmin
Minimal x coordinate.
-
ymin
protected double ymin
Minimal y coordinate.
-
xmax
protected double xmax
Maximal x coordinate.
-
ymax
protected double ymax
Maximal y coordinate.
-
-
Constructor Detail
-
XRectangle2D
public XRectangle2D()
Construct a default rectangle. Initial coordinates are(0,0,0,0)
.
-
XRectangle2D
public XRectangle2D(double x, double y, double width, double height)
Construct a rectangle with the specified location and dimension. This constructor uses the same signature thanRectangle2D
for consistency.
-
XRectangle2D
public XRectangle2D(Rectangle2D rect)
Construct a rectangle with the same coordinates than the supplied rectangle.- Parameters:
rect
- The rectangle, ornull
in none (in which case this constructor is equivalents to the no-argument constructor). UseINFINITY
for initializing thisXRectangle2D
with infinite bounds.
-
-
Method Detail
-
createFromExtremums
public static XRectangle2D createFromExtremums(double xmin, double ymin, double xmax, double ymax)
Create a rectangle using maximal x and y values rather than width and height. This factory avoid the problem of NaN values when extremums are infinite numbers.
-
isEmpty
public boolean isEmpty()
Determines whether theRectangularShape
is empty. When theRectangularShape
is empty, it encloses no area.- Specified by:
isEmpty
in classRectangularShape
- Returns:
true
if theRectangularShape
is empty;false
otherwise.
-
getX
public double getX()
Returns the X coordinate of the upper left corner of the framing rectangle indouble
precision.- Specified by:
getX
in classRectangularShape
- Returns:
- the x coordinate of the upper left corner of the framing rectangle.
-
getY
public double getY()
Returns the Y coordinate of the upper left corner of the framing rectangle indouble
precision.- Specified by:
getY
in classRectangularShape
- Returns:
- the y coordinate of the upper left corner of the framing rectangle.
-
getWidth
public double getWidth()
Returns the width of the framing rectangle indouble
precision.- Specified by:
getWidth
in classRectangularShape
- Returns:
- the width of the framing rectangle.
-
getHeight
public double getHeight()
Returns the height of the framing rectangle indouble
precision.- Specified by:
getHeight
in classRectangularShape
- Returns:
- the height of the framing rectangle.
-
getMinX
public double getMinX()
Returns the smallest X coordinate of the rectangle.- Overrides:
getMinX
in classRectangularShape
-
getMinY
public double getMinY()
Returns the smallest Y coordinate of the rectangle.- Overrides:
getMinY
in classRectangularShape
-
getMaxX
public double getMaxX()
Returns the largest X coordinate of the rectangle.- Overrides:
getMaxX
in classRectangularShape
-
getMaxY
public double getMaxY()
Returns the largest Y coordinate of the rectangle.- Overrides:
getMaxY
in classRectangularShape
-
getCenterX
public double getCenterX()
Returns the X coordinate of the center of the rectangle.- Overrides:
getCenterX
in classRectangularShape
-
getCenterY
public double getCenterY()
Returns the Y coordinate of the center of the rectangle.- Overrides:
getCenterY
in classRectangularShape
-
setRect
public void setRect(double x, double y, double width, double height)
Sets the location and size of thisRectangle2D
to the specified double values.- Specified by:
setRect
in classRectangle2D
- Parameters:
x
- the x coordinates to which to set the location of the upper left corner of thisRectangle2D
y
- the y coordinates to which to set the location of the upper left corner of thisRectangle2D
width
- the value to use to set the width of thisRectangle2D
height
- the value to use to set the height of thisRectangle2D
-
setRect
public void setRect(Rectangle2D r)
Sets thisRectangle2D
to be the same as the specifiedRectangle2D
.- Overrides:
setRect
in classRectangle2D
- Parameters:
r
- the specifiedRectangle2D
-
intersects
public boolean intersects(double x, double y, double width, double height)
Tests if the interior of thisRectangle2D
intersects the interior of a specified set of rectangular coordinates.- Specified by:
intersects
in interfaceShape
- Overrides:
intersects
in classRectangle2D
- Parameters:
x
- the x coordinates of the upper left corner of the specified set of rectangular coordinatesy
- the y coordinates of the upper left corner of the specified set of rectangular coordinateswidth
- the width of the specified set of rectangular coordinatesheight
- the height of the specified set of rectangular coordinates- Returns:
true
if thisRectangle2D
intersects the interior of a specified set of rectangular coordinates;false
otherwise.
-
intersects
public boolean intersects(Rectangle2D rect)
Tests if the interior of this shape intersects the interior of a specified rectangle. This methods overrides the defaultRectangle2D
implementation in order to work correctly with infinites and NaN values.- Specified by:
intersects
in interfaceShape
- Overrides:
intersects
in classRectangularShape
- Parameters:
rect
- the specified rectangle.- Returns:
true
if this shape and the specified rectangle intersect each other.- See Also:
intersectInclusive(Rectangle2D, Rectangle2D)
-
intersectInclusive
public static boolean intersectInclusive(Rectangle2D rect1, Rectangle2D rect2)
Tests if the interior and/or the edge of two rectangles intersect. This method is similar tointersects(Rectangle2D)
except for the following points:- This method doesn't test only if the interiors intersect. It tests for the edges as well.
- This method tests also rectangle with zero width or
height (which are empty according
Shape
contract). However, rectangle with negative width or height are still considered as empty. - This method work correctly with infinites and NaN values.
- Parameters:
rect1
- The first rectangle to test.rect2
- The second rectangle to test.- Returns:
true
if the interior and/or the edge of the two specified rectangles intersects.
-
intersectInclusive
public static boolean intersectInclusive(Shape shape, Rectangle2D rect)
Tests if the interior of theShape
intersects the interior of a specified rectangle. This method might conservatively returntrue
when there is a high probability that the rectangle and the shape intersect, but the calculations to accurately determine this intersection are prohibitively expensive. This is similar toShape.intersects(Rectangle2D)
, except that this method tests also rectangle with zero width or height (which are empty accordingShape
contract). However, rectangle with negative width or height are still considered as empty.
This method is said inclusive because it try to mimicintersectInclusive(Rectangle2D, Rectangle2D)
behavior, at least for rectangle with zero width or height.- Parameters:
shape
- The shape.rect
- The rectangle to test for inclusion.- Returns:
true
if the interior of the shape and the interior of the specified rectangle intersect, or are both highly likely to intersect.
-
equalsEpsilon
public static boolean equalsEpsilon(Rectangle2D rect1, Rectangle2D rect2)
Returnstrue
if the two rectangles are equals up to an epsilon value.
-
contains
public boolean contains(double x, double y, double width, double height)
Tests if the interior of thisRectangle2D
entirely contains the specified set of rectangular coordinates.- Specified by:
contains
in interfaceShape
- Overrides:
contains
in classRectangle2D
- Parameters:
x
- the x coordinates of the upper left corner of the specified set of rectangular coordinatesy
- the y coordinates of the upper left corner of the specified set of rectangular coordinateswidth
- the width of the specified set of rectangular coordinatesheight
- the height of the specified set of rectangular coordinates- Returns:
true
if thisRectangle2D
entirely contains specified set of rectangular coordinates;false
otherwise.
-
contains
public boolean contains(Rectangle2D rect)
Tests if the interior of this shape entirely contains the specified rectangle. This methods overrides the defaultRectangle2D
implementation in order to work correctly with infinites and NaN values.- Specified by:
contains
in interfaceShape
- Overrides:
contains
in classRectangularShape
- Parameters:
rect
- the specified rectangle.- Returns:
true
if this shape entirely contains the specified rectangle.
-
contains
public boolean contains(double x, double y)
Tests if a specified coordinate is inside the boundary of thisRectangle2D
.- Specified by:
contains
in interfaceShape
- Overrides:
contains
in classRectangle2D
- Parameters:
x
- the x coordinates to test.y
- the y coordinates to test.- Returns:
true
if the specified coordinates are inside the boundary of thisRectangle2D
;false
otherwise.
-
containsInclusive
public static boolean containsInclusive(Rectangle2D outter, Rectangle2D inner)
Tests if the interior of theinner
rectangle is contained in the interior and/or the edge of theoutter
rectangle. This method is similar tocontains(Rectangle2D)
except for the following points: This method is said inclusive because it tests bounds as closed interval rather then open interval (the default Java2D behavior). Usage of closed interval is required if at least one rectangle may be the bounding box of a perfectly horizontal or vertical line; such a bounding box has 0 width or height.- Parameters:
outter
- The first rectangle to test.inner
- The second rectangle to test.- Returns:
true
if the interior ofinner
is inside the interior and/or the edge ofoutter
.
-
outcode
public int outcode(double x, double y)
Determines where the specified coordinates lie with respect to thisRectangle2D
. This method computes a binary OR of the appropriate mask values indicating, for each side of thisRectangle2D
, whether or not the specified coordinates are on the same side of the edge as the rest of thisRectangle2D
.- Specified by:
outcode
in classRectangle2D
- Returns:
- the logical OR of all appropriate out codes.
- See Also:
Rectangle2D.OUT_LEFT
,Rectangle2D.OUT_TOP
,Rectangle2D.OUT_RIGHT
,Rectangle2D.OUT_BOTTOM
-
createIntersection
public Rectangle2D createIntersection(Rectangle2D rect)
Returns a newRectangle2D
object representing the intersection of thisRectangle2D
with the specifiedRectangle2D
.- Specified by:
createIntersection
in classRectangle2D
- Parameters:
rect
- theRectangle2D
to be intersected with thisRectangle2D
- Returns:
- the largest
Rectangle2D
contained in both the specifiedRectangle2D
and in thisRectangle2D
.
-
createUnion
public Rectangle2D createUnion(Rectangle2D rect)
Returns a newRectangle2D
object representing the union of thisRectangle2D
with the specifiedRectangle2D
.- Specified by:
createUnion
in classRectangle2D
- Parameters:
rect
- theRectangle2D
to be combined with thisRectangle2D
- Returns:
- the smallest
Rectangle2D
containing both the specifiedRectangle2D
and thisRectangle2D
.
-
add
public void add(double x, double y)
Adds a point, specified by the double precision argumentsx
andy
, to thisRectangle2D
. The resultingRectangle2D
is the smallestRectangle2D
that contains both the originalRectangle2D
and the specified point.After adding a point, a call to
contains
with the added point as an argument does not necessarily returntrue
. Thecontains
method does not returntrue
for points on the right or bottom edges of a rectangle. Therefore, if the added point falls on the left or bottom edge of the enlarged rectangle,contains
returnsfalse
for that point.- Overrides:
add
in classRectangle2D
-
add
public void add(Rectangle2D rect)
Adds aRectangle2D
object to thisRectangle2D
. The resultingRectangle2D
is the union of the twoRectangle2D
objects.- Overrides:
add
in classRectangle2D
- Parameters:
rect
- theRectangle2D
to add to thisRectangle2D
.
-
-