Class IntervalRectangle
java.lang.Object
java.awt.geom.RectangularShape
java.awt.geom.Rectangle2D
org.apache.sis.internal.referencing.j2d.IntervalRectangle
- Direct Known Subclasses:
DatumShiftGridGroup.Region
Rectangle defines by intervals instead of by a size.
Instead of (
x, y, width, height) values as do standard Java2D implementations,
this class contains (xmin, xmax, ymin, ymax) values. This choice provides
three benefits:
- Allows this class to work correctly with infinite and NaN values. By comparison, the (width, height) alternative is ambiguous.
- Slightly faster
contains(…)andintersects(…)methods since there is no addition or subtraction to perform. - Better inter-operability with
Envelope2Dwhen a rectangle spans the anti-meridian. ThisIntervalRectangleclass does not support such envelopes by itself, but it is okay to create a rectangle with negative width and gives it in argument toEnvelope2D.contains(Rectangle2D)orEnvelope2D.intersects(Rectangle2D)methods.
Rectangle2D:
This class does not support by itself rectangles crossing the anti-meridian of a geographic CRS.
However, the getX(), getY(), getWidth() and getHeight() methods are defined in
the straightforward way expected by Envelope2D.intersects(Rectangle2D) and similar methods for computing
correct result if the given Rectangle2D crosses the anti-meridian.
Internal usage of inheritance:
this class may also be opportunistically extended by some Apache SIS internal classes that need a rectangle in
addition of their own information. All
Rectangle2D methods are declared final for reducing the risk of
confusion with other aspects managed by subclasses. We don't do that in public API because this is not a
recommended approach, but for Apache SIS private classes this is a way to reduce pressure on garbage collector.- Since:
- 0.8
- Version:
- 1.1
-
Nested Class Summary
Nested classes/interfaces inherited from class java.awt.geom.Rectangle2D
Rectangle2D.Double, Rectangle2D.Float -
Field Summary
FieldsModifier and TypeFieldDescriptiondoubleMaximal x coordinate value.doubleMinimal x coordinate value.doubleMaximal y coordinate value.doubleMinimal y coordinate value.Fields inherited from class java.awt.geom.Rectangle2D
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a default rectangle initialized to(0,0,0,0).IntervalRectangle(double xmin, double ymin, double xmax, double ymax) Creates a rectangle using maximal x and y values rather than width and height.IntervalRectangle(org.opengis.geometry.DirectPosition lower, org.opengis.geometry.DirectPosition upper) Constructs a rectangle initialized to the two first dimensions of the given corners.IntervalRectangle(org.opengis.geometry.Envelope envelope) Constructs a rectangle initialized to the two first dimensions of the given envelope. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidadd(double x, double y) Adds a point, specified by the argumentsxandy, to this rectangle.final voidadd(Rectangle2D rect) Adds aRectangle2Dobject to this rectangle.final booleancontains(double x, double y) Tests if a specified coordinate is inside the boundary of thisRectangle2D.final booleancontains(double x, double y, double width, double height) Tests if the interior of this rectangle entirely contains the specified set of rectangular coordinates.final booleancontains(Rectangle2D rect) Tests if the interior of this shape entirely contains the specified rectangle.final booleancontainsInclusive(double x, double y) Tests if a specified coordinate is inside the boundary of thisRectangle2Dwith all edges inclusive.final Rectangle2DReturns a newRectangle2Dobject representing the intersection of this rectangle with the specified one.final Rectangle2DcreateUnion(Rectangle2D rect) Returns a newRectangle2Dobject representing the union of this rectangle with the specified one.final doubledistanceSquared(double x, double y) Returns the square of the minimal distance between a point and this rectangle.final doubleReturns the x coordinate of the center of the rectangle.final doubleReturns the y coordinate of the center of the rectangle.final doubleReturns the height of the rectangle.final doublegetMaxX()Returns the maximal x coordinate value.final doublegetMaxY()Returns the maximal y coordinate value.final doublegetMinX()Returns the minimal x coordinate value.final doublegetMinY()Returns the minimal y coordinate value.final doublegetWidth()Returns the width of the rectangle.final doublegetX()Returns the minimal x coordinate value.final doublegetY()Returns the minimal y coordinate value.final voidintersect(Rectangle2D rect) Intersects aRectangle2Dobject with this rectangle.final booleanintersects(double x, double y, double width, double height) Tests if the interior of this rectangle intersects the interior of a specified set of rectangular coordinates.final booleanintersects(Rectangle2D rect) Tests if the interior of this shape intersects the interior of a specified rectangle.final booleanisEmpty()Determines whether this rectangle is empty.final intoutcode(double x, double y) Determines where the specified coordinates lie with respect to this rectangle.final voidSets the framing rectangle to the given rectangle.final voidsetRect(double x, double y, double width, double height) Sets the location and size of this rectangle to the specified values.final voidSets this rectangle to be the same as the specified rectangle.toString()Returns theStringrepresentation of thisRectangle2D.Methods inherited from class java.awt.geom.Rectangle2D
add, equals, getBounds2D, getPathIterator, getPathIterator, hashCode, intersect, intersectsLine, intersectsLine, outcode, setFrame, unionMethods inherited from class java.awt.geom.RectangularShape
clone, contains, getBounds, getFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal
-
Field Details
-
xmin
public double xminMinimal x coordinate value. -
ymin
public double yminMinimal y coordinate value. -
xmax
public double xmaxMaximal x coordinate value. -
ymax
public double ymaxMaximal y coordinate value.
-
-
Constructor Details
-
IntervalRectangle
public IntervalRectangle()Constructs a default rectangle initialized to(0,0,0,0). -
IntervalRectangle
public IntervalRectangle(org.opengis.geometry.Envelope envelope) Constructs a rectangle initialized to the two first dimensions of the given envelope. If the given envelope crosses the anti-meridian, then the new rectangle will span the full longitude range (i.e. this constructor does not preserve the convention of using negative width for envelopes crossing anti-meridian).Note: this constructor expands envelopes that cross the anti-meridian because the methods defined in this class are not designed for handling such envelopes. If a rectangle with negative width is nevertheless desired for envelope crossing the anti-meridian, one can use the following constructor:- Parameters:
envelope- the envelope from which to copy the values.
-
IntervalRectangle
public IntervalRectangle(org.opengis.geometry.DirectPosition lower, org.opengis.geometry.DirectPosition upper) Constructs a rectangle initialized to the two first dimensions of the given corners. This constructor unconditionally assignslowercoordinates toxmin,yminanduppercoordinates toxmax,ymaxregardless of their values; this constructor does not verify iflowercoordinates are smaller thanuppercoordinates. This is sometimes useful for creating a rectangle crossing the anti-meridian, even ifIntervalRectangleclass does not support such rectangles by itself.- Parameters:
lower- the limits in the direction of decreasing coordinate values for each dimension.upper- the limits in the direction of increasing coordinate values for each dimension.- See Also:
-
Envelope.getLowerCorner()Envelope.getUpperCorner()
-
IntervalRectangle
public IntervalRectangle(double xmin, double ymin, double xmax, double ymax) Creates a rectangle using maximal x and y values rather than width and height. This constructor avoid the problem of NaN values when extremum are infinite numbers.- Parameters:
xmin- minimal x coordinate value.ymin- minimal y coordinate value.xmax- maximal x coordinate value.ymax- maximal y coordinate value.
-
-
Method Details
-
isEmpty
public final boolean isEmpty()Determines whether this rectangle is empty. If this rectangle has at least one NaN value, then it is considered empty.- Specified by:
isEmptyin classRectangularShape- Returns:
trueif this rectangle is empty;falseotherwise.
-
getX
public final double getX()Returns the minimal x coordinate value.- Specified by:
getXin classRectangularShape- Returns:
- the minimal x coordinate value.
-
getY
public final double getY()Returns the minimal y coordinate value.- Specified by:
getYin classRectangularShape- Returns:
- the minimal y coordinate value.
-
getWidth
public final double getWidth()Returns the width of the rectangle. May be negative if the rectangle crosses the anti-meridian. ThisIntervalRectangleclass does not support such envelopes itself, but other classes likeEnvelope2Dwill handle correctly the negative width.- Specified by:
getWidthin classRectangularShape- Returns:
- the width of the rectangle.
-
getHeight
public final double getHeight()Returns the height of the rectangle.- Specified by:
getHeightin classRectangularShape- Returns:
- the height of the rectangle.
-
getMinX
public final double getMinX()Returns the minimal x coordinate value.- Overrides:
getMinXin classRectangularShape- Returns:
- the minimal x coordinate value.
-
getMinY
public final double getMinY()Returns the minimal y coordinate value.- Overrides:
getMinYin classRectangularShape- Returns:
- the minimal y coordinate value.
-
getMaxX
public final double getMaxX()Returns the maximal x coordinate value.- Overrides:
getMaxXin classRectangularShape- Returns:
- the maximal x coordinate value.
-
getMaxY
public final double getMaxY()Returns the maximal y coordinate value.- Overrides:
getMaxYin classRectangularShape- Returns:
- the maximal y coordinate value.
-
getCenterX
public final double getCenterX()Returns the x coordinate of the center of the rectangle.- Overrides:
getCenterXin classRectangularShape- Returns:
- the median x coordinate value.
-
getCenterY
public final double getCenterY()Returns the y coordinate of the center of the rectangle.- Overrides:
getCenterYin classRectangularShape- Returns:
- the median y coordinate value.
-
setRect
public final void setRect(double x, double y, double width, double height) Sets the location and size of this rectangle to the specified values.- Specified by:
setRectin classRectangle2D- Parameters:
x- the x minimal coordinate value.y- the y minimal coordinate value.width- the rectangle width.height- the rectangle height.
-
setRect
Sets this rectangle to be the same as the specified rectangle.- Overrides:
setRectin classRectangle2D- Parameters:
r- the rectangle to copy values from.
-
setFrame
Sets the framing rectangle to the given rectangle. The current implementation delegates tosetRect(Rectangle2D). This is consistent with the default implementation ofRectangle2D.setFrame(double, double, double, double), which delegates to the corresponding method ofsetRect.- Overrides:
setFramein classRectangularShape
-
intersects
public final boolean intersects(double x, double y, double width, double height) Tests if the interior of this rectangle intersects the interior of a specified set of rectangular coordinates. The edges are considered exclusive; this method returnsfalseif the two rectangles just touch to each other.- Specified by:
intersectsin interfaceShape- Overrides:
intersectsin classRectangle2D- Parameters:
x- the x minimal coordinate value.y- the y minimal coordinate value.width- the rectangle width.height- the rectangle height.- Returns:
trueif this rectangle intersects the interior of the specified set of rectangular coordinates.
-
intersects
Tests if the interior of this shape intersects the interior of a specified rectangle. The edges are considered exclusive; this method returnsfalseif the two rectangles just touch to each other.- Specified by:
intersectsin interfaceShape- Overrides:
intersectsin classRectangularShape- Parameters:
rect- the specified rectangle.- Returns:
trueif this shape and the specified rectangle intersect each other.
-
contains
public final boolean contains(double x, double y, double width, double height) Tests if the interior of this rectangle entirely contains the specified set of rectangular coordinates.- Specified by:
containsin interfaceShape- Overrides:
containsin classRectangle2D- Parameters:
x- the x minimal coordinate value.y- the y minimal coordinate value.width- the rectangle width.height- the rectangle height.- Returns:
trueif this rectangle entirely contains specified set of rectangular coordinates.
-
contains
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 final boolean contains(double x, double y) Tests if a specified coordinate is inside the boundary of thisRectangle2D. The maximal x and y values (i.e. the right and bottom edges of this rectangle) are exclusive.- 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 this rectangle.
-
containsInclusive
public final boolean containsInclusive(double x, double y) Tests if a specified coordinate is inside the boundary of thisRectangle2Dwith all edges inclusive.- Parameters:
x- the x coordinates to test.y- the y coordinates to test.- Returns:
trueif the specified coordinates are inside this rectangle, all edges included.
-
distanceSquared
public final double distanceSquared(double x, double y) Returns the square of the minimal distance between a point and this rectangle. If the point is inside the rectangle or on the edge, then this method returns 0.- Parameters:
x- the x coordinates to test.y- the y coordinates to test.- Returns:
- square of minimal distance, or 0 if the point is inside this rectangle.
-
outcode
public final int outcode(double x, double y) Determines where the specified coordinates lie with respect to this rectangle. 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:
-
intersect
Intersects aRectangle2Dobject with this rectangle. The resulting* rectangle is the intersection of the twoRectangle2Dobjects. Invoking this method is equivalent to invoking the following code, except that this method behaves correctly with infinite values andEnvelope2Dimplementation.- Parameters:
rect- theRectangle2Dto intersect with this rectangle.- See Also:
-
createIntersection
Returns a newRectangle2Dobject representing the intersection of this rectangle with the specified one.- Specified by:
createIntersectionin classRectangle2D- Parameters:
rect- theRectangle2Dto be intersected with this rectangle.- Returns:
- the largest
Rectangle2Dcontained in both the specified rectangle and this one. - See Also:
-
createUnion
Returns a newRectangle2Dobject representing the union of this rectangle with the specified one.- Specified by:
createUnionin classRectangle2D- Parameters:
rect- theRectangle2Dto be combined with this rectangle.- Returns:
- the smallest
Rectangle2Dcontaining both the specifiedRectangle2Dand this one.
-
add
public final void add(double x, double y) Adds a point, specified by the argumentsxandy, to this rectangle. The resultingRectangle2Dis the smallest rectangle that contains both the original rectangle and 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- Parameters:
x- x coordinate value of the point to add.y- y coordinate value of the point to add.
-
add
Adds aRectangle2Dobject to this rectangle. The resulting rectangle is the union of the twoRectangle2Dobjects.- Overrides:
addin classRectangle2D- Parameters:
rect- theRectangle2Dto add to this rectangle.
-
toString
Returns theStringrepresentation of thisRectangle2D. The coordinate order is (xmin, ymin, xmax, ymax), which is consistent with theIntervalRectangle(double, double, double, double)constructor and with theBBOXWell Known Text (WKT) syntax.
-