Class Rect


  • public final class Rect
    extends java.lang.Object
    A class to describe a rectangle. It consists of left, top, right, and, bottom extents.

    Instances of this class are immutable. All change operations return a new instance of this Rect class.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Rect ZERO  
    • Constructor Summary

      Constructors 
      Constructor Description
      Rect()
      Instantiates a Rect of zero extents.
      Rect​(CoordPair topLeft, CoordPair bottomRight)
      Instantiates a Rect specified by the given CoordPair.
      Rect​(Rect source)
      Deprecated.
      Rect is immutable, so there is no need to copy an instance.
      Rect​(UnitSpan left, UnitSpan top, UnitSpan right, UnitSpan bottom)
      Instantiates a Rect specified by the given UnitSpan extents.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Rect add​(CoordPair add)
      Returns a Rect representing the shift of this object by the given CoordPair.
      UnitSpan bottom()
      Gets this object's bottommost extent.
      Rect bottomHeight​(UnitSpan newBottom, UnitSpan newHeight)
      Returns a Rect representing the change in height of this object by the given UnitSpan extents.
      CoordPair bottomLeft()
      Gets this object's bottom left coordinate.
      Rect bottomLeft​(CoordPair newBottomLeft)
      Returns a Rect representing the extention of this object using the given bottom left CoordPair.
      CoordPair bottomRight()
      Gets this object's bottom right coordinate.
      Rect bottomRight​(CoordPair newBottomRight)
      Returns a Rect representing the extention of this object using the given bottom right CoordPair.
      CoordPair centre()
      Gets this object's centre coordinate.
      Rect changeUnits​(int eNewUnits)
      Returns a Rect representing the change of this object's unit span units to the given unit code.
      boolean contains​(CoordPair point)
      Determines if this object contains the given CoordPair.
      boolean contains​(Rect rect)
      Determines if this object contains the given Rect.
      boolean disjoint​(Rect rect)
      Determines if this object is disjoint from the given Rect.
      boolean equals​(java.lang.Object object)
      Determines if this object is equal to the given Object.
      int hashCode()  
      UnitSpan height()
      Gets this object's height.
      Rect height​(UnitSpan newHeight, boolean bStretchTop)
      Returns a Rect representing the stretch in height of this object by the given UnitSpan.
      Rect intersection​(Rect intersect)
      Returns a Rect representing the intersection of this object with the given Rect.
      boolean isDegenerate()
      Determines if this object is degenerate.
      UnitSpan left()
      Gets this object's leftmost extent.
      Rect leftRight​(UnitSpan newLeft, UnitSpan newRight)
      Returns a Rect representing the change in width of this object by the given UnitSpan extents.
      Rect leftWidth​(UnitSpan newLeft, UnitSpan newWidth)
      Returns a Rect representing the change in width of this object by the given UnitSpan extents.
      boolean notEquals​(java.lang.Object compare)
      Determines if this object is not equal to the given Object.
      boolean overlaps​(Rect rect)
      Determines if this object overlaps the given Rect.
      UnitSpan right()
      Gets this object's rightmost extent.
      Rect rightWidth​(UnitSpan newRight, UnitSpan newWidth)
      Returns a Rect representing the change in width of this object by the given UnitSpan extents.
      Rect rotate​(CoordPair point, Angle angle)
      Returns a Rect representing the rotation of this object about the given CoordPair and Angle of rotation.
      Rect subtract​(CoordPair subtract)
      Returns a Rect representing the shift of this object and the given CoordPair.
      UnitSpan top()
      Gets this object's topmost extent.
      Rect topBottom​(UnitSpan newTop, UnitSpan newBottom)
      Returns a Rect representing the change in height of this object by the given UnitSpan extents.
      Rect topHeight​(UnitSpan newTop, UnitSpan newHeight)
      Returns a Rect representing the change in height of this object by the given UnitSpan extents.
      CoordPair topLeft()
      Gets this object's top left coordinate.
      Rect topLeft​(CoordPair newTopLeft)
      Returns a Rect representing the extention of this object using the given top left CoordPair.
      CoordPair topRight()
      Gets this object's top right coordinate.
      Rect topRight​(CoordPair newTopRight)
      Returns a Rect representing the extention of this object using the given top right CoordPair.
      Rect union​(Rect union)
      Returns a Rect representing the union of this object with the given Rect.
      UnitSpan width()
      Gets this object's width.
      Rect width​(UnitSpan newWidth, boolean bStretchLeft)
      Returns a Rect representing the stretch in width of this object by the given UnitSpan.
      static Rect zero()
      The zero rectangle.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ZERO

        public static final Rect ZERO
    • Constructor Detail

      • Rect

        public Rect()
        Instantiates a Rect of zero extents.
      • Rect

        public Rect​(Rect source)
        Deprecated.
        Rect is immutable, so there is no need to copy an instance.
        Instantiates a Rect from the given Rect.
        Parameters:
        source - the Rect to copy to this object.
      • Rect

        public Rect​(CoordPair topLeft,
                    CoordPair bottomRight)
        Instantiates a Rect specified by the given CoordPair.
        Parameters:
        topLeft - the top left coordinate of the rectangle.
        bottomRight - the bottom right coordinate of the rectangle.
      • Rect

        public Rect​(UnitSpan left,
                    UnitSpan top,
                    UnitSpan right,
                    UnitSpan bottom)
        Instantiates a Rect specified by the given UnitSpan extents. The rectangle's extents are normalized such that:
        • the leftmost extent is set to min(oLeft, oRight),
        • the rightmost extent is set to max(oLeft, oRight),
        • the topmost extent is set to min(oTop, oBottom),
        • the bottommost extent is set to max(oTop, oBottom),
        • the width is set to |oLeft - oRight|, and,
        • the height is set to |oTop - oBottom|.
        Parameters:
        left - the left extent of the rectangle.
        top - the top extent of the rectangle.
        right - the right extent of the rectangle.
        bottom - the bottom extent of the rectangle.
    • Method Detail

      • left

        public UnitSpan left()
        Gets this object's leftmost extent.
        Returns:
        the leftmost extent.
      • top

        public UnitSpan top()
        Gets this object's topmost extent.
        Returns:
        the topmost extent.
      • right

        public UnitSpan right()
        Gets this object's rightmost extent.
        Returns:
        the rightmost extent.
      • bottom

        public UnitSpan bottom()
        Gets this object's bottommost extent.
        Returns:
        the bottommost extent.
      • leftRight

        public Rect leftRight​(UnitSpan newLeft,
                              UnitSpan newRight)
        Returns a Rect representing the change in width of this object by the given UnitSpan extents. The left/right extends are normalized.
        Parameters:
        newLeft - the new left extent.
        newRight - the new right extent.
        Returns:
        a rectangle of the changed width.
      • topBottom

        public Rect topBottom​(UnitSpan newTop,
                              UnitSpan newBottom)
        Returns a Rect representing the change in height of this object by the given UnitSpan extents.
        Parameters:
        newTop - the new left extent.
        newBottom - the new bottom extent.
        Returns:
        a rectangle of the changed height.
      • leftWidth

        public Rect leftWidth​(UnitSpan newLeft,
                              UnitSpan newWidth)
        Returns a Rect representing the change in width of this object by the given UnitSpan extents.
        Parameters:
        newLeft - the new left extent.
        newWidth - the new width.
        Returns:
        a rectangle of the changed width.
      • rightWidth

        public Rect rightWidth​(UnitSpan newRight,
                               UnitSpan newWidth)
        Returns a Rect representing the change in width of this object by the given UnitSpan extents.
        Parameters:
        newRight - the new right extent.
        newWidth - the new width.
        Returns:
        a rectangle of the changed width.
      • topHeight

        public Rect topHeight​(UnitSpan newTop,
                              UnitSpan newHeight)
        Returns a Rect representing the change in height of this object by the given UnitSpan extents.
        Parameters:
        newTop - the new top extent.
        newHeight - the new height.
        Returns:
        a rectangle of the changed height.
      • bottomHeight

        public Rect bottomHeight​(UnitSpan newBottom,
                                 UnitSpan newHeight)
        Returns a Rect representing the change in height of this object by the given UnitSpan extents.
        Parameters:
        newBottom - the new bottom extent.
        newHeight - the new height.
        Returns:
        a rectangle of the changed height.
      • topLeft

        public CoordPair topLeft()
        Gets this object's top left coordinate.
        Returns:
        the top left coordinate.
      • topRight

        public CoordPair topRight()
        Gets this object's top right coordinate.
        Returns:
        the top right coordinate.
      • bottomLeft

        public CoordPair bottomLeft()
        Gets this object's bottom left coordinate.
        Returns:
        the bottom left coordinate.
      • bottomRight

        public CoordPair bottomRight()
        Gets this object's bottom right coordinate.
        Returns:
        the bottom right coordinate.
      • changeUnits

        public Rect changeUnits​(int eNewUnits)
        Returns a Rect representing the change of this object's unit span units to the given unit code.
        Parameters:
        eNewUnits - the new unit code.
        Returns:
        a rectangle of the change.
      • topLeft

        public Rect topLeft​(CoordPair newTopLeft)
        Returns a Rect representing the extention of this object using the given top left CoordPair.
        Parameters:
        newTopLeft - the new top left coordinate.
        Returns:
        a rectangle of the extention.
      • topRight

        public Rect topRight​(CoordPair newTopRight)
        Returns a Rect representing the extention of this object using the given top right CoordPair.
        Parameters:
        newTopRight - the new top right coordinate.
        Returns:
        a rectangle of the extention.
      • bottomLeft

        public Rect bottomLeft​(CoordPair newBottomLeft)
        Returns a Rect representing the extention of this object using the given bottom left CoordPair.
        Parameters:
        newBottomLeft - the new bottom left coordinate.
        Returns:
        a rectangle of the extention.
      • bottomRight

        public Rect bottomRight​(CoordPair newBottomRight)
        Returns a Rect representing the extention of this object using the given bottom right CoordPair.
        Parameters:
        newBottomRight - the new bottom right coordinate.
        Returns:
        a rectangle of the extention.
      • height

        public UnitSpan height()
        Gets this object's height.
        Returns:
        the height.
      • width

        public UnitSpan width()
        Gets this object's width.
        Returns:
        the width.
      • height

        public Rect height​(UnitSpan newHeight,
                           boolean bStretchTop)
        Returns a Rect representing the stretch in height of this object by the given UnitSpan.
        Parameters:
        newHeight - the new height.
        bStretchTop - stretch from the top when set; stretch from the bottom when not set.
        Returns:
        a rectangle of the stretched height.
      • width

        public Rect width​(UnitSpan newWidth,
                          boolean bStretchLeft)
        Returns a Rect representing the stretch in width of this object by the given UnitSpan.
        Parameters:
        newWidth - the new width.
        bStretchLeft - stretch from the left when set; stretch from the right when not set.
        Returns:
        a rectangle of the stretched width.
      • centre

        public CoordPair centre()
        Gets this object's centre coordinate.
        Returns:
        the center of the rectangle.
      • contains

        public boolean contains​(CoordPair point)
        Determines if this object contains the given CoordPair.
        Parameters:
        point - a coordinate.
        Returns:
        true if the coordinate is contained, false otherwise
      • contains

        public boolean contains​(Rect rect)
        Determines if this object contains the given Rect.
        Parameters:
        rect - a rectangle.
        Returns:
        true if given the rectangle is contained, false otherwise
      • overlaps

        public boolean overlaps​(Rect rect)
        Determines if this object overlaps the given Rect.
        Parameters:
        rect - a rectangle.
        Returns:
        true if the rectangle overlaps, false otherwise
      • disjoint

        public boolean disjoint​(Rect rect)
        Determines if this object is disjoint from the given Rect. This is slightly different than the result of ! overlaps() in that, if the rectangles share an edge, this will return true.
        Parameters:
        rect - a rectangle.
        Returns:
        true if the rectangle is disjoint, false otherwise
        See Also:
        overlaps(Rect)
      • isDegenerate

        public boolean isDegenerate()
        Determines if this object is degenerate. Only the zero rectangle is degenerate.
        Returns:
        true if this rectangle is degenerate, false otherwise
        See Also:
        zero()
      • rotate

        public Rect rotate​(CoordPair point,
                           Angle angle)
        Returns a Rect representing the rotation of this object about the given CoordPair and Angle of rotation.
        Parameters:
        point - the point of rotation.
        angle - the angle of rotation.
        Returns:
        a rectangle of the rotation.
      • equals

        public boolean equals​(java.lang.Object object)
        Determines if this object is equal to the given Object. Comparisons with instances of non-Rect objects are never equal.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        object - the Object to compare.
        Returns:
        true if equal, false otherwise.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • notEquals

        public boolean notEquals​(java.lang.Object compare)
        Determines if this object is not equal to the given Object. Comparisons with instances of non-Rect objects are always not equal.
        Parameters:
        compare - the Object to compare.
        Returns:
        true if not equal, false otherwise
      • add

        public Rect add​(CoordPair add)
        Returns a Rect representing the shift of this object by the given CoordPair.
        Parameters:
        add - the CoordPair to add.
        Returns:
        a rectangle of the shift.
      • subtract

        public Rect subtract​(CoordPair subtract)
        Returns a Rect representing the shift of this object and the given CoordPair.
        Parameters:
        subtract - the CoordPair to subtract.
        Returns:
        a rectangle of the shift.
      • union

        public Rect union​(Rect union)
        Returns a Rect representing the union of this object with the given Rect.
        Parameters:
        union - the Rect to unite with.
        Returns:
        a rectangle of the union.
      • intersection

        public Rect intersection​(Rect intersect)
        Returns a Rect representing the intersection of this object with the given Rect.
        Parameters:
        intersect - the Rect to intersect with.
        Returns:
        a rectangle of the intersection.
      • zero

        public static Rect zero()
        The zero rectangle.
        Returns:
        the rectangle equal to zero.