Module org.dyn4j

Class 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 other AABBs to combine them into another AABB. If an intersection produces no result, a degenerate AABB is returned. A degenerate AABB can be tested by the isDegenerate() methods and is defined as an AABB who's maximum and minimum are equal.

    AABBs can also be tested for overlap and (full) containment using the overlaps(AABB) and contains(AABB) method.

    The expand(double) method can be used to expand the bounds of the AABB by some amount.

    Since:
    3.0.0
    Version:
    4.0.0
    Author:
    William Bittle
    • Constructor Detail

      • AABB

        public AABB​(double minX,
                    double minY,
                    double maxX,
                    double maxY)
        Full constructor.
        Parameters:
        minX - the minimum x extent
        minY - the minimum y extent
        maxX - the maximum x extent
        maxY - the maximum y extent
      • AABB

        public AABB​(Vector2 min,
                    Vector2 max)
        Full constructor.
        Parameters:
        min - the minimum extent
        max - 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 circle
        radius - the radius of the circle
        Throws:
        IllegalArgumentException - if the given radius is less than zero
        Since:
        3.1.5
      • AABB

        public AABB​(AABB aabb)
        Copy constructor.
        Parameters:
        aabb - the AABB to copy
        Since:
        3.1.1
    • 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 point
        point2 - 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 A
        point1y - The y coordinate of point A
        point2x - The x coordinate of point B
        point2y - 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.
        Specified by:
        copy in interface Copyable<AABB>
        Returns:
        T
      • 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
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • 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 interface Translatable
        Parameters:
        x - the translation in the x direction
        y - 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 interface Translatable
        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 this AABB.
        Returns:
        double
        Since:
        3.0.1
      • getHeight

        public double getHeight()
        Returns the height of this AABB.
        Returns:
        double
        Since:
        3.0.1
      • getPerimeter

        public double getPerimeter()
        Returns the perimeter of this AABB.
        Returns:
        double
      • getArea

        public double getArea()
        Returns the area of this AABB;.
        Returns:
        double
      • union

        public AABB union​(AABB aabb)
        Performs a union of this AABB and the given AABB placing the result of the union into this AABB and then returns this AABB
        Parameters:
        aabb - the AABB to union
        Returns:
        AABB
      • union

        public AABB union​(AABB aabb1,
                          AABB aabb2)
        Performs a union of the given AABBs and places the result into this AABB and then returns this AABB.
        Parameters:
        aabb1 - the first AABB to union
        aabb2 - the second AABB to union
        Returns:
        AABB
        Since:
        4.0.0
      • getUnion

        public AABB getUnion​(AABB aabb)
        Performs a union of this AABB and the given AABB returning a new AABB containing the result.
        Parameters:
        aabb - the AABB to union
        Returns:
        AABB the resulting union
      • intersection

        public AABB intersection​(AABB aabb)
        Performs the intersection of this AABB and the given AABB placing the result into this AABB and then returns this AABB.

        If the given AABB does not overlap this AABB, this AABB is set to a zero AABB.

        Parameters:
        aabb - the AABB to intersect
        Returns:
        AABB
        Since:
        3.1.1
      • intersection

        public AABB intersection​(AABB aabb1,
                                 AABB aabb2)
        Performs the intersection of the given AABBs and places the result into this AABB and then returns this AABB.

        If the given AABBs do not overlap, this AABB is set to a zero AABB.

        Parameters:
        aabb1 - the first AABB to intersect
        aabb2 - the second AABB to intersect
        Returns:
        AABB
        Since:
        4.0.0
      • getIntersection

        public AABB getIntersection​(AABB aabb)
        Performs the intersection of this AABB and the given AABB returning the result in a new AABB.

        If the given AABB does not overlap this AABB, a zero AABB is returned.

        Parameters:
        aabb - the AABB to intersect
        Returns:
        AABB
        Since:
        3.1.1
      • expand

        public AABB expand​(double expansion)
        Expands this AABB by half the given expansion in each direction and then returns this AABB.

        The expansion can be negative to shrink the AABB. However, if the expansion is greater than the current width/height, the AABB 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 new AABB 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, the AABB 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)
        Returns true if the given AABB and this AABB overlap.
        Parameters:
        aabb - the AABB to test
        Returns:
        boolean true if the AABBs overlap
      • contains

        public boolean contains​(AABB aabb)
        Returns true if the given AABB is contained within this AABB.
        Parameters:
        aabb - the AABB to test
        Returns:
        boolean
      • contains

        public boolean contains​(Vector2 point)
        Returns true if the given point is contained within this AABB.
        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 this AABB.
        Parameters:
        x - the x coordinate of the point
        y - the y coordinate of the point
        Returns:
        boolean
        Since:
        3.1.1
      • isDegenerate

        public boolean isDegenerate()
        Returns true if this AABB 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 this AABB 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()
      • getCenter

        public Vector2 getCenter()
        Returns the center of the AABB.
        Returns:
        Vector2
        Since:
        4.0.0
      • 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