Class R2Rect

  • All Implemented Interfaces:
    java.io.Serializable

    @GwtCompatible(serializable=true)
    public final class R2Rect
    extends java.lang.Object
    implements java.io.Serializable
    An R2Rect represents a closed axis-aligned rectangle in the (x,y) plane. This class is mutable to allow iteratively constructing bounds via e.g. addPoint(R2Vector).
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  R2Rect.Axis
      Valid axes.
    • Constructor Summary

      Constructors 
      Constructor Description
      R2Rect()
      Creates an empty R2Rect.
      R2Rect​(R1Interval x, R1Interval y)
      Constructs a rectangle from the given intervals in x and y.
      R2Rect​(R2Rect rect)
      Copy constructor.
      R2Rect​(R2Vector lo, R2Vector hi)
      Constructs a rectangle from the given lower-left and upper-right points.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addPoint​(R2Vector p)
      Increase the size of the bounding rectangle to include the given point.
      void addRect​(R2Rect other)
      Expand the rectangle to include the given other rectangle.
      boolean approxEquals​(R2Rect other)
      Returns true if the x- and y-intervals of the two rectangles are the same up to the given tolerance.
      boolean approxEquals​(R2Rect other, double maxError)
      Returns true if the given rectangles are equal to within maxError.
      R2Vector clampPoint​(R2Vector p)
      Return the closest point in this rectangle to the given point "p".
      boolean contains​(R2Rect other)
      Returns true if and only if this rectangle contains the given other rectangle.
      boolean contains​(R2Vector p)
      Returns true if this rectangle contains the given point.
      static R2Rect empty()
      Returns a new instance of the canonical empty rectangle.
      boolean equals​(java.lang.Object other)
      Returns true if two rectangles contains the same set of points.
      (package private) void expand​(double margin)
      Expands this rectangle on both axes by the given margin.
      R2Rect expanded​(double margin)
      Returns a rectangle that has been expanded on both sides by the given margin.
      R2Rect expanded​(R2Vector margin)
      Return a rectangle that has been expanded on each side in the x-direction by margin.x(), and on each side in the y-direction by margin.y().
      static R2Rect fromCenterSize​(R2Vector center, R2Vector size)
      Returns a new rectangle from a center point and size in each dimension.
      static R2Rect fromPoint​(R2Vector p)
      Returns a rectangle containing a single point.
      static R2Rect fromPointPair​(R2Vector p1, R2Vector p2)
      Returns the minimal bounding rectangle containing the two given points.
      R2Vector getCenter()
      Returns the center of this rectangle in (x,y)-space.
      R1Interval getInterval​(R2Rect.Axis axis)
      Returns the interval for the given axis, which must not be null.
      R2Vector getSize()
      Return the width and height of this rectangle in (x,y)-space.
      R2Vector getVertex​(int k)
      Returns the kth vertex of this rectangle (k = 0,1,2,3) in CCW order.
      R2Vector getVertex​(int i, int j)
      Returns the vertex in direction "i" along the x-axis (0=left, 1=right) and direction "j" along the y-axis (0=down, 1=up).
      int hashCode()
      Returns a simple convolution hashcodes from the x and y internals.
      R2Vector hi()
      Returns the point in this rectangle with the maximum x and y values.
      boolean interiorContains​(R2Rect other)
      Returns true if and only if the interior of this rectangle contains all points of the given other rectangle (including its boundary).
      boolean interiorContains​(R2Vector p)
      Returns true if and only if the given point is contained in the interior of the region (i.e.
      boolean interiorIntersects​(R2Rect other)
      Return true if and only if the interior of this rectangle intersects any point (including the boundary) of the given other rectangle.
      R2Rect intersection​(R2Rect other)
      Returns the smallest rectangle containing the intersection of this rectangle and the given rectangle.
      boolean intersects​(R2Rect other)
      Returns true if this rectangle and the given other rectangle have any points in common.
      boolean isEmpty()
      Return true if this rectangle is empty, i.e.
      boolean isValid()
      Returns true if this rectangle is valid, which essentially just means that if the bound for either axis is empty then both must be.
      R2Vector lo()
      Returns the point in this rectangle with the minimum x and y values.
      java.lang.String toString()
      Returns a simple string representation of this rectangle's lower and upper corners.
      R2Rect union​(R2Rect other)
      Returns the smallest rectangle containing the union of this rectangle and the given rectangle.
      R1Interval x()
      Returns the interval along the x-axis.
      R1Interval y()
      Returns the interval along the y-axis.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • R2Rect

        public R2Rect()
        Creates an empty R2Rect.
      • R2Rect

        public R2Rect​(R2Vector lo,
                      R2Vector hi)
        Constructs a rectangle from the given lower-left and upper-right points.
      • R2Rect

        public R2Rect​(R1Interval x,
                      R1Interval y)
        Constructs a rectangle from the given intervals in x and y. The two intervals must either be both empty or both non-empty.
      • R2Rect

        public R2Rect​(R2Rect rect)
        Copy constructor.
    • Method Detail

      • empty

        public static R2Rect empty()
        Returns a new instance of the canonical empty rectangle. Use isEmpty() to test for empty rectangles, since they have more than one representation.
      • fromCenterSize

        public static R2Rect fromCenterSize​(R2Vector center,
                                            R2Vector size)
        Returns a new rectangle from a center point and size in each dimension. Both components of size should be non-negative, i.e. this method cannot be used to create an empty rectangle.
      • fromPoint

        public static R2Rect fromPoint​(R2Vector p)
        Returns a rectangle containing a single point.
      • fromPointPair

        public static R2Rect fromPointPair​(R2Vector p1,
                                           R2Vector p2)
        Returns the minimal bounding rectangle containing the two given points. This is equivalent to starting with an empty rectangle and calling addPoint() twice. Note that it is different than the R2Rect(lo, hi) constructor, where the first point is always used as the lower-left corner of the resulting rectangle.
      • x

        public R1Interval x()
        Returns the interval along the x-axis.
      • y

        public R1Interval y()
        Returns the interval along the y-axis.
      • lo

        public R2Vector lo()
        Returns the point in this rectangle with the minimum x and y values.
      • hi

        public R2Vector hi()
        Returns the point in this rectangle with the maximum x and y values.
      • isValid

        public boolean isValid()
        Returns true if this rectangle is valid, which essentially just means that if the bound for either axis is empty then both must be.
      • isEmpty

        public boolean isEmpty()
        Return true if this rectangle is empty, i.e. it contains no points at all.
      • getVertex

        public R2Vector getVertex​(int k)
        Returns the kth vertex of this rectangle (k = 0,1,2,3) in CCW order. Vertex 0 is in the lower-left corner.
      • getVertex

        public R2Vector getVertex​(int i,
                                  int j)
        Returns the vertex in direction "i" along the x-axis (0=left, 1=right) and direction "j" along the y-axis (0=down, 1=up). Equivalently, returns the vertex constructed by selecting endpoint "i" of the x-interval (0=lo, 1=hi) and vertex "j" of the y-interval.
      • getCenter

        public R2Vector getCenter()
        Returns the center of this rectangle in (x,y)-space.
      • getSize

        public R2Vector getSize()
        Return the width and height of this rectangle in (x,y)-space. Empty rectangles have a negative width and height.
      • getInterval

        public R1Interval getInterval​(R2Rect.Axis axis)
        Returns the interval for the given axis, which must not be null.
      • contains

        public boolean contains​(R2Vector p)
        Returns true if this rectangle contains the given point. Note that rectangles are closed regions, i.e. they contain their boundary.
      • interiorContains

        public boolean interiorContains​(R2Vector p)
        Returns true if and only if the given point is contained in the interior of the region (i.e. the region excluding its boundary).
      • contains

        public boolean contains​(R2Rect other)
        Returns true if and only if this rectangle contains the given other rectangle.
      • interiorContains

        public boolean interiorContains​(R2Rect other)
        Returns true if and only if the interior of this rectangle contains all points of the given other rectangle (including its boundary).
      • intersects

        public boolean intersects​(R2Rect other)
        Returns true if this rectangle and the given other rectangle have any points in common.
      • interiorIntersects

        public boolean interiorIntersects​(R2Rect other)
        Return true if and only if the interior of this rectangle intersects any point (including the boundary) of the given other rectangle.
      • addPoint

        public void addPoint​(R2Vector p)
        Increase the size of the bounding rectangle to include the given point. This rectangle is expanded by the minimum amount possible.
      • addRect

        public void addRect​(R2Rect other)
        Expand the rectangle to include the given other rectangle. This is the same as replacing the rectangle by the union of the two rectangles, but is somewhat more efficient.
      • clampPoint

        public R2Vector clampPoint​(R2Vector p)
        Return the closest point in this rectangle to the given point "p". This rectangle must be non-empty.
      • expanded

        @CheckReturnValue
        public R2Rect expanded​(R2Vector margin)
        Return a rectangle that has been expanded on each side in the x-direction by margin.x(), and on each side in the y-direction by margin.y(). If either margin is empty, then shrink the interval on the corresponding sides instead. The resulting rectangle may be empty. Any expansion of an empty rectangle remains empty.
      • expanded

        @CheckReturnValue
        public R2Rect expanded​(double margin)
        Returns a rectangle that has been expanded on both sides by the given margin. Any expansion of an empty rectangle remains empty.
      • expand

        void expand​(double margin)
        Expands this rectangle on both axes by the given margin.

        Package private since only S2 classes are intended to mutate R2Rects for now.

      • union

        @CheckReturnValue
        public R2Rect union​(R2Rect other)
        Returns the smallest rectangle containing the union of this rectangle and the given rectangle.
      • intersection

        @CheckReturnValue
        public R2Rect intersection​(R2Rect other)
        Returns the smallest rectangle containing the intersection of this rectangle and the given rectangle.
      • hashCode

        public int hashCode()
        Returns a simple convolution hashcodes from the x and y internals.
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object other)
        Returns true if two rectangles contains the same set of points.
        Overrides:
        equals in class java.lang.Object
      • approxEquals

        public boolean approxEquals​(R2Rect other)
        Returns true if the x- and y-intervals of the two rectangles are the same up to the given tolerance. See R1Interval for details on approximate interval equality.
      • approxEquals

        public boolean approxEquals​(R2Rect other,
                                    double maxError)
        Returns true if the given rectangles are equal to within maxError. See R1Interval for details on approximate interval equality.
      • toString

        public java.lang.String toString()
        Returns a simple string representation of this rectangle's lower and upper corners.
        Overrides:
        toString in class java.lang.Object