- java.lang.Object
-
- impl.org.controlsfx.tools.rectangle.Rectangles2D
-
public class Rectangles2D extends Object
Usability methods for rectangles.
-
-
Constructor Summary
Constructors Constructor Description Rectangles2D()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
contains(Rectangle2D rectangle, Edge2D edge)
Indicates whether the specified rectangle contains the specified edge.static Rectangle2D
fixRatio(Rectangle2D original, double ratio)
Creates a new rectangle with the same center point and area as the specifiedoriginal
rectangle with the specifiedratio
.static Rectangle2D
fixRatioWithinBounds(Rectangle2D original, double ratio, Rectangle2D bounds)
Creates a new rectangle with the same center point and area (if possible) as the specifiedoriginal
rectangle with the specifiedratio
and respecting the specifiedbounds
.static Rectangle2D
forCenterAndAreaAndRatio(Point2D centerPoint, double area, double ratio)
Creates a new rectangle with the specifiedcenterPoint
,area
andratio
.static Rectangle2D
forCenterAndAreaAndRatioWithinBounds(Point2D centerPoint, double area, double ratio, Rectangle2D bounds)
Creates a new rectangle with the specifiedcenterPoint
,area
(if possible) andratio
, respecting the specifiedbounds
.static Rectangle2D
forCenterAndSize(Point2D centerPoint, double width, double height)
Creates a new rectangle with the specified center and the specified width and height.static Rectangle2D
forDiagonalCorners(Point2D oneCorner, Point2D diagonalCorner)
Creates a new rectangle with the two specified corners.static Rectangle2D
forDiagonalCornersAndRatio(Point2D fixedCorner, Point2D diagonalCorner, double ratio)
Creates a new rectangle with the two specified corners.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).static Rectangle2D
forEdgeAndOpposingPointAndRatioWithinBounds(Edge2D edge, Point2D point, double ratio, Rectangle2D bounds)
Returns a rectangle that is principally defined by the specified edge and point.static Rectangle2D
forUpperLeftCornerAndSize(Point2D upperLeft, double width, double height)
Creates a new rectangle with the specifiedupperLeft
corner and the specifiedwidth
andheight
.static Rectangle2D
fromBounds(Bounds bounds)
Returns a rectangle with the same coordinates as the specified bounds.static Point2D
getCenterPoint(Rectangle2D rectangle)
Returns the center of the specified rectangle as a point.static Point2D
inRectangle(Rectangle2D rectangle, Point2D point)
Moves the specified point into the specified rectangle.static Rectangle2D
intersection(Rectangle2D a, Rectangle2D b)
Returns the rectangle which represents the intersection of the two specified rectangles.
-
-
-
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 checkedge
- the edge to check- Returns:
true
if both end points of the edge arecontained
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
- theRectangle2D
into which the point should be movedpoint
- thePoint2D
which is checked- Returns:
- either the specified
point
or thePoint2D
which is closest to it while still being contained on therectangle
-
getCenterPoint
public static Point2D getCenterPoint(Rectangle2D rectangle)
Returns the center of the specified rectangle as a point.- Parameters:
rectangle
- theRectangle2D
whose center point will be returned- Returns:
- the
Point2D
whose x/y coordinates lie at(min + max) / 2
.
-
intersection
public static Rectangle2D intersection(Rectangle2D a, Rectangle2D b)
Returns the rectangle which represents the intersection of the two specified rectangles.- Parameters:
a
- aRectangle2D
b
- anotherRectangle2D
- Returns:
- a
Rectangle2D
which is the intersection ofa
andb
; possibleRectangle2D.EMPTY
.
-
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 cornerdiagonalCorner
- 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 specifiedupperLeft
corner and the specifiedwidth
andheight
.- Parameters:
upperLeft
- one cornerwidth
- the new rectangle's widthheight
- 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 specifiedfixedCorner
as its corner. The other one will either be on the same x- or y-parallel as thediagonalCorner
but will be such that the rectangle has the specifiedratio
.- Parameters:
fixedCorner
- one cornerdiagonalCorner
- another corner, diagonal from the firstratio
- the ratio the returned rectangle must have; must be non-negative- Returns:
- the
Rectangle2D
which is defined by thefixedCorner
, the x- or y-parallel of thediagonalCorner
and theratio
-
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 rectanglewidth
- the width of the new rectangleheight
- 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 specifiedoriginal
rectangle with the specifiedratio
.- Parameters:
original
- the original rectangleratio
- the new ratio- Returns:
- a new
Rectangle2D
with the same center point as theoriginal
and the specifiedratio
; it has the same area as theoriginal
- Throws:
NullPointerException
- if theoriginal
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 specifiedoriginal
rectangle with the specifiedratio
and respecting the specifiedbounds
.- Parameters:
original
- the original rectangleratio
- the new ratiobounds
- the bounds within which the new rectangle will be located- Returns:
- a new
Rectangle2D
with the same center point as theoriginal
and the specifiedratio
; it has the same area as theoriginal
unless this would violate the bounds; in this case it is as large as possible while still staying within the bounds - Throws:
NullPointerException
- if theoriginal
orbounds
rectangle is nullIllegalArgumentException
- if theoriginal
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 specifiedcenterPoint
,area
andratio
.- Parameters:
centerPoint
- the new rectangle's center pointarea
- the new rectangle's arearatio
- the new ratio- Returns:
- a new
Rectangle2D
with the specifiedcenterPoint
,area
andratio
- Throws:
IllegalArgumentException
- if thecenterPoint
is out of thebounds
-
forCenterAndAreaAndRatioWithinBounds
public static Rectangle2D forCenterAndAreaAndRatioWithinBounds(Point2D centerPoint, double area, double ratio, Rectangle2D bounds)
Creates a new rectangle with the specifiedcenterPoint
,area
(if possible) andratio
, respecting the specifiedbounds
.- Parameters:
centerPoint
- the new rectangle's center pointarea
- the new rectangle's area (if possible without violating the bounds)ratio
- the new ratiobounds
- the bounds within which the new rectangle will be located- Returns:
- a new
Rectangle2D
with the specifiedcenterPoint
andratio
; it has the specifiedarea
unless this would violate thebounds
; in this case it is as large as possible while still staying within the bounds - Throws:
IllegalArgumentException
- if thecenterPoint
is out of thebounds
-
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 rectanglepoint
- 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 (compareforEdgeAndOpposingPoint
) 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 specifiedbounds
point
- the point to which the rectangle spans if ratio and bounds allow itratio
- the ratio the new rectangle must havebounds
- 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
- theBounds
for which the rectangle will be created- Returns:
- a
Rectangle2D
with the same minX-, minY-, maxX- and maxY-coordiantes as the specified bounds
-
-