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
Envelope2D
when a rectangle spans the anti-meridian. ThisIntervalRectangle
class 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 TypeFieldDescriptiondouble
Maximal x coordinate value.double
Minimal x coordinate value.double
Maximal y coordinate value.double
Minimal 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 void
add
(double x, double y) Adds a point, specified by the argumentsx
andy
, to this rectangle.final void
add
(Rectangle2D rect) Adds aRectangle2D
object to this rectangle.final boolean
contains
(double x, double y) Tests if a specified coordinate is inside the boundary of thisRectangle2D
.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.final boolean
contains
(Rectangle2D rect) Tests if the interior of this shape entirely contains the specified rectangle.final boolean
containsInclusive
(double x, double y) Tests if a specified coordinate is inside the boundary of thisRectangle2D
with all edges inclusive.final Rectangle2D
Returns a newRectangle2D
object representing the intersection of this rectangle with the specified one.final Rectangle2D
createUnion
(Rectangle2D rect) Returns a newRectangle2D
object representing the union of this rectangle with the specified one.final double
distanceSquared
(double x, double y) Returns the square of the minimal distance between a point and this rectangle.final double
Returns the x coordinate of the center of the rectangle.final double
Returns the y coordinate of the center of the rectangle.final double
Returns the height of the rectangle.final double
getMaxX()
Returns the maximal x coordinate value.final double
getMaxY()
Returns the maximal y coordinate value.final double
getMinX()
Returns the minimal x coordinate value.final double
getMinY()
Returns the minimal y coordinate value.final double
getWidth()
Returns the width of the rectangle.final double
getX()
Returns the minimal x coordinate value.final double
getY()
Returns the minimal y coordinate value.final void
intersect
(Rectangle2D rect) Intersects aRectangle2D
object with this rectangle.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.final boolean
intersects
(Rectangle2D rect) Tests if the interior of this shape intersects the interior of a specified rectangle.final boolean
isEmpty()
Determines whether this rectangle is empty.final int
outcode
(double x, double y) Determines where the specified coordinates lie with respect to this rectangle.final void
Sets the framing rectangle to the given rectangle.final void
setRect
(double x, double y, double width, double height) Sets the location and size of this rectangle to the specified values.final void
Sets this rectangle to be the same as the specified rectangle.toString()
Returns theString
representation of thisRectangle2D
.Methods inherited from class java.awt.geom.Rectangle2D
add, equals, getBounds2D, getPathIterator, getPathIterator, hashCode, intersect, intersectsLine, intersectsLine, outcode, setFrame, union
Methods 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 assignslower
coordinates toxmin
,ymin
andupper
coordinates toxmax
,ymax
regardless of their values; this constructor does not verify iflower
coordinates are smaller thanupper
coordinates. This is sometimes useful for creating a rectangle crossing the anti-meridian, even ifIntervalRectangle
class 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:
-
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:
isEmpty
in classRectangularShape
- Returns:
true
if this rectangle is empty;false
otherwise.
-
getX
public final double getX()Returns the minimal x coordinate value.- Specified by:
getX
in classRectangularShape
- Returns:
- the minimal x coordinate value.
-
getY
public final double getY()Returns the minimal y coordinate value.- Specified by:
getY
in 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. ThisIntervalRectangle
class does not support such envelopes itself, but other classes likeEnvelope2D
will handle correctly the negative width.- Specified by:
getWidth
in classRectangularShape
- Returns:
- the width of the rectangle.
-
getHeight
public final double getHeight()Returns the height of the rectangle.- Specified by:
getHeight
in classRectangularShape
- Returns:
- the height of the rectangle.
-
getMinX
public final double getMinX()Returns the minimal x coordinate value.- Overrides:
getMinX
in classRectangularShape
- Returns:
- the minimal x coordinate value.
-
getMinY
public final double getMinY()Returns the minimal y coordinate value.- Overrides:
getMinY
in classRectangularShape
- Returns:
- the minimal y coordinate value.
-
getMaxX
public final double getMaxX()Returns the maximal x coordinate value.- Overrides:
getMaxX
in classRectangularShape
- Returns:
- the maximal x coordinate value.
-
getMaxY
public final double getMaxY()Returns the maximal y coordinate value.- Overrides:
getMaxY
in classRectangularShape
- Returns:
- the maximal y coordinate value.
-
getCenterX
public final double getCenterX()Returns the x coordinate of the center of the rectangle.- Overrides:
getCenterX
in classRectangularShape
- Returns:
- the median x coordinate value.
-
getCenterY
public final double getCenterY()Returns the y coordinate of the center of the rectangle.- Overrides:
getCenterY
in 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:
setRect
in 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:
setRect
in 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:
setFrame
in 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 returnsfalse
if the two rectangles just touch to each other.- Specified by:
intersects
in interfaceShape
- Overrides:
intersects
in classRectangle2D
- Parameters:
x
- the x minimal coordinate value.y
- the y minimal coordinate value.width
- the rectangle width.height
- the rectangle height.- Returns:
true
if 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 returnsfalse
if the two rectangles just touch to each other.- 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.
-
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:
contains
in interfaceShape
- Overrides:
contains
in classRectangle2D
- Parameters:
x
- the x minimal coordinate value.y
- the y minimal coordinate value.width
- the rectangle width.height
- the rectangle height.- Returns:
true
if 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 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 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:
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 this rectangle.
-
containsInclusive
public final boolean containsInclusive(double x, double y) Tests if a specified coordinate is inside the boundary of thisRectangle2D
with all edges inclusive.- Parameters:
x
- the x coordinates to test.y
- the y coordinates to test.- Returns:
true
if 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:
outcode
in classRectangle2D
- Returns:
- the logical OR of all appropriate out codes.
- See Also:
-
intersect
Intersects aRectangle2D
object with this rectangle. The resulting* rectangle is the intersection of the twoRectangle2D
objects. Invoking this method is equivalent to invoking the following code, except that this method behaves correctly with infinite values andEnvelope2D
implementation.- Parameters:
rect
- theRectangle2D
to intersect with this rectangle.- See Also:
-
createIntersection
Returns a newRectangle2D
object representing the intersection of this rectangle with the specified one.- Specified by:
createIntersection
in classRectangle2D
- Parameters:
rect
- theRectangle2D
to be intersected with this rectangle.- Returns:
- the largest
Rectangle2D
contained in both the specified rectangle and this one. - See Also:
-
createUnion
Returns a newRectangle2D
object representing the union of this rectangle with the specified one.- Specified by:
createUnion
in classRectangle2D
- Parameters:
rect
- theRectangle2D
to be combined with this rectangle.- Returns:
- the smallest
Rectangle2D
containing both the specifiedRectangle2D
and this one.
-
add
public final void add(double x, double y) Adds a point, specified by the argumentsx
andy
, to this rectangle. The resultingRectangle2D
is the smallest rectangle that contains both the original rectangle 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
- Parameters:
x
- x coordinate value of the point to add.y
- y coordinate value of the point to add.
-
add
Adds aRectangle2D
object to this rectangle. The resulting rectangle is the union of the twoRectangle2D
objects.- Overrides:
add
in classRectangle2D
- Parameters:
rect
- theRectangle2D
to add to this rectangle.
-
toString
Returns theString
representation of thisRectangle2D
. The coordinate order is (xmin, ymin, xmax, ymax), which is consistent with theIntervalRectangle(double, double, double, double)
constructor and with theBBOX
Well Known Text (WKT) syntax.
-