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:
    5.0.0
    Author:
    William Bittle
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected double maxX
      The maximum extent along the x-axis
      protected double maxY
      The maximum extent along the y-axis
      protected double minX
      The minimum extent along the x-axis
      protected double minY
      The minimum extent along the y-axis
    • 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 this AABB.
      boolean contains​(AABB aabb)
      Returns true if the given AABB is contained within this AABB.
      boolean contains​(Vector2 point)
      Returns true if the given point is contained within this AABB.
      AABB copy()
      Returns a deep copy of this object.
      static AABB createFromPoints​(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 createFromPoints​(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)
      Expands this AABB by half the given expansion in each direction and then returns this AABB.
      double getArea()
      Returns the area of this AABB;.
      Vector2 getCenter()
      Returns the center of the AABB.
      AABB getExpanded​(double expansion)
      Returns a new AABB of this AABB expanded by half the given expansion in both the x and y directions.
      double getHeight()
      Returns the height of this AABB.
      AABB getIntersection​(AABB aabb)
      Performs the intersection of this AABB and the given AABB returning the result in a new 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 this AABB.
      AABB getTranslated​(Vector2 translation)
      Returns a new AABB of this AABB translated by the given translation amount.
      AABB getUnion​(AABB aabb)
      Performs a union of this AABB and the given AABB returning a new AABB containing the result.
      double getWidth()
      Returns the width of this AABB.
      int hashCode()  
      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.
      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.
      boolean isDegenerate()
      Returns true if this AABB is degenerate.
      boolean isDegenerate​(double error)
      Returns true if this AABB is degenerate given the specified error.
      boolean overlaps​(AABB aabb)
      Returns true if the given AABB and this AABB overlap.
      AABB set​(AABB aabb)
      Sets this aabb to the given aabb's value and returns this AABB.
      static void setFromPoints​(double point1x, double point1y, double point2x, double point2y, AABB result)
      Method to create the valid AABB defined by the two points A(point1x, point1y) and B(point2x, point2y) and places the result in the given AABB.
      static void setFromPoints​(Vector2 point1, Vector2 point2, AABB result)
      Method to create the valid AABB defined by the two points point1 and point2 and places the result in the given 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)
      Performs a union of this AABB and the given AABB placing the result of the union into this AABB and then returns this AABB
      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.
      void zero()
      Sets this AABB to a degenerate zero AABB.
    • Field Detail

      • minX

        protected double minX
        The minimum extent along the x-axis
      • minY

        protected double minY
        The minimum extent along the y-axis
      • maxX

        protected double maxX
        The maximum extent along the x-axis
      • maxY

        protected double maxY
        The maximum extent along the y-axis
    • 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
        Throws:
        IllegalArgumentException - if minX is greater than maxX or if minY is greater than maxY
      • 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

      • createFromPoints

        public static AABB createFromPoints​(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
      • createFromPoints

        public static AABB createFromPoints​(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
      • setFromPoints

        public static void setFromPoints​(Vector2 point1,
                                         Vector2 point2,
                                         AABB result)
        Method to create the valid AABB defined by the two points point1 and point2 and places the result in the given AABB.
        Parameters:
        point1 - the first point
        point2 - the second point
        result - the AABB to set
        Since:
        4.1.0
      • setFromPoints

        public static void setFromPoints​(double point1x,
                                         double point1y,
                                         double point2x,
                                         double point2y,
                                         AABB result)
        Method to create the valid AABB defined by the two points A(point1x, point1y) and B(point2x, point2y) and places the result in the given AABB.
        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
        result - the AABB to set
        Since:
        4.1.0
      • 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
      • zero

        public void zero()
        Sets this AABB to a degenerate zero AABB.
        Since:
        4.1.0
      • 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