Package org.evoludo.geom
Class Rectangle2D
Object
Rectangle2D
Utility class for dealing with rectangles in 2D.
Note: this implementation is intended as a drop-in replacement for
java.awt.geom.Rectangle2D.Double. Methods are implemented only on an
as-needed basis.
- Author:
- Christoph Hauert
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe bottom segment of the rectangle.protected double
The height of the rectangle.The left segment of the rectangle.protected Point2D
The origin of the rectangle (lower left corner).The right segment of the rectangle.The top segment of the rectangle.protected double
The width of the rectangle. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new empty 2D rectangle with lower left corner at(0,0)
and zero width and zero height.Rectangle2D
(double x, double y, double width, double height) Create a new 2D rectangle with lower left corner at(x,y)
, widthwidth
and heightheight
.Create a copy of 2D the rectangler
. -
Method Summary
Modifier and TypeMethodDescriptionadjust
(double dx, double dy, double dw, double dh) Adjust the position of the origin, the lower left corner(x,y)
, as well as the dimensions, i.e.boolean
contains
(double px, double py) Check if the rectangle contains the point(px, py)
.boolean
Check if the rectangle contains the pointp
.double
Get theheight
of the rectangle.double
getWidth()
Get thewidth
of the rectangle.double
getX()
Get thex
-coordinate of the lower left corner of the rectangle.double
getY()
Get they
-coordinate of the lower left corner of the rectangle.boolean
intersects
(Line2D l) Check if the line (or line segment)l
intersects the rectangle.set
(double x, double y, double width, double height) Set the coordinates of the lower left corner(x,y)
and the dimensions of the rectangle.setOrigin
(double x, double y) Set the coordinates of the origin, lower left corner(x,y)
, of the rectangle.setSize
(double width, double height) Set the dimensions, the width and height, of the rectangle.shift
(double dx, double dy) Shift/translate the rectangle.toString()
-
Field Details
-
right
The right segment of the rectangle. -
top
The top segment of the rectangle. -
left
The left segment of the rectangle. -
bottom
The bottom segment of the rectangle. -
origin
The origin of the rectangle (lower left corner). -
width
protected double widthThe width of the rectangle. -
height
protected double heightThe height of the rectangle.
-
-
Constructor Details
-
Rectangle2D
public Rectangle2D()Create a new empty 2D rectangle with lower left corner at(0,0)
and zero width and zero height. -
Rectangle2D
Create a copy of 2D the rectangler
.- Parameters:
r
- the 2D rectangle to copy
-
Rectangle2D
public Rectangle2D(double x, double y, double width, double height) Create a new 2D rectangle with lower left corner at(x,y)
, widthwidth
and heightheight
.- Parameters:
x
- the first coordinate of rectangle (lower left corner)y
- the second coordinate of rectangle (lower left corner)width
- the width of rectangleheight
- the height of rectangle
-
-
Method Details
-
set
Set the coordinates of the lower left corner(x,y)
and the dimensions of the rectangle.- Parameters:
x
- thex
-coordinatey
- they
-coordinatewidth
- the widthheight
- the height- Returns:
- the new rectangle
-
setOrigin
Set the coordinates of the origin, lower left corner(x,y)
, of the rectangle.- Parameters:
x
- thex
-coordinatey
- they
-coordinate- Returns:
- the transformed rectangle
-
setSize
Set the dimensions, the width and height, of the rectangle.- Parameters:
width
- the widthheight
- the height- Returns:
- the transformed rectangle
-
adjust
Adjust the position of the origin, the lower left corner(x,y)
, as well as the dimensions, i.e. the width and height, of the rectangle.- Parameters:
dx
- the horizontal shift of the origindy
- the vertical shift of the origindw
- the increase in widthdh
- the increase in height- Returns:
- the transformed rectangle
-
shift
Shift/translate the rectangle.- Parameters:
dx
- the horizontal shift of the origindy
- the vertical shift of the origin- Returns:
- the translated rectangle
-
contains
Check if the rectangle contains the pointp
.- Parameters:
p
- the point to check- Returns:
true
if the point lies inside
-
contains
public boolean contains(double px, double py) Check if the rectangle contains the point(px, py)
.- Parameters:
px
- thex
-coordinatepy
- they
-coordinate- Returns:
true
if the point lies inside
-
intersects
Check if the line (or line segment)l
intersects the rectangle.- Parameters:
l
- the line to check- Returns:
true
if the line intersects
-
getX
public double getX()Get thex
-coordinate of the lower left corner of the rectangle.Method to ensure compatibility with
java.awt.geom.Rectangle2D.Double
.- Returns:
- the
x
-coordinate of lower left corner
-
getY
public double getY()Get they
-coordinate of the lower left corner of the rectangle.Method to ensure compatibility with
java.awt.geom.Rectangle2D.Double
.- Returns:
- the
y
-coordinate of lower left corner
-
getWidth
public double getWidth()Get thewidth
of the rectangle.Method to ensure compatibility with
java.awt.geom.Rectangle2D.Double
.- Returns:
- the width of rectangle
-
getHeight
public double getHeight()Get theheight
of the rectangle.Method to ensure compatibility with
java.awt.geom.Rectangle2D.Double
.- Returns:
- the height of rectangle
-
toString
-