- Object
-
- AABB
-
- All Implemented Interfaces:
Copyable<AABB>
,Translatable
public class AABB extends Object implements Translatable, Copyable<AABB>
Implementation of an Axis-Align Bounding Box.An
AABB
has minimum and maximum coordinates that define the box.An
AABB
can be unioned or intersected with otherAABB
s to combine them into anotherAABB
. If an intersection produces no result, a degenerateAABB
is returned. A degenerateAABB
can be tested by theisDegenerate()
methods and is defined as anAABB
who's maximum and minimum are equal.AABB
s can also be tested for overlap and (full) containment using theoverlaps(AABB)
andcontains(AABB)
method.The
expand(double)
method can be used to expand the bounds of theAABB
by some amount.- Since:
- 3.0.0
- Version:
- 4.0.0
- Author:
- William Bittle
-
-
Constructor Summary
Constructors Constructor Description AABB(double radius)
Full constructor.AABB(double minX, double minY, double maxX, double maxY)
Full constructor.AABB(AABB aabb)
Copy constructor.AABB(Vector2 center, double radius)
Full constructor.AABB(Vector2 min, Vector2 max)
Full constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(double x, double y)
Returns true if the given point's coordinates are contained within thisAABB
.boolean
contains(AABB aabb)
boolean
contains(Vector2 point)
Returns true if the given point is contained within thisAABB
.AABB
copy()
Returns a deep copy of this object.static AABB
createAABBFromPoints(double point1x, double point1y, double point2x, double point2y)
Method to create the valid AABB defined by the two points A(point1x, point1y) and B(point2x, point2y).static AABB
createAABBFromPoints(Vector2 point1, Vector2 point2)
Method to create the valid AABB defined by the two points point1 and point2.boolean
equals(Object obj)
AABB
expand(double expansion)
double
getArea()
Returns the area of thisAABB
;.Vector2
getCenter()
Returns the center of the AABB.AABB
getExpanded(double expansion)
Returns a newAABB
of this AABB expanded by half the given expansion in both the x and y directions.double
getHeight()
Returns the height of thisAABB
.AABB
getIntersection(AABB aabb)
double
getMaxX()
Returns the maximum x extent.double
getMaxY()
Returns the maximum y extent.double
getMinX()
Returns the minimum x extent.double
getMinY()
Returns the minimum y extent.double
getPerimeter()
Returns the perimeter of thisAABB
.AABB
getTranslated(Vector2 translation)
Returns a new AABB of this AABB translated by the given translation amount.AABB
getUnion(AABB aabb)
double
getWidth()
Returns the width of thisAABB
.int
hashCode()
AABB
intersection(AABB aabb)
AABB
intersection(AABB aabb1, AABB aabb2)
boolean
isDegenerate()
Returns true if thisAABB
is degenerate.boolean
isDegenerate(double error)
Returns true if thisAABB
is degenerate given the specified error.boolean
overlaps(AABB aabb)
AABB
set(AABB aabb)
Sets this aabb to the given aabb's value and returns this AABB.String
toString()
void
translate(double x, double y)
Translates the object the given amounts in the respective directions.void
translate(Vector2 translation)
Translates the object along the given vector.AABB
union(AABB aabb)
AABB
union(AABB aabb1, AABB aabb2)
-
-
-
Constructor Detail
-
AABB
public AABB(double minX, double minY, double maxX, double maxY)
Full constructor.- Parameters:
minX
- the minimum x extentminY
- the minimum y extentmaxX
- the maximum x extentmaxY
- the maximum y extent
-
AABB
public AABB(Vector2 min, Vector2 max)
Full constructor.- Parameters:
min
- the minimum extentmax
- the maximum extent- Throws:
IllegalArgumentException
- if either coordinate of the given min is greater than the given max
-
AABB
public AABB(double radius)
Full constructor.- Parameters:
radius
- the radius of a circle fitting inside an AABB- Since:
- 3.1.5
-
AABB
public AABB(Vector2 center, double radius)
Full constructor.Creates an AABB for a circle with the given center and radius.
- Parameters:
center
- the center of the circleradius
- the radius of the circle- Throws:
IllegalArgumentException
- if the given radius is less than zero- Since:
- 3.1.5
-
-
Method Detail
-
createAABBFromPoints
public static AABB createAABBFromPoints(Vector2 point1, Vector2 point2)
Method to create the valid AABB defined by the two points point1 and point2.- Parameters:
point1
- the first pointpoint2
- the second point- Returns:
- The one and only one valid AABB formed by point1 and point2
-
createAABBFromPoints
public static AABB createAABBFromPoints(double point1x, double point1y, double point2x, double point2y)
Method to create the valid AABB defined by the two points A(point1x, point1y) and B(point2x, point2y).- Parameters:
point1x
- The x coordinate of point Apoint1y
- The y coordinate of point Apoint2x
- The x coordinate of point Bpoint2y
- The y coordinate of point B- Returns:
- The one and only one valid AABB formed by A and B
-
copy
public AABB copy()
Description copied from interface:Copyable
Returns a deep copy of this object.
-
set
public AABB set(AABB aabb)
Sets this aabb to the given aabb's value and returns this AABB.- Parameters:
aabb
- the aabb to copy- Returns:
AABB
- Since:
- 3.2.5
-
translate
public void translate(double x, double y)
Description copied from interface:Translatable
Translates the object the given amounts in the respective directions.- Specified by:
translate
in interfaceTranslatable
- Parameters:
x
- the translation in the x directiony
- the translation in the y direction
-
translate
public void translate(Vector2 translation)
Description copied from interface:Translatable
Translates the object along the given vector.- Specified by:
translate
in interfaceTranslatable
- Parameters:
translation
- the translation along a vector
-
getTranslated
public AABB getTranslated(Vector2 translation)
Returns a new AABB of this AABB translated by the given translation amount.- Parameters:
translation
- the translation- Returns:
- AABB
- Since:
- 3.1.1
-
getWidth
public double getWidth()
Returns the width of thisAABB
.- Returns:
- double
- Since:
- 3.0.1
-
getHeight
public double getHeight()
Returns the height of thisAABB
.- Returns:
- double
- Since:
- 3.0.1
-
getPerimeter
public double getPerimeter()
Returns the perimeter of thisAABB
.- Returns:
- double
-
getArea
public double getArea()
Returns the area of thisAABB
;.- Returns:
- double
-
expand
public AABB expand(double expansion)
Expands thisAABB
by half the given expansion in each direction and then returns thisAABB
.The expansion can be negative to shrink the
AABB
. However, if the expansion is greater than the current width/height, theAABB
can become invalid. In this case, the AABB will become a degenerate AABB at the mid point of the min and max for the respective coordinates.- Parameters:
expansion
- the expansion amount- Returns:
AABB
-
getExpanded
public AABB getExpanded(double expansion)
Returns a newAABB
of this AABB expanded by half the given expansion in both the x and y directions.The expansion can be negative to shrink the
AABB
. However, if the expansion is greater than the current width/height, theAABB
can become invalid. In this case, the AABB will become a degenerate AABB at the mid point of the min and max for the respective coordinates.- Parameters:
expansion
- the expansion amount- Returns:
AABB
- Since:
- 3.1.1
-
overlaps
public boolean overlaps(AABB aabb)
-
contains
public boolean contains(Vector2 point)
Returns true if the given point is contained within thisAABB
.- Parameters:
point
- the point to test- Returns:
- boolean
- Since:
- 3.1.1
-
contains
public boolean contains(double x, double y)
Returns true if the given point's coordinates are contained within thisAABB
.- Parameters:
x
- the x coordinate of the pointy
- the y coordinate of the point- Returns:
- boolean
- Since:
- 3.1.1
-
isDegenerate
public boolean isDegenerate()
Returns true if thisAABB
is degenerate.A degenerate
AABB
is one where its min and max x or y coordinates are equal.- Returns:
- boolean
- Since:
- 3.1.1
-
isDegenerate
public boolean isDegenerate(double error)
Returns true if thisAABB
is degenerate given the specified error.An
AABB
is degenerate given some error if max - min <= error for either the x or y coordinate.- Parameters:
error
- the allowed error- Returns:
- boolean
- Since:
- 3.1.1
- See Also:
isDegenerate()
-
getMinX
public double getMinX()
Returns the minimum x extent.- Returns:
- double
-
getMaxX
public double getMaxX()
Returns the maximum x extent.- Returns:
- double
-
getMaxY
public double getMaxY()
Returns the maximum y extent.- Returns:
- double
-
getMinY
public double getMinY()
Returns the minimum y extent.- Returns:
- double
-
-