Package org.djutils.draw.bounds
Class Bounds2d
java.lang.Object
org.djutils.draw.bounds.Bounds2d
- All Implemented Interfaces:
Serializable
,Iterable<Point2d>
,Bounds<Bounds2d,
,Point2d> Drawable<Point2d>
,Drawable2d
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-2025 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, Wouter Schakel
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionBounds2d
(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<Drawable2d> drawableCollection) Construct a Bounds2d for a Collection of Drawable2d objects.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
(Drawable2d... drawable2d) Construct a Bounds2d for several Drawable2d objects.Construct a Bounds2d from an array of Point2d, finding the lowest and highest x and y coordinates. -
Method Summary
Modifier and TypeMethodDescriptionboolean
contains
(double x, double y) Check if this Bounds2d contains a point.boolean
Check if this Bounds completely contains another Bounds.boolean
Check if a point is contained in this Bounds.boolean
covers
(double x, double y) Check if this Bounds2d covers a point.boolean
Check if no part of a Bounds is outside this Bounds.boolean
Check if this Bounds covers or touches a point.boolean
Return whether a Bounds is disjoint from this Bounds.static Iterator<Drawable2d>
ensureHasOne
(Iterator<Drawable2d> iterator) Verify that the iterator has something to return.(package private) static Drawable2d[]
ensureHasOne
(Drawable2d[] drawable2dArray) Verify that the array contains at least one entry.boolean
double
getArea()
Return the area of this Bounds2d.Retrieve the bounding rectangle of the object.double
getMaxX()
Return the absolute upper bound for x.double
getMaxY()
Return the absolute upper bound for y.double
getMinX()
Return the absolute lower bound for x.double
getMinY()
Return the absolute lower bound for y.int
hashCode()
intersection
(Bounds2d otherBounds2d) Return the intersecting Bounds of this Bounds and another Bounds.boolean
intersects
(Bounds2d otherBounds2d) Return whether this Bounds intersects another Bounds.iterator()
midPoint()
Return the mid point of this Bounds object.pointsOf
(Collection<Drawable2d> drawableCollection) Return an iterator that will return all points of one or more Drawable2d objects.pointsOf
(Drawable2d... drawable2d) Return an iterator that will return all points of one or more Drawable objects.int
size()
Retrieve the number of points that make up the object.Return an AWT Rectangle2D that covers the same area as this Bounds2d.toString()
Produce a string describing the Drawable using default conversion for the (double) coordinate values.Produce a String describing the Drawable.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.djutils.draw.Drawable
getPointList, toString, toString
Methods inherited from interface org.djutils.draw.Drawable2d
getDimensions
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Bounds2d
public Bounds2d(double minX, double maxX, double minY, double maxY) throws ArithmeticException, IllegalArgumentException Construct a Bounds2d by providing its lower and upper bounds in both dimensions.- Parameters:
minX
- the lower bound for xmaxX
- the upper bound for xminY
- the lower bound for ymaxY
- the upper bound for y- Throws:
ArithmeticException
- whenminX
,maxX
,minY
, ormaxY
isNaN
IllegalArgumentException
- whenminX
>maxX
, orminY
>maxY
-
Bounds2d
public Bounds2d(double deltaX, double deltaY) Constructs a new Bounds2d around the origin (0, 0).- Parameters:
deltaX
- the deltaX value around the origindeltaY
- the deltaY value around the origin- Throws:
ArithmeticException
- whendeltaX
, ordeltaY
isNaN
IllegalArgumentException
- whendeltaX < 0.0
, ordeltaY < 0.0
-
Bounds2d
Construct a Bounds2d from some collection of points, finding the lowest and highest x and y coordinates.- Parameters:
points
- Iterator that will generate all the points for which to construct a Bounds2d- Throws:
NullPointerException
- whenpoints
isnull
IllegalArgumentException
- when thepoints
iterator provides zero points
-
Bounds2d
Construct a Bounds2d from an array of Point2d, finding the lowest and highest x and y coordinates.- Parameters:
points
- the points to construct a Bounds2d from- Throws:
NullPointerException
- whenpoints
isnull
IllegalArgumentException
- when zero points are provided
-
Bounds2d
Construct a Bounds2d for a Drawable2d.- Parameters:
drawable2d
- any object that implements the Drawable2d interface- Throws:
NullPointerException
- whendrawable2d
isnull
-
Bounds2d
Construct a Bounds2d for several Drawable2d objects.- Parameters:
drawable2d
- the Drawable2d objects- Throws:
NullPointerException
- when thedrawable2d
array isnull
, or contains anull
valueIllegalArgumentException
- when the length of thedrawable2d
array is 0
-
Bounds2d
Construct a Bounds2d for a Collection of Drawable2d objects.- Parameters:
drawableCollection
- the collection- Throws:
NullPointerException
- when thedrawableCollection
isnull
, or contains anull
valueIllegalArgumentException
- when thedrawableCollection
is empty
-
-
Method Details
-
ensureHasOne
Verify that the array contains at least one entry.- Parameters:
drawable2dArray
- array of Drawable2d objects- Returns:
- the array
- Throws:
NullPointerException
- whendrawable2darray
isnull
IllegalArgumentException
- when thedrawable2dArray
contains 0 elements
-
pointsOf
Return an iterator that will return all points of one or more Drawable objects.- Parameters:
drawable2d
- the Drawable objects- Returns:
- iterator that will return all points of the Drawable objects
- Throws:
NullPointerException
- whendrawable2d
isnull
, or contains anull
valueIllegalArgumentException
- whendrawable2d
is empty
-
pointsOf
Return an iterator that will return all points of one or more Drawable2d objects.- Parameters:
drawableCollection
- the collection of Drawable2d objects- Returns:
- iterator that will return all points of the Drawable objects
- Throws:
NullPointerException
- when thedrawableCollection
isnull
, or contains anull
valueIllegalArgumentException
- when thedrawableCollection
is empty
-
ensureHasOne
Verify that the iterator has something to return.- Parameters:
iterator
- the iterator- Returns:
- the iterator
- Throws:
NullPointerException
- when theiterator
isnull
IllegalArgumentException
- when thehasNext
method of theiterator
returnsfalse
before even oneDrawable2d
was delivered
-
iterator
-
size
public int size()Description copied from interface:Drawable
Retrieve the number of points that make up the object. -
contains
public boolean contains(double x, double y) Check if this Bounds2d contains a point. Contains considers a point on the border of this Bounds2d to be outside.- Parameters:
x
- the x-coordinate of the pointy
- the y-coordinate of the point- Returns:
- whether this Bounds2d contains the point
- Throws:
ArithmeticException
- whenx
, ory
isNaN
-
contains
Description copied from interface:Bounds
Check if a point is contained in this Bounds. -
contains
Description copied from interface:Bounds
Check if this Bounds completely contains another Bounds. If any point of the other Bounds lies on an edge (2d) or surface (3d) of this Bounds, this method returnsfalse
.- Specified by:
contains
in interfaceBounds<Bounds2d,
Point2d> - Parameters:
otherBounds
- the Bounds to check for complete containment within this Bounds.- Returns:
false
if any point of D is on or outside one of the borders of this Bounds;true
when all points of D are contained within this Bounds.- Throws:
NullPointerException
- whenotherBounds
isnull
-
covers
public boolean covers(double x, double y) Check if this Bounds2d covers a point. Covers returnstrue
when the point is on, or inside this Bounds2d.- Parameters:
x
- the x-coordinate of the pointy
- the y-coordinate of the point- Returns:
- whether this Bounds2d, including its borders, contains the point
- Throws:
ArithmeticException
- whenx
, ory
isNaN
-
covers
Description copied from interface:Bounds
Check if this Bounds covers or touches a point. -
covers
Description copied from interface:Bounds
Check if no part of a Bounds is outside this Bounds. The edges/surfaces of this Bounds are considered inside. -
disjoint
Description copied from interface:Bounds
Return whether a Bounds is disjoint from this Bounds. Touching at an edge is not considered disjoint. A Bounds that completely surrounds this Bounds is not disjoint. -
intersects
Description copied from interface:Bounds
Return whether this Bounds intersects another Bounds. Touching at an edge is considered intersecting.- Specified by:
intersects
in interfaceBounds<Bounds2d,
Point2d> - Parameters:
otherBounds2d
- the other Bounds- Returns:
- whether this bounding box/rectangle intersects the other Bounds
-
intersection
Description copied from interface:Bounds
Return the intersecting Bounds of this Bounds and another Bounds. Touching at an edge is considered intersecting. In case there is no intersection, null is returned.- Specified by:
intersection
in interfaceBounds<Bounds2d,
Point2d> - Parameters:
otherBounds2d
- the other Bounds- Returns:
- 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
Return an AWT Rectangle2D that covers the same area as this Bounds2d.- Returns:
- java.awt.geom.Rectangle2D; the rectangle that covers the same area as this Bounds2d
-
getMinX
public double getMinX()Description copied from interface:Bounds
Return the absolute lower bound for x. -
getMaxX
public double getMaxX()Description copied from interface:Bounds
Return the absolute upper bound for x. -
getMinY
public double getMinY()Description copied from interface:Bounds
Return the absolute lower bound for y. -
getMaxY
public double getMaxY()Description copied from interface:Bounds
Return the absolute upper bound for y. -
midPoint
Description copied from interface:Bounds
Return the mid point of this Bounds object. -
getArea
public double getArea()Return the area of this Bounds2d.- Returns:
- the area of this Bounds2d
-
getBounds
Description copied from interface:Drawable2d
Retrieve the bounding rectangle of the object.- Specified by:
getBounds
in interfaceDrawable2d
- Returns:
- the bounding box of the object
-
toString
Description copied from interface:Drawable
Produce a string describing the Drawable using default conversion for the (double) coordinate values. Regrettably, it is not allowed to provide a default implementation here. -
toString
Description copied from interface:Drawable
Produce a String describing the Drawable.- Specified by:
toString
in interfaceDrawable<Point2d>
- Parameters:
doubleFormat
- a format string (something like "%6.3f") which will be used to render every coordinate value)doNotIncludeClassName
- iftrue
; the output oftoString
is not prefixed by the class name. This is useful for concatenating the textual representation of lots of Drawables (e.g. an array, or a List).- Returns:
- textual representation of the Drawable
-
hashCode
public int hashCode() -
equals
-