Class WorldUtils


  • public final class WorldUtils
    extends Object
    Library for geometric problems
    • Constructor Detail

      • WorldUtils

        public WorldUtils()
    • Method Detail

      • getDistance

        public static double getDistance​(CoordI2 v)
        The Pythagorean theorem
        Parameters:
        v - Vector
        Returns:
        Length of vector v
      • getDistance

        public static double getDistance​(CoordD2 v)
        The Pythagorean theorem
        Parameters:
        v - Vector
        Returns:
        Length of vector v
      • getDistance

        public static double getDistance​(HasPosition2 p0,
                                         HasPosition2 p1)
        The Pythagorean theorem
        Parameters:
        p0 - Coordinate 0
        p1 - Coordinate 1
        Returns:
        Distance
      • getDistance

        public static double getDistance​(CoordI2 c0,
                                         CoordI2 c1)
        The Pythagorean theorem
        Parameters:
        c0 - Coordinate 0
        c1 - Coordinate 1
        Returns:
        Distance
      • getDistance

        public static double getDistance​(CoordD2 c0,
                                         CoordD2 c1)
        The Pythagorean theorem
        Parameters:
        c0 - Coordinate 0
        c1 - Coordinate 1
        Returns:
        Distance
      • getVectorAngle

        public static double getVectorAngle​(CoordI2 v0,
                                            CoordI2 v1)
        Calculate the angle between 2 vectors
        Parameters:
        v0 - Vector 0
        v1 - Vector 1
        Returns:
        Angle (radians)
      • getVectorAngle

        public static double getVectorAngle​(CoordD2 v0,
                                            CoordD2 v1)
        Calculate the angle between 2 vectors
        Parameters:
        v0 - Vector 0
        v1 - Vector 1
        Returns:
        Angle (radians)
      • getVectorAngle

        public static double getVectorAngle​(CoordI2 v)
        Calculate the direction of a vector
        Parameters:
        v0 - Vector 0
        v1 - Vector 1
        Returns:
        Angle (radians)
      • getVectorAngle

        public static double getVectorAngle​(CoordD2 v)
        Calculate the direction of a vector
        Parameters:
        v0 - Vector 0
        v1 - Vector 1
        Returns:
        Angle (radians)
      • getVectorDotProduct

        public static double getVectorDotProduct​(CoordD2 v0,
                                                 CoordD2 v1)
        Calculate the dot product
        Parameters:
        v0 - Vector 0
        v1 - Vector 1
        Returns:
        Dot product
      • rotateVector

        public static CoordD2 rotateVector​(CoordD2 v,
                                           double θ)
        Rotates a vector
        Returns:
        Rotated vector
      • isInBounds

        public static boolean isInBounds​(HasPosition2 hasPosition,
                                         CoordI2 dimension)
        Checks whether coord is within bounds (e.g. composite entity)
        Parameters:
        hasPosition -
        Returns:
      • isInBounds

        public static boolean isInBounds​(CoordI2 coord,
                                         CoordI2 dimension)
        Checks whether coord is within bounds (e.g. composite entity)
        Parameters:
        coord -
        Returns:
      • strictSignum

        public static int strictSignum​(int n)
        Signum function that strictly returns -1 or 1.
        Parameters:
        n -
        Returns:
        Mathematical signum function that returns -1 if n is negative and 1 if n is positive (including 0)
      • strictSignum

        public static int strictSignum​(double n)
        Signum function that strictly returns -1 or 1.
        Parameters:
        n -
        Returns:
        Mathematical signum function that returns -1 if n is negative and 1 if n is positive (including 0)
      • intersectRectangles

        public static RectI2 intersectRectangles​(RectI2 r0,
                                                 RectI2 r1)
        Intersects 2 recangles
        Parameters:
        r0 - Rectangle 0
        r1 - Rectangle 1
        Returns:
        The intersected area if r0 and r1 overlap, else null
      • intersects

        public static boolean intersects​(HasPositionAndDimension2 obj0,
                                         HasPositionAndDimension2 obj1)
        Checks whether 2 objects intersect
        Parameters:
        obj0 - HasPositionAndDimension2 object 0
        obj1 - HasPositionAndDimension2 object 1
        Returns:
        true / false whether obj0 intersects with obj1
      • intersects

        public static boolean intersects​(CoordI2 position0,
                                         CoordI2 dimension0,
                                         CoordI2 position1,
                                         CoordI2 dimension1)
        Checks whether 2 objects intersect
        Parameters:
        position0 - position of object 0
        dimension0 - dimension of object 0
        position1 - position of object 1
        dimension1 - dimension of object 1
        Returns:
        true / false whether obj0 intersects with obj1
      • translateWorldPosToEntityPos

        public static CoordI2 translateWorldPosToEntityPos​(AbstractCompositeEntity compositeEntity,
                                                           CoordI2 worldPos,
                                                           boolean ignoreBounds)
        Translates world coordinate to relative coordinate within composite entity
        Parameters:
        compositeEntity -
        worldPos -
        Returns:
        Relative entity coordinate, null if coordinate is outside bounds of the entity
      • translateEntityPosToWorldPos

        public static CoordI2 translateEntityPosToWorldPos​(AbstractCompositeEntity compositeEntity,
                                                           CoordI2 entityPos)
        Translates coordinate within composite entity to absolute world coordinate
        Parameters:
        compositeEntity -
        entityPos -
        Returns:
        Coordinate of entityPos in the world
      • pointsToArea

        public static Area2 pointsToArea​(CoordI2... points)
        Builds an aligned Area2 from arbitrary points
        Returns:
      • intercept

        public static CoordI2 intercept​(CoordI2 pOrigin_,
                                        double velOrigin,
                                        CoordI2 pTarget_,
                                        CoordD2 vTarget_)
        Calculates the point of intersection to intercept target from origin with velocity velOrigin
        Parameters:
        pOrigin_ - Position of origin (e.g. ship that intercepts target)
        velOrigin - Velocity of origin
        pTarget_ - Position of target
        vTarget_ - Velocity vector of target
        Returns:
        Point of interception or null if interception is not possible
      • getSector

        public static Edge2 getSector​(CoordI2 point,
                                      HasPositionAndDimension2 source)
        Returns the sector the point is located from source's view
        Parameters:
        point -
        source -
        Returns:
        Sector (Edge2)
      • getPedal

        public static CoordD2 getPedal​(CoordI2 a,
                                       CoordI2 b,
                                       CoordI2 c)
        Calculates the pedal of altitude c of triangle A.B,C
        Parameters:
        a - Point A
        b - Point B
        c - Point C
        Returns:
        Pedal of altitude c
      • getPedalOnLineSegmentAB

        public static CoordD2 getPedalOnLineSegmentAB​(CoordI2 a,
                                                      CoordI2 b,
                                                      CoordI2 c)
        Get's the pedal of altitude c of triangle A.B,C or the closest point on line segment a,b
           a-----p2-------b   p1
                 |         \  |
                 |          \ |
                 |           \|
                 c2            c1
         
        Returns null if there is no pedal.
      • getClosestPoint

        public static CoordI2 getClosestPoint​(CoordI2 origin,
                                              HasPositionAndDimension2 target)
        Get's the closest point at area target from origin
                        *-----*
              |     |
              |     x _______ origin
              |     |
              *-----*
         
        Parameters:
        origin -
        target -
        Returns:
      • evade

        public static List<CoordI2> evade​(HasPositionAndDimension2 origin,
                                          double velOrigin,
                                          CoordD2 vOrigin,
                                          HasPositionAndDimension2 target,
                                          CoordD2 vTarget,
                                          CoordI2 oTarget,
                                          CoordI2 pInterception_)
        Calculates waypoints to evade a collision between origin and target.
        Parameters:
        pOrigin - Origin
        velOrigin - Origin velocity
        vOrigin - Origin velocity vector, origin course not necessarily the current velocity vector
        pTarget - Target
        vTarget - Target velocity vector
        oTarget - Collision offset on target (entityPos)
        pInterception - (Optional) Point of interception. Can be null, in this case it is calculated based on the velocity of source and the velocity vector of target.
        Returns:
        Waypoints for evasion or null if there is no collision
      • getVertices

        public static List<CoordI2> getVertices​(HasPositionAndDimension2 obj)
          
           x-----x
           |     |
           |     |
           x-----x
         
        Returns the 4 vertices of a rectangle
        Parameters:
        obj - Rectangle
        Returns:
        4 vertices ordered clock-wise
      • getSides

        public static List<Area2> getSides​(HasPositionAndDimension2 obj)
          
           x-----x
           |     |
           |     |
           x-----x
         
        Returns the 4 sides of a rectangle
        Parameters:
        obj - Rectangle
        Returns:
        4 sides ordered clock-wise
      • getRandomPointInCircle

        public static CoordI2 getRandomPointInCircle​(CoordI2 center,
                                                     int radius)
        Returns a random point in a circle
        Parameters:
        center - of the circle
        radius - of the circle
        Returns:
        random point in the circle
      • getRandomPointInCircle

        public static CoordI2 getRandomPointInCircle​(CoordI2 center,
                                                     int radius,
                                                     Random random)
        Returns a point in a circle
        Parameters:
        center - of the circle
        radius - of the circle
        random - rng
        Returns:
        random point in the circle
      • getRandomPointOnCircle

        public static CoordI2 getRandomPointOnCircle​(CoordI2 center,
                                                     int radius)
        Returns a random point on a circle
        Parameters:
        center - of the circle
        radius - of the circle
        Returns:
        random point in the circle
      • getRandomPointOnCircle

        public static CoordI2 getRandomPointOnCircle​(CoordI2 center,
                                                     int radius,
                                                     Random random)
        Returns a random point on a circle
        Parameters:
        center - of the circle
        radius - of the circle
        random - rng
        Returns:
        random point in the circle
      • getReproduciblePointOnCircle

        public static CoordI2 getReproduciblePointOnCircle​(CoordI2 center,
                                                           int radius,
                                                           AtomicInteger counter)
        Returns a reproducible point on a circle
        Parameters:
        center - of the circle
        radius - of the circle
        counter - decides where the point is
        Returns:
        random point in the circle
      • calcEdgeRequirement

        public static int calcEdgeRequirement​(CoordI2 c0,
                                              CoordI2 c1)
        Calculates edge required at c1 to block the path from c0 to c1. Swap both parameters to get edge required at c0 to block the path from c1 to c0.

        c0==> |c1
        c0| <==c1
        Parameters:
        c0 - Position
        c1 - Target
        Returns:
        Edges that are required as bit mask