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,widthandheight, this class store rectangle's coordinates into the following fields:xmin,xmax,yminetymax. Methods likescontainsandintersectsare 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 Rectangle2DINFINITYAn immutable instance of aRectangle2Dwith bounds extending toward infinities.protected doublexmaxMaximal x coordinate.protected doublexminMinimal x coordinate.protected doubleymaxMaximal y coordinate.protected doubleyminMinimal 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 voidadd(double x, double y)Adds a point, specified by the double precision argumentsxandy, to thisRectangle2D.voidadd(Rectangle2D rect)Adds aRectangle2Dobject to thisRectangle2D.booleancontains(double x, double y)Tests if a specified coordinate is inside the boundary of thisRectangle2D.booleancontains(double x, double y, double width, double height)Tests if the interior of thisRectangle2Dentirely contains the specified set of rectangular coordinates.booleancontains(Rectangle2D rect)Tests if the interior of this shape entirely contains the specified rectangle.static booleancontainsInclusive(Rectangle2D outter, Rectangle2D inner)Tests if the interior of theinnerrectangle is contained in the interior and/or the edge of theoutterrectangle.static XRectangle2DcreateFromExtremums(double xmin, double ymin, double xmax, double ymax)Create a rectangle using maximal x and y values rather than width and height.Rectangle2DcreateIntersection(Rectangle2D rect)Returns a newRectangle2Dobject representing the intersection of thisRectangle2Dwith the specifiedRectangle2D.Rectangle2DcreateUnion(Rectangle2D rect)Returns a newRectangle2Dobject representing the union of thisRectangle2Dwith the specifiedRectangle2D.static booleanequalsEpsilon(Rectangle2D rect1, Rectangle2D rect2)Returnstrueif the two rectangles are equals up to an epsilon value.doublegetCenterX()Returns the X coordinate of the center of the rectangle.doublegetCenterY()Returns the Y coordinate of the center of the rectangle.doublegetHeight()Returns the height of the framing rectangle indoubleprecision.doublegetMaxX()Returns the largest X coordinate of the rectangle.doublegetMaxY()Returns the largest Y coordinate of the rectangle.doublegetMinX()Returns the smallest X coordinate of the rectangle.doublegetMinY()Returns the smallest Y coordinate of the rectangle.doublegetWidth()Returns the width of the framing rectangle indoubleprecision.doublegetX()Returns the X coordinate of the upper left corner of the framing rectangle indoubleprecision.doublegetY()Returns the Y coordinate of the upper left corner of the framing rectangle indoubleprecision.static booleanintersectInclusive(Rectangle2D rect1, Rectangle2D rect2)Tests if the interior and/or the edge of two rectangles intersect.static booleanintersectInclusive(Shape shape, Rectangle2D rect)Tests if the interior of theShapeintersects the interior of a specified rectangle.booleanintersects(double x, double y, double width, double height)Tests if the interior of thisRectangle2Dintersects the interior of a specified set of rectangular coordinates.booleanintersects(Rectangle2D rect)Tests if the interior of this shape intersects the interior of a specified rectangle.booleanisEmpty()Determines whether theRectangularShapeis empty.intoutcode(double x, double y)Determines where the specified coordinates lie with respect to thisRectangle2D.voidsetRect(double x, double y, double width, double height)Sets the location and size of thisRectangle2Dto the specified double values.voidsetRect(Rectangle2D r)Sets thisRectangle2Dto be the same as the specifiedRectangle2D.StringtoString()Returns theStringrepresentation 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 aRectangle2Dwith 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 theXRectangle2Dconstructor for initializing a newXRectangle2Dto 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 thanRectangle2Dfor consistency.
-
XRectangle2D
public XRectangle2D(Rectangle2D rect)
Construct a rectangle with the same coordinates than the supplied rectangle.- Parameters:
rect- The rectangle, ornullin none (in which case this constructor is equivalents to the no-argument constructor). UseINFINITYfor initializing thisXRectangle2Dwith 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 theRectangularShapeis empty. When theRectangularShapeis empty, it encloses no area.- Specified by:
isEmptyin classRectangularShape- Returns:
trueif theRectangularShapeis empty;falseotherwise.
-
getX
public double getX()
Returns the X coordinate of the upper left corner of the framing rectangle indoubleprecision.- Specified by:
getXin 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 indoubleprecision.- Specified by:
getYin 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 indoubleprecision.- Specified by:
getWidthin classRectangularShape- Returns:
- the width of the framing rectangle.
-
getHeight
public double getHeight()
Returns the height of the framing rectangle indoubleprecision.- Specified by:
getHeightin classRectangularShape- Returns:
- the height of the framing rectangle.
-
getMinX
public double getMinX()
Returns the smallest X coordinate of the rectangle.- Overrides:
getMinXin classRectangularShape
-
getMinY
public double getMinY()
Returns the smallest Y coordinate of the rectangle.- Overrides:
getMinYin classRectangularShape
-
getMaxX
public double getMaxX()
Returns the largest X coordinate of the rectangle.- Overrides:
getMaxXin classRectangularShape
-
getMaxY
public double getMaxY()
Returns the largest Y coordinate of the rectangle.- Overrides:
getMaxYin classRectangularShape
-
getCenterX
public double getCenterX()
Returns the X coordinate of the center of the rectangle.- Overrides:
getCenterXin classRectangularShape
-
getCenterY
public double getCenterY()
Returns the Y coordinate of the center of the rectangle.- Overrides:
getCenterYin classRectangularShape
-
setRect
public void setRect(double x, double y, double width, double height)Sets the location and size of thisRectangle2Dto the specified double values.- Specified by:
setRectin classRectangle2D- Parameters:
x- the x coordinates to which to set the location of the upper left corner of thisRectangle2Dy- the y coordinates to which to set the location of the upper left corner of thisRectangle2Dwidth- the value to use to set the width of thisRectangle2Dheight- the value to use to set the height of thisRectangle2D
-
setRect
public void setRect(Rectangle2D r)
Sets thisRectangle2Dto be the same as the specifiedRectangle2D.- Overrides:
setRectin classRectangle2D- Parameters:
r- the specifiedRectangle2D
-
intersects
public boolean intersects(double x, double y, double width, double height)Tests if the interior of thisRectangle2Dintersects the interior of a specified set of rectangular coordinates.- Specified by:
intersectsin interfaceShape- Overrides:
intersectsin 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:
trueif thisRectangle2Dintersects the interior of a specified set of rectangular coordinates;falseotherwise.
-
intersects
public boolean intersects(Rectangle2D rect)
Tests if the interior of this shape intersects the interior of a specified rectangle. This methods overrides the defaultRectangle2Dimplementation in order to work correctly with infinites and NaN values.- Specified by:
intersectsin interfaceShape- Overrides:
intersectsin classRectangularShape- Parameters:
rect- the specified rectangle.- Returns:
trueif 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
Shapecontract). 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:
trueif 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 theShapeintersects the interior of a specified rectangle. This method might conservatively returntruewhen 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 accordingShapecontract). 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:
trueif 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)
Returnstrueif 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 thisRectangle2Dentirely contains the specified set of rectangular coordinates.- Specified by:
containsin interfaceShape- Overrides:
containsin 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:
trueif thisRectangle2Dentirely contains specified set of rectangular coordinates;falseotherwise.
-
contains
public boolean contains(Rectangle2D rect)
Tests if the interior of this shape entirely contains the specified rectangle. This methods overrides the defaultRectangle2Dimplementation in order to work correctly with infinites and NaN values.- Specified by:
containsin interfaceShape- Overrides:
containsin classRectangularShape- Parameters:
rect- the specified rectangle.- Returns:
trueif 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:
containsin interfaceShape- Overrides:
containsin classRectangle2D- Parameters:
x- the x coordinates to test.y- the y coordinates to test.- Returns:
trueif the specified coordinates are inside the boundary of thisRectangle2D;falseotherwise.
-
containsInclusive
public static boolean containsInclusive(Rectangle2D outter, Rectangle2D inner)
Tests if the interior of theinnerrectangle is contained in the interior and/or the edge of theoutterrectangle. 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:
trueif the interior ofinneris 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:
outcodein 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 newRectangle2Dobject representing the intersection of thisRectangle2Dwith the specifiedRectangle2D.- Specified by:
createIntersectionin classRectangle2D- Parameters:
rect- theRectangle2Dto be intersected with thisRectangle2D- Returns:
- the largest
Rectangle2Dcontained in both the specifiedRectangle2Dand in thisRectangle2D.
-
createUnion
public Rectangle2D createUnion(Rectangle2D rect)
Returns a newRectangle2Dobject representing the union of thisRectangle2Dwith the specifiedRectangle2D.- Specified by:
createUnionin classRectangle2D- Parameters:
rect- theRectangle2Dto be combined with thisRectangle2D- Returns:
- the smallest
Rectangle2Dcontaining both the specifiedRectangle2Dand thisRectangle2D.
-
add
public void add(double x, double y)Adds a point, specified by the double precision argumentsxandy, to thisRectangle2D. The resultingRectangle2Dis the smallestRectangle2Dthat contains both the originalRectangle2Dand the specified point.After adding a point, a call to
containswith the added point as an argument does not necessarily returntrue. Thecontainsmethod does not returntruefor 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,containsreturnsfalsefor that point.- Overrides:
addin classRectangle2D
-
add
public void add(Rectangle2D rect)
Adds aRectangle2Dobject to thisRectangle2D. The resultingRectangle2Dis the union of the twoRectangle2Dobjects.- Overrides:
addin classRectangle2D- Parameters:
rect- theRectangle2Dto add to thisRectangle2D.
-
-