Class IntervalRectangle

java.lang.Object
java.awt.geom.RectangularShape
java.awt.geom.Rectangle2D
org.apache.sis.internal.referencing.j2d.IntervalRectangle
All Implemented Interfaces:
Shape, Cloneable
Direct Known Subclasses:
DatumShiftGridGroup.Region

public class IntervalRectangle extends Rectangle2D
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(…) and intersects(…) methods since there is no addition or subtraction to perform.
  • Better inter-operability with Envelope2D when a rectangle spans the anti-meridian. This IntervalRectangle class does not support such envelopes by itself, but it is okay to create a rectangle with negative width and gives it in argument to Envelope2D.contains(Rectangle2D) or Envelope2D.intersects(Rectangle2D) methods.
This class provides the following additional methods which are not defined in 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

    Fields
    Modifier and Type
    Field
    Description
    double
    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

    Constructors
    Constructor
    Description
    Constructs 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 Type
    Method
    Description
    final void
    add(double x, double y)
    Adds a point, specified by the arguments x and y, to this rectangle.
    final void
    Adds a Rectangle2D object to this rectangle.
    final boolean
    contains(double x, double y)
    Tests if a specified coordinate is inside the boundary of this Rectangle2D.
    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
    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 this Rectangle2D with all edges inclusive.
    Returns a new Rectangle2D object representing the intersection of this rectangle with the specified one.
    Returns a new Rectangle2D 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
    Returns the maximal x coordinate value.
    final double
    Returns the maximal y coordinate value.
    final double
    Returns the minimal x coordinate value.
    final double
    Returns the minimal y coordinate value.
    final double
    Returns the width of the rectangle.
    final double
    Returns the minimal x coordinate value.
    final double
    Returns the minimal y coordinate value.
    final void
    Intersects a Rectangle2D 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
    Tests if the interior of this shape intersects the interior of a specified rectangle.
    final boolean
    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.
    Returns the String representation of this Rectangle2D.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • xmin

      public double xmin
      Minimal x coordinate value.
    • ymin

      public double ymin
      Minimal y coordinate value.
    • xmax

      public double xmax
      Maximal x coordinate value.
    • ymax

      public double ymax
      Maximal 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 assigns lower coordinates to xmin, ymin and upper coordinates to xmax, ymax regardless of their values; this constructor does not verify if lower coordinates are smaller than upper coordinates. This is sometimes useful for creating a rectangle crossing the anti-meridian, even if IntervalRectangle 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:
      • 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:
      isEmpty in class RectangularShape
      Returns:
      true if this rectangle is empty; false otherwise.
    • getX

      public final double getX()
      Returns the minimal x coordinate value.
      Specified by:
      getX in class RectangularShape
      Returns:
      the minimal x coordinate value.
    • getY

      public final double getY()
      Returns the minimal y coordinate value.
      Specified by:
      getY in class RectangularShape
      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. This IntervalRectangle class does not support such envelopes itself, but other classes like Envelope2D will handle correctly the negative width.
      Specified by:
      getWidth in class RectangularShape
      Returns:
      the width of the rectangle.
    • getHeight

      public final double getHeight()
      Returns the height of the rectangle.
      Specified by:
      getHeight in class RectangularShape
      Returns:
      the height of the rectangle.
    • getMinX

      public final double getMinX()
      Returns the minimal x coordinate value.
      Overrides:
      getMinX in class RectangularShape
      Returns:
      the minimal x coordinate value.
    • getMinY

      public final double getMinY()
      Returns the minimal y coordinate value.
      Overrides:
      getMinY in class RectangularShape
      Returns:
      the minimal y coordinate value.
    • getMaxX

      public final double getMaxX()
      Returns the maximal x coordinate value.
      Overrides:
      getMaxX in class RectangularShape
      Returns:
      the maximal x coordinate value.
    • getMaxY

      public final double getMaxY()
      Returns the maximal y coordinate value.
      Overrides:
      getMaxY in class RectangularShape
      Returns:
      the maximal y coordinate value.
    • getCenterX

      public final double getCenterX()
      Returns the x coordinate of the center of the rectangle.
      Overrides:
      getCenterX in class RectangularShape
      Returns:
      the median x coordinate value.
    • getCenterY

      public final double getCenterY()
      Returns the y coordinate of the center of the rectangle.
      Overrides:
      getCenterY in class RectangularShape
      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 class Rectangle2D
      Parameters:
      x - the x minimal coordinate value.
      y - the y minimal coordinate value.
      width - the rectangle width.
      height - the rectangle height.
    • setRect

      public final void setRect(Rectangle2D r)
      Sets this rectangle to be the same as the specified rectangle.
      Overrides:
      setRect in class Rectangle2D
      Parameters:
      r - the rectangle to copy values from.
    • setFrame

      public final void setFrame(Rectangle2D r)
      Sets the framing rectangle to the given rectangle. The current implementation delegates to setRect(Rectangle2D). This is consistent with the default implementation of Rectangle2D.setFrame(double, double, double, double), which delegates to the corresponding method of setRect.
      Overrides:
      setFrame in class RectangularShape
    • 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 returns false if the two rectangles just touch to each other.
      Specified by:
      intersects in interface Shape
      Overrides:
      intersects in class Rectangle2D
      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

      public final boolean intersects(Rectangle2D rect)
      Tests if the interior of this shape intersects the interior of a specified rectangle. The edges are considered exclusive; this method returns false if the two rectangles just touch to each other.
      Specified by:
      intersects in interface Shape
      Overrides:
      intersects in class RectangularShape
      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 interface Shape
      Overrides:
      contains in class Rectangle2D
      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

      public final boolean contains(Rectangle2D rect)
      Tests if the interior of this shape entirely contains the specified rectangle. This methods overrides the default Rectangle2D implementation in order to work correctly with infinites and NaN values.
      Specified by:
      contains in interface Shape
      Overrides:
      contains in class RectangularShape
      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 this Rectangle2D. The maximal x and y values (i.e. the right and bottom edges of this rectangle) are exclusive.
      Specified by:
      contains in interface Shape
      Overrides:
      contains in class Rectangle2D
      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 this Rectangle2D 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 this Rectangle2D, whether or not the specified coordinates are on the same side of the edge as the rest of this Rectangle2D.
      Specified by:
      outcode in class Rectangle2D
      Returns:
      the logical OR of all appropriate out codes.
      See Also:
    • intersect

      public final void intersect(Rectangle2D rect)
      Intersects a Rectangle2D object with this rectangle. The resulting* rectangle is the intersection of the two Rectangle2D objects. Invoking this method is equivalent to invoking the following code, except that this method behaves correctly with infinite values and Envelope2D implementation.
      Parameters:
      rect - the Rectangle2D to intersect with this rectangle.
      See Also:
    • createIntersection

      public final Rectangle2D createIntersection(Rectangle2D rect)
      Returns a new Rectangle2D object representing the intersection of this rectangle with the specified one.
      Specified by:
      createIntersection in class Rectangle2D
      Parameters:
      rect - the Rectangle2D to be intersected with this rectangle.
      Returns:
      the largest Rectangle2D contained in both the specified rectangle and this one.
      See Also:
    • createUnion

      public final Rectangle2D createUnion(Rectangle2D rect)
      Returns a new Rectangle2D object representing the union of this rectangle with the specified one.
      Specified by:
      createUnion in class Rectangle2D
      Parameters:
      rect - the Rectangle2D to be combined with this rectangle.
      Returns:
      the smallest Rectangle2D containing both the specified Rectangle2D and this one.
    • add

      public final void add(double x, double y)
      Adds a point, specified by the arguments x and y, to this rectangle. The resulting Rectangle2D 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 return true. The contains method does not return true 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 returns false for that point.

      Overrides:
      add in class Rectangle2D
      Parameters:
      x - x coordinate value of the point to add.
      y - y coordinate value of the point to add.
    • add

      public final void add(Rectangle2D rect)
      Adds a Rectangle2D object to this rectangle. The resulting rectangle is the union of the two Rectangle2D objects.
      Overrides:
      add in class Rectangle2D
      Parameters:
      rect - the Rectangle2D to add to this rectangle.
    • toString

      public String toString()
      Returns the String representation of this Rectangle2D. The coordinate order is (xmin, ymin, xmax, ymax), which is consistent with the IntervalRectangle(double, double, double, double) constructor and with the BBOX Well Known Text (WKT) syntax.
      Overrides:
      toString in class Object
      Returns:
      a String representing this Rectangle2D.