LazyLib

org.lazywizard.lazylib
Class MathUtils

Object
  extended by MathUtils

public class MathUtils
extends Object

Contains methods for working with vectors, angles, distances, and circles.

Since:
1.0
Author:
LazyWizard

Method Summary
static float clampAngle(float angle)
          Clamps an angle within 360 degrees (ex: 400 degrees becomes 40 degrees).
static boolean equals(float a, float b)
          Tests for near-equality of floating point numbers.
static float getAngle(Vector2f from, Vector2f to)
          Returns the angle between two Vector2fs.
static Vector2f getDirectionalVector(CombatEntityAPI source, CombatEntityAPI destination)
          Returns a normalized Vector2f pointing from source to destination.
static Vector2f getDirectionalVector(CombatEntityAPI source, Vector2f destination)
          Returns a normalized Vector2f pointing from source to destination.
static Vector2f getDirectionalVector(Vector2f source, Vector2f destination)
          Returns a normalized Vector2f pointing from source to destination.
static float getDistance(CombatEntityAPI obj1, CombatEntityAPI obj2)
          Returns the distance between two CombatEntityAPIs, including collision radius.
static float getDistance(CombatEntityAPI entity, Vector2f vector)
          Returns the distance between a CombatEntityAPI and a Vector2f (includes collision radius).
static float getDistance(SectorEntityToken token1, SectorEntityToken token2)
          Returns the distance between two SectorEntityTokens, including interaction radius.
static float getDistance(SectorEntityToken token, Vector2f vector)
          Returns the distance between a SectorEntityToken and a Vector2f (includes interaction radius).
static float getDistance(Vector2f vector1, Vector2f vector2)
          Returns the distance between two Vector2fs.
static float getDistanceSquared(CombatEntityAPI obj1, CombatEntityAPI obj2)
          Returns the distance squared between two CombatEntityAPIs (includes collision radii).
static float getDistanceSquared(CombatEntityAPI entity, Vector2f vector)
          Returns the distance squared between a CombatEntityAPI and a Vector2f (includes collision radius).
static float getDistanceSquared(SectorEntityToken token1, SectorEntityToken token2)
          Returns the distance squared between two SectorEntityTokens.
static float getDistanceSquared(SectorEntityToken token, Vector2f vector)
          Returns the distance squared between a SectorEntityToken and a Vector2f.
static float getDistanceSquared(Vector2f vector1, Vector2f vector2)
          Returns the distance squared between two Vector2fs (avoids a costly sqrt()).
static List<Vector2f> getEquidistantPointsInsideCircle(Vector2f center, float radius, float spaceBetweenPoints)
          Returns a List of evenly spaced Vector2fs inside a circle.
static float getFacing(Vector2f vector)
          Returns the facing of a Vector2f.
static Vector2f getPointOnCircumference(Vector2f center, float radius, float angle)
          Returns a point along the circumference of a circle at the given angle.
static List<Vector2f> getPointsAlongCircumference(Vector2f center, float radius, int numPoints, float angleOffset)
          Returns an evenly distributed List of points along a circle's circumference.
static Random getRandom()
          Returns the random number generator used by LazyLib's methods.
static float getRandomNumberInRange(float min, float max)
          Returns a random number within a given range.
static Vector2f getRandomPointInCircle(Vector2f center, float radius)
          Returns a random point inside of a circle with uniform distribution.
static Vector2f getRandomPointOnCircumference(Vector2f center, float radius)
          Returns a random point along the circumference of a circle.
static Vector2f getRandomPointOnLine(Vector2f lineStart, Vector2f lineEnd)
          Returns a random point along the line between two Vector2fs.
static boolean isPointOnLine(Vector2f point, Vector2f lineStart, Vector2f lineEnd)
          Check if a point is along the line between two Vector2fs.
static boolean isPointWithinBounds(Vector2f point, CombatEntityAPI entity)
          Deprecated. Use CollisionUtils.isPointWithinBounds(org.lwjgl.util.vector.Vector2f, com.fs.starfarer.api.combat.CombatEntityAPI) instead.
static boolean isPointWithinCircle(Vector2f point, Vector2f center, float radius)
          Returns whether a point is within the bounds of a circle or not.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getDistance

public static float getDistance(SectorEntityToken token1,
                                SectorEntityToken token2)
Returns the distance between two SectorEntityTokens, including interaction radius.

Since:
1.0
See Also:
getDistance(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

getDistance

public static float getDistance(SectorEntityToken token,
                                Vector2f vector)
Returns the distance between a SectorEntityToken and a Vector2f (includes interaction radius).

Since:
1.1
See Also:
getDistance(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

getDistance

public static float getDistance(CombatEntityAPI obj1,
                                CombatEntityAPI obj2)
Returns the distance between two CombatEntityAPIs, including collision radius.

Since:
1.0
See Also:
getDistance(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

getDistance

public static float getDistance(CombatEntityAPI entity,
                                Vector2f vector)
Returns the distance between a CombatEntityAPI and a Vector2f (includes collision radius).

Since:
1.0
See Also:
getDistance(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

getDistance

public static float getDistance(Vector2f vector1,
                                Vector2f vector2)
Returns the distance between two Vector2fs. For comparing distances, it is vastly more efficient to use getDistanceSquared(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f).

Returns:
The distance between the two vectors.
Since:
1.0

getDistanceSquared

public static float getDistanceSquared(SectorEntityToken token1,
                                       SectorEntityToken token2)
Returns the distance squared between two SectorEntityTokens.

Since:
1.0
See Also:
getDistanceSquared(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

getDistanceSquared

public static float getDistanceSquared(SectorEntityToken token,
                                       Vector2f vector)
Returns the distance squared between a SectorEntityToken and a Vector2f.

Since:
1.1
See Also:
getDistanceSquared(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

getDistanceSquared

public static float getDistanceSquared(CombatEntityAPI obj1,
                                       CombatEntityAPI obj2)
Returns the distance squared between two CombatEntityAPIs (includes collision radii). With the addition of collision radius checking, there's no way to avoid calculating the square root. getDistance(com.fs.starfarer.api.combat.CombatEntityAPI, com.fs.starfarer.api.combat.CombatEntityAPI) will be just as efficient.

Since:
1.0
See Also:
getDistanceSquared(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

getDistanceSquared

public static float getDistanceSquared(CombatEntityAPI entity,
                                       Vector2f vector)
Returns the distance squared between a CombatEntityAPI and a Vector2f (includes collision radius). With the addition of collision radius checking, there's no way to avoid calculating the square root. getDistance(com.fs.starfarer.api.combat.CombatEntityAPI, org.lwjgl.util.vector.Vector2f) will be just as efficient.

Since:
1.0
See Also:
getDistanceSquared(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

getDistanceSquared

public static float getDistanceSquared(Vector2f vector1,
                                       Vector2f vector2)
Returns the distance squared between two Vector2fs (avoids a costly sqrt()). When comparing distances, use this function instead of getDistance(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f).

Returns:
The distance squared between the two vectors.
Since:
1.0

getDirectionalVector

public static Vector2f getDirectionalVector(Vector2f source,
                                            Vector2f destination)
Returns a normalized Vector2f pointing from source to destination.

Parameters:
source - The origin of the vector.
destination - The location to point at.
Returns:
A normalized Vector2f pointing at destination.
Since:
1.0

getDirectionalVector

public static Vector2f getDirectionalVector(CombatEntityAPI source,
                                            Vector2f destination)
Returns a normalized Vector2f pointing from source to destination.

Since:
1.0
See Also:
getDirectionalVector(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

getDirectionalVector

public static Vector2f getDirectionalVector(CombatEntityAPI source,
                                            CombatEntityAPI destination)
Returns a normalized Vector2f pointing from source to destination.

Since:
1.0
See Also:
getDirectionalVector(org.lwjgl.util.vector.Vector2f, org.lwjgl.util.vector.Vector2f)

clampAngle

public static float clampAngle(float angle)
Clamps an angle within 360 degrees (ex: 400 degrees becomes 40 degrees).

Parameters:
angle - The angle to be clamped.
Returns:
A value between 0 and 360 degrees.
Since:
1.2

getFacing

public static float getFacing(Vector2f vector)
Returns the facing of a Vector2f.

Parameters:
vector - The vector to get the facing of.
Returns:
The facing (angle) of vector.
Since:
1.0

getAngle

public static float getAngle(Vector2f from,
                             Vector2f to)
Returns the angle between two Vector2fs.

Parameters:
from - The source Vector2f.
to - The Vector2f to get the angle to.
Returns:
The angle from from to to.
Since:
1.0

getPointOnCircumference

public static Vector2f getPointOnCircumference(Vector2f center,
                                               float radius,
                                               float angle)
Returns a point along the circumference of a circle at the given angle.

Parameters:
center - The center point of the circle (can be null for a 0, 0 origin).
radius - The radius of the circle.
angle - The angle, in degrees, to get the point at.
Returns:
A Vector2f at [@code angle} degrees along the circumference of the given circle.
Since:
1.0

getRandomPointOnCircumference

public static Vector2f getRandomPointOnCircumference(Vector2f center,
                                                     float radius)
Returns a random point along the circumference of a circle.

Parameters:
center - The center point of the circle (can be null for a 0, 0 origin).
radius - The radius of the circle.
Returns:
A random point along the circumference of the given circle.
Since:
1.0
See Also:
getPointOnCircumference(org.lwjgl.util.vector.Vector2f, float, float)

getRandomPointInCircle

public static Vector2f getRandomPointInCircle(Vector2f center,
                                              float radius)
Returns a random point inside of a circle with uniform distribution.

Parameters:
center - The center point of the circle (can be null for a 0, 0 origin).
radius - The radius of the circle.
Returns:
A random point inside of the given circle.
Since:
1.0

getRandomPointOnLine

public static Vector2f getRandomPointOnLine(Vector2f lineStart,
                                            Vector2f lineEnd)
Returns a random point along the line between two Vector2fs.

Parameters:
lineStart - The starting point of the line.
lineEnd - The end point of the line.
Returns:
A random Vector2f along the line between lineStart and lineEnd.
Since:
1.6

getPointsAlongCircumference

public static List<Vector2f> getPointsAlongCircumference(Vector2f center,
                                                         float radius,
                                                         int numPoints,
                                                         float angleOffset)
Returns an evenly distributed List of points along a circle's circumference.

Parameters:
center - The center point of the circle (can be null for a 0, 0 origin).
radius - The radius of the circle.
numPoints - How many points to generate.
angleOffset - The offset angle of the initial point.
Returns:
A List of Vector2fs that are evenly distributed along the circle's circumference.
Since:
1.0

isPointWithinCircle

public static boolean isPointWithinCircle(Vector2f point,
                                          Vector2f center,
                                          float radius)
Returns whether a point is within the bounds of a circle or not.

Parameters:
point - The Vector2f to check.
center - The center point of the circle (can be null for a 0, 0 origin).
radius - The radius of the circle.
Returns:
true if point is within the circle, false otherwise.
Since:
1.0

isPointOnLine

public static boolean isPointOnLine(Vector2f point,
                                    Vector2f lineStart,
                                    Vector2f lineEnd)
Check if a point is along the line between two Vector2fs.

Parameters:
point - The point to check.
lineStart - The starting point of the line.
lineEnd - The end point of the line.
Returns:
true if the point is along the line, false otherwise.
Since:
1.6

getEquidistantPointsInsideCircle

public static List<Vector2f> getEquidistantPointsInsideCircle(Vector2f center,
                                                              float radius,
                                                              float spaceBetweenPoints)
Returns a List of evenly spaced Vector2fs inside a circle. WARNING: be VERY conservative using this method - a radius of 250 and a spacing of 5 will result in 10,000 circle checks and 7,825 Vector2fs created!

Parameters:
center - The center point of the circle (can be null for a 0, 0 origin).
radius - The radius of the circle.
spaceBetweenPoints - How much space should be between each point.
Returns:
A List of evenly spaced Vector2fs inside a circle.
Since:
1.4

getRandomNumberInRange

public static float getRandomNumberInRange(float min,
                                           float max)
Returns a random number within a given range.

Parameters:
min - The minimum value to select.
max - The maximum value to select.
Returns:
A random Float between min and max.
Since:
1.4

equals

public static boolean equals(float a,
                             float b)
Tests for near-equality of floating point numbers.

Parameters:
a - The first float to compare.
b - The second float to compare.
Returns:
true if a and b are within 99.99999% of each other, false otherwise.

getRandom

public static Random getRandom()
Returns the random number generator used by LazyLib's methods. Useful for seed manipulation or to avoid instantiating your own.

Returns:
The Random instance used by LazyLib.
Since:
1.5

isPointWithinBounds

@Deprecated
public static boolean isPointWithinBounds(Vector2f point,
                                                     CombatEntityAPI entity)
Deprecated. Use CollisionUtils.isPointWithinBounds(org.lwjgl.util.vector.Vector2f, com.fs.starfarer.api.combat.CombatEntityAPI) instead.

Since:
1.0

LazyLib