Package org.djutils.draw.bounds
Class Bounds2d
- java.lang.Object
-
- org.djutils.draw.bounds.Bounds2d
-
- All Implemented Interfaces:
Serializable
,Bounds<Bounds2d,Space2d>
,Drawable<Point2d,Space2d>
,Drawable2d
public class Bounds2d extends Object implements Drawable2d, Bounds<Bounds2d,Space2d>
A Bounds2d stores the rectangular 2D bounds of a 2d object, or a collection of 2d objects. The Bounds2d is an immutable object.Copyright (c) 2020-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.- Author:
- Alexander Verbraeck, Peter Knoppers
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Bounds2d(double deltaX, double deltaY)
Constructs a new Bounds2d around the origin (0, 0).Bounds2d(double minX, double maxX, double minY, double maxY)
Construct a Bounds2d by providing its lower and upper bounds in both dimensions.Bounds2d(Collection<Point2d> points)
Construct a Bounds2d from a collection of Point2d, finding the lowest and highest x and y coordinates.Bounds2d(Iterator<? extends Point2d> points)
Construct a Bounds2d from some collection of points, finding the lowest and highest x and y coordinates.Bounds2d(Drawable2d drawable2d)
Construct a Bounds2d for a Drawable2d.Bounds2d(Point2d[] points)
Construct a Bounds2d from an array of Point2d, finding the lowest and highest x and y coordinates.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(double x, double y)
Check if this Bounds2d contains a point.boolean
contains(Drawable2d drawable)
Check if this Bounds2d completely contains a Drawable2d.boolean
contains(Point2d point)
Check if this Bounds2d contains a given point.boolean
covers(double x, double y)
Check if this Bounds2d contains a point.boolean
covers(Bounds2d otherBounds2d)
Check if this Bounds contains another Bounds.boolean
covers(Point2d point)
Check if this Bounds2d contains a point.boolean
disjoint(Bounds2d otherBounds2d)
Return whether this Bounds is disjoint from another Bounds.boolean
equals(Object obj)
double
getArea()
Return the area of this Bounds2d.Bounds2d
getBounds()
Retrieve the bounding rectangle of the object.double
getMaxX()
Return the upper bound for x.double
getMaxY()
Return the upper bound for y.double
getMinX()
Return the lower bound for x.double
getMinY()
Return the lower bound for y.Iterator<Point2d>
getPoints()
Retrieve, or generate all points that make up the object.int
hashCode()
Bounds2d
intersection(Bounds2d otherBounds2d)
Return the intersecting Bounds of this Bounds and another Bounds.boolean
intersects(Bounds2d otherBounds2d)
Return whether this Bounds intersects another Bounds.Point2d
midPoint()
Return the mid point of this Bounds2d.int
size()
Retrieve the number of points that make up the object.Rectangle2D
toRectangle2D()
Return an AWT Rectangle2D that covers the same area as this Bounds2d.String
toString()
-
-
-
Constructor Detail
-
Bounds2d
public Bounds2d(double minX, double maxX, double minY, double maxY) throws IllegalArgumentException
Construct a Bounds2d by providing its lower and upper bounds in both dimensions.- Parameters:
minX
- double; the lower bound for xmaxX
- double; the upper bound for xminY
- double; the lower bound for ymaxY
- double; the upper bound for y- Throws:
IllegalArgumentException
- when a lower bound is larger than the corresponding upper bound, or any of the bounds is NaN
-
Bounds2d
public Bounds2d(double deltaX, double deltaY)
Constructs a new Bounds2d around the origin (0, 0).- Parameters:
deltaX
- double; the deltaX value around the origindeltaY
- double; the deltaY value around the origin- Throws:
IllegalArgumentException
- when one of the delta values is less than zero
-
Bounds2d
public Bounds2d(Iterator<? extends Point2d> points)
Construct a Bounds2d from some collection of points, finding the lowest and highest x and y coordinates.- Parameters:
points
- Iterator<? extends Point2d>; Iterator that will generate all the points for which to construct a Bounds2d- Throws:
NullPointerException
- when points is nullIllegalArgumentException
- when the iterator provides zero points
-
Bounds2d
public Bounds2d(Point2d[] points) throws NullPointerException, IllegalArgumentException
Construct a Bounds2d from an array of Point2d, finding the lowest and highest x and y coordinates.- Parameters:
points
- Point2d[]; the points to construct a Bounds2d from- Throws:
NullPointerException
- when points is nullIllegalArgumentException
- when zero points are provided
-
Bounds2d
public Bounds2d(Drawable2d drawable2d) throws NullPointerException
Construct a Bounds2d for a Drawable2d.- Parameters:
drawable2d
- Drawable2d; any object that implements the Drawable2d interface- Throws:
NullPointerException
- when drawable2d is null
-
Bounds2d
public Bounds2d(Collection<Point2d> points)
Construct a Bounds2d from a collection of Point2d, finding the lowest and highest x and y coordinates.- Parameters:
points
- Collection<Point2d>; the collection of points to construct a Bounds2d from- Throws:
NullPointerException
- when points is nullIllegalArgumentException
- when the collection is empty
-
-
Method Detail
-
getPoints
public Iterator<Point2d> getPoints()
Retrieve, or generate all points that make up the object.
-
size
public int size()
Retrieve the number of points that make up the object.
-
contains
public boolean contains(Point2d point)
Check if this Bounds2d contains a given point. Contains considers a point on the border of this Bounds2d to be outside.- Parameters:
point
- Point2d; the point- Returns:
- boolean; true this Bounds2d contains the point; false if this Bounds2d does not contain the point
- Throws:
NullPointerException
- when point is null
-
contains
public boolean contains(double x, double y) throws IllegalArgumentException
Check if this Bounds2d contains a point. Contains considers a point on the border of this Bounds2d to be outside.- Parameters:
x
- double; the x-coordinate of the pointy
- double; the y-coordinate of the point- Returns:
- boolean; whether this Bounds2d contains the point
- Throws:
IllegalArgumentException
- when any of the coordinates is NaN
-
contains
public boolean contains(Drawable2d drawable) throws NullPointerException
Check if this Bounds2d completely contains a Drawable2d.- Parameters:
drawable
- Drawable2d; the object for which to check if it is completely contained within this Bounds2d.- Returns:
- boolean; false if any point of the Drawable2d is on or outside one of the borders of this Bounds2d; true when all points of the Drawable2d are contained within this Bounds2d.
- Throws:
NullPointerException
- when drawable2d is null
-
covers
public boolean covers(double x, double y)
Check if this Bounds2d contains a point. Covers returns true when the point is on, or within the border of this Bounds2d.- Parameters:
x
- double; the x-coordinate of the pointy
- double; the y-coordinate of the point- Returns:
- boolean; whether this Bounds2d, including its borders, contains the point
-
covers
public boolean covers(Point2d point)
Check if this Bounds2d contains a point. Covers returns true when the point is on, or within the border of this Bounds2d.- Parameters:
point
- Point2d; the point- Returns:
- boolean; whether this Bounds2d, including its borders, contains the point
- Throws:
NullPointerException
- when point is null
-
covers
public boolean covers(Bounds2d otherBounds2d) throws NullPointerException
Check if this Bounds contains another Bounds. Covers returns true when one of the edges of the other Bounds (partly) overlaps a border of this Bounds.- Specified by:
covers
in interfaceBounds<Bounds2d,Space2d>
- Parameters:
otherBounds2d
- B; the Bounds for which to check if it is contained within this Bounds- Returns:
- boolean; whether this Bounds contains the provided Bounds, including overlapping borders
- Throws:
NullPointerException
- when otherBounds is null
-
disjoint
public boolean disjoint(Bounds2d otherBounds2d) throws NullPointerException
Return whether this Bounds is disjoint from another Bounds. Only touching at an edge is considered disjoint.- Specified by:
disjoint
in interfaceBounds<Bounds2d,Space2d>
- Parameters:
otherBounds2d
- B; the other Bounds- Returns:
- boolean; whether this Bounds is disjoint from another Bounds
- Throws:
NullPointerException
- when bounds is null
-
intersects
public boolean intersects(Bounds2d otherBounds2d) throws NullPointerException
Return whether this Bounds intersects another Bounds. Only touching at an edge is not seen as intersecting.- Specified by:
intersects
in interfaceBounds<Bounds2d,Space2d>
- Parameters:
otherBounds2d
- B; the other Bounds- Returns:
- boolean; whether this bounding rectangle intersects the other Bounds
- Throws:
NullPointerException
- when otherBounds is null
-
intersection
public Bounds2d intersection(Bounds2d otherBounds2d)
Return the intersecting Bounds of this Bounds and another Bounds. Touching at the edge is not seen as intersecting. In case there is no intersection, null is returned.- Specified by:
intersection
in interfaceBounds<Bounds2d,Space2d>
- Parameters:
otherBounds2d
- B; the other Bounds- Returns:
- Bounds; the intersecting Bounds of this Bounds and another Bounds. Touching at the edge is not seen as intersecting. If not intersecting; null is returned
-
toRectangle2D
public Rectangle2D toRectangle2D()
Return an AWT Rectangle2D that covers the same area as this Bounds2d.- Returns:
- Rectangle2D; the rectangle that covers the same area as this Bounds2d
-
getMinX
public double getMinX()
Return the lower bound for x.
-
getMaxX
public double getMaxX()
Return the upper bound for x.
-
getMinY
public double getMinY()
Return the lower bound for y.
-
getMaxY
public double getMaxY()
Return the upper bound for y.
-
midPoint
public Point2d midPoint()
Return the mid point of this Bounds2d.- Returns:
- Point2d; the mid point of this Bounds2d
-
getArea
public double getArea()
Return the area of this Bounds2d.- Returns:
- double; the area of this Bounds2d
-
getBounds
public Bounds2d getBounds()
Retrieve the bounding rectangle of the object.- Specified by:
getBounds
in interfaceDrawable2d
- Returns:
- Bounds2d; the bounding box of the object
-
-