Class Rectangles2D


  • public class Rectangles2D
    extends Object
    Usability methods for rectangles.
    • Constructor Detail

      • Rectangles2D

        public Rectangles2D()
    • Method Detail

      • contains

        public static boolean contains​(Rectangle2D rectangle,
                                       Edge2D edge)
        Indicates whether the specified rectangle contains the specified edge.
        Parameters:
        rectangle - the rectangle to check
        edge - the edge to check
        Returns:
        true if both end points of the edge are contained in the rectangle
      • inRectangle

        public static Point2D inRectangle​(Rectangle2D rectangle,
                                          Point2D point)
        Moves the specified point into the specified rectangle. If the point is already with the rectangle, it is returned. Otherwise the point in the rectangle which is closest to the specified one is returned.
        Parameters:
        rectangle - the Rectangle2D into which the point should be moved
        point - the Point2D which is checked
        Returns:
        either the specified point or the Point2D which is closest to it while still being contained on the rectangle
      • getCenterPoint

        public static Point2D getCenterPoint​(Rectangle2D rectangle)
        Returns the center of the specified rectangle as a point.
        Parameters:
        rectangle - the Rectangle2D whose center point will be returned
        Returns:
        the Point2D whose x/y coordinates lie at (min + max) / 2.
      • forDiagonalCorners

        public static Rectangle2D forDiagonalCorners​(Point2D oneCorner,
                                                     Point2D diagonalCorner)
        Creates a new rectangle with the two specified corners. The two corners will be interpreted as being diagonal of each other.
        Parameters:
        oneCorner - one corner
        diagonalCorner - another corner, diagonal from the first
        Returns:
        the Rectangle2D which is defined by the two corners
      • forUpperLeftCornerAndSize

        public static Rectangle2D forUpperLeftCornerAndSize​(Point2D upperLeft,
                                                            double width,
                                                            double height)
        Creates a new rectangle with the specified upperLeft corner and the specified width and height.
        Parameters:
        upperLeft - one corner
        width - the new rectangle's width
        height - the new rectangle's height
        Returns:
        the Rectangle2D which is defined by the specified upper left corner and width and height
      • forDiagonalCornersAndRatio

        public static Rectangle2D forDiagonalCornersAndRatio​(Point2D fixedCorner,
                                                             Point2D diagonalCorner,
                                                             double ratio)
        Creates a new rectangle with the two specified corners. The two corners will be interpreted as being diagonal of each other. The returned rectangle will have the specified fixedCorner as its corner. The other one will either be on the same x- or y-parallel as the diagonalCorner but will be such that the rectangle has the specified ratio.
        Parameters:
        fixedCorner - one corner
        diagonalCorner - another corner, diagonal from the first
        ratio - the ratio the returned rectangle must have; must be non-negative
        Returns:
        the Rectangle2D which is defined by the fixedCorner, the x- or y-parallel of the diagonalCorner and the ratio
      • forCenterAndSize

        public static Rectangle2D forCenterAndSize​(Point2D centerPoint,
                                                   double width,
                                                   double height)
        Creates a new rectangle with the specified center and the specified width and height.
        Parameters:
        centerPoint - the center point o the new rectangle
        width - the width of the new rectangle
        height - the height of the new rectangle
        Returns:
        a rectangle with the specified center and size
      • fixRatio

        public static Rectangle2D fixRatio​(Rectangle2D original,
                                           double ratio)
        Creates a new rectangle with the same center point and area as the specified original rectangle with the specified ratio.
        Parameters:
        original - the original rectangle
        ratio - the new ratio
        Returns:
        a new Rectangle2D with the same center point as the original and the specified ratio; it has the same area as the original
        Throws:
        NullPointerException - if the original rectangle is null
      • fixRatioWithinBounds

        public static Rectangle2D fixRatioWithinBounds​(Rectangle2D original,
                                                       double ratio,
                                                       Rectangle2D bounds)
        Creates a new rectangle with the same center point and area (if possible) as the specified original rectangle with the specified ratio and respecting the specified bounds.
        Parameters:
        original - the original rectangle
        ratio - the new ratio
        bounds - the bounds within which the new rectangle will be located
        Returns:
        a new Rectangle2D with the same center point as the original and the specified ratio; it has the same area as the original unless this would violate the bounds; in this case it is as large as possible while still staying within the bounds
        Throws:
        NullPointerException - if the original or bounds rectangle is null
        IllegalArgumentException - if the original rectangle's center point is out of the bounds
      • forCenterAndAreaAndRatio

        public static Rectangle2D forCenterAndAreaAndRatio​(Point2D centerPoint,
                                                           double area,
                                                           double ratio)
        Creates a new rectangle with the specified centerPoint, area and ratio.
        Parameters:
        centerPoint - the new rectangle's center point
        area - the new rectangle's area
        ratio - the new ratio
        Returns:
        a new Rectangle2D with the specified centerPoint, area and ratio
        Throws:
        IllegalArgumentException - if the centerPoint is out of the bounds
      • forCenterAndAreaAndRatioWithinBounds

        public static Rectangle2D forCenterAndAreaAndRatioWithinBounds​(Point2D centerPoint,
                                                                       double area,
                                                                       double ratio,
                                                                       Rectangle2D bounds)
        Creates a new rectangle with the specified centerPoint, area (if possible) and ratio, respecting the specified bounds.
        Parameters:
        centerPoint - the new rectangle's center point
        area - the new rectangle's area (if possible without violating the bounds)
        ratio - the new ratio
        bounds - the bounds within which the new rectangle will be located
        Returns:
        a new Rectangle2D with the specified centerPoint and ratio; it has the specified area unless this would violate the bounds; in this case it is as large as possible while still staying within the bounds
        Throws:
        IllegalArgumentException - if the centerPoint is out of the bounds
      • forEdgeAndOpposingPoint

        public static Rectangle2D forEdgeAndOpposingPoint​(Edge2D edge,
                                                          Point2D point)
        Returns a rectangle that has the specified edge and has its opposing edge on the parallel axis defined by the specified point's X or Y coordinate (depending on the edge's orientation).
        Parameters:
        edge - the edge which will be contained in the returned rectangle
        point - the point whose X or Y coordinate defines the other edge
        Returns:
        a rectangle
      • forEdgeAndOpposingPointAndRatioWithinBounds

        public static Rectangle2D forEdgeAndOpposingPointAndRatioWithinBounds​(Edge2D edge,
                                                                              Point2D point,
                                                                              double ratio,
                                                                              Rectangle2D bounds)
        Returns a rectangle that is principally defined by the specified edge and point. It should have the specified edge as one of its own and its parallel edge should contain the point. While this would already well-define the rectangle (compare forEdgeAndOpposingPoint) the additionally specified ratio and bounds have precedence over these arguments:
        The returned rectangle will have the ratio and will be within the bounds. If the bounds make it possible, the specified point will lie on the edge parallel to the specified one. In order to maintain the ratio, this will make it necessary to not use the specified edge but instead one with a different length. The new edge will have the same center point as the specified one.
        This results on the following behavior: As the point is moved closer to or further away from the edge, the resulting rectangle shrinks and grows while being anchored to the specified edge's center point and keeping the ratio. This is limited by the bounds.
        Parameters:
        edge - the edge which defines the center point and orientation of one of the rectangle's edges; must be within the specified bounds
        point - the point to which the rectangle spans if ratio and bounds allow it
        ratio - the ratio the new rectangle must have
        bounds - the bounds within which the new rectangle must lie
        Returns:
        a rectangle
      • fromBounds

        public static Rectangle2D fromBounds​(Bounds bounds)
        Returns a rectangle with the same coordinates as the specified bounds.
        Parameters:
        bounds - the Bounds for which the rectangle will be created
        Returns:
        a Rectangle2D with the same minX-, minY-, maxX- and maxY-coordiantes as the specified bounds