Fork me on GitHub
View source

package haxepunk.math

class MathUtil

This class is for math utility functions previously stored in HXP.


Available since:

4.0.0

Static variables

static read only DEG : Float

View source

static read only EPSILON : Float

View source

static read only INT_MAX_VALUE : Int

View source

static read only INT_MIN_VALUE : Int

static read only NUMBER_MAX_VALUE : Float

View source

static read only PI : Float

static read only RAD : Float

Static functions

View source

static abs (f : Float) : Float

View source

static acos (theta : Float) : Float

View source

static anchorTo (object : Position, anchor : Position, distance : Float = 0) : Void

Anchors the object to a position.


Parameters:
object   

The object to anchor.

anchor   

The anchor object.

distance   

The max distance object can be anchored to the anchor.

View source

static angle (x1 : Float, y1 : Float, x2 : Float, y2 : Float) : Float

Finds the angle (in degrees) from point 1 to point 2.


Parameters:
x1   

The first x-position.

y1   

The first y-position.

x2   

The second x-position.

y2   

The second y-position.


Returns:

The angle from (x1, y1) to (x2, y2).

View source

static angleDifference (angle1 : Float, angle2 : Float) : Float

Get difference between two angles. Result will be between -180 and 180.


Parameters:
angle1   

First angle, in degrees.

angle2   

Second angle, in degrees.


Returns:

The angle difference, in degrees.

View source

static angleXY (object : Position, angle : Float, length : Float = 1, x : Float = 0, y : Float = 0) : Void

Sets the x/y values of the provided object to a vector of the specified angle and length.


Parameters:
object   

The object whose x/y properties should be set.

angle   

The angle of the vector, in degrees.

length   

The distance to the vector from (0, 0).

x   

X offset.

y   

Y offset.

View source

static approach (value : Float, target : Float, amount : Float) : Float

Approaches the value towards the target, by the specified amount, without overshooting the target.


Parameters:
value   

The starting value.

target   

The target that you want value to approach.

amount   

How much you want the value to approach target by.


Returns:

The new value.

View source

static asin (theta : Float) : Float

View source

static atan (theta : Float) : Float

View source

static atan2 (dy : Float, dx : Float) : Float

View source

static ceil (f : Float) : Int

View source

static clamp (value : Float, min : Float, max : Float) : Float

Clamps the value within the minimum and maximum values.


Parameters:
value   

The Float to evaluate.

min   

The minimum range.

max   

The maximum range.


Returns:

The clamped value.

View source

static clampInRect (object : Position, x : Float, y : Float, width : Float, height : Float, padding : Float = 0) : Void

Clamps the object inside the rectangle.


Parameters:
object   

The object to clamp (must have an x and y property).

x   

Rectangle's x.

y   

Rectangle's y.

width   

Rectangle's width.

height   

Rectangle's height.

padding   

Rectangle's padding.

View source

static cos (theta : Float) : Float

View source

static distance (x1 : Float, y1 : Float, x2 : Float = 0, y2 : Float = 0) : Float

Find the distance between two points.


Parameters:
x1   

The first x-position.

y1   

The first y-position.

x2   

The second x-position.

y2   

The second y-position.


Returns:

The distance.

View source

static distanceRectPoint (px : Float, py : Float, rx : Float, ry : Float, rw : Float, rh : Float) : Float

Find the distance between a point and a rectangle. Returns 0 if the point is within the rectangle.


Parameters:
px   

The x-position of the point.

py   

The y-position of the point.

rx   

The x-position of the rect.

ry   

The y-position of the rect.

rw   

The width of the rect.

rh   

The height of the rect.


Returns:

The distance.

View source

static distanceRects (x1 : Float, y1 : Float, w1 : Float, h1 : Float, x2 : Float, y2 : Float, w2 : Float, h2 : Float) : Float

Find the distance between two rectangles. Will return 0 if the rectangles overlap.


Parameters:
x1   

The x-position of the first rect.

y1   

The y-position of the first rect.

w1   

The width of the first rect.

h1   

The height of the first rect.

x2   

The x-position of the second rect.

y2   

The y-position of the second rect.

w2   

The width of the second rect.

h2   

The height of the second rect.


Returns:

The distance.

View source

static distanceSquared (x1 : Float, y1 : Float, x2 : Float = 0, y2 : Float = 0) : Float

Find the squared distance between two points.


Parameters:
x1   

The first x-position.

y1   

The first y-position.

x2   

The second x-position.

y2   

The second y-position.


Returns:

The squared distance.

View source

static floor (f : Float) : Int

View source

static get_DEG () : Float

View source

static get_NUMBER_MAX_VALUE () : Float

View source

static get_RAD () : Float

View source

static iabs (i : Int) : Int

View source

static iclamp (value : Int, min : Int, max : Int) : Int

View source

static ilerp (a : Int, b : Int, t : Float = 1) : Int

Linear interpolation between two values. Result rounded to an integer.


Parameters:
a   

First value.

b   

Second value.

t   

Interpolation factor.


Returns:

When t=0, returns a. When t=1, returns b. When t=0.5, will return halfway between a and b. Etc.

View source

static imax (a : Int, b : Int) : Int

View source

static imin (a : Int, b : Int) : Int

View source

static iround (f : Float) : Int

View source

static lerp (a : Float, b : Float, t : Float = 1) : Float

Linear interpolation between two values.


Parameters:
a   

First value.

b   

Second value.

t   

Interpolation factor.


Returns:

When t=0, returns a. When t=1, returns b. When t=0.5, will return halfway between a and b. Etc.

View source

static log (v : Float) : Float

View source

static max (a : Float, b : Float) : Float

View source

static maxOf3 (a : Float, b : Float, c : Float) : Float

View source

static min (a : Float, b : Float) : Float

View source

static minOf3 (a : Float, b : Float, c : Float) : Float

View source

static pow (v : Float, p : Float) : Float

View source

static random () : Float

View source

static rotateAround (object : Position, anchor : Position, angle : Float = 0, relative : Bool = true) : Void

Rotates the object around the anchor by the specified amount.


Parameters:
object   

Object to rotate around the anchor.

anchor   

Anchor to rotate around.

angle   

The amount of degrees to rotate by.

relative   

If the angle is relative to the angle between the object and the anchor.

View source

static round (f : Float) : Int

View source

static roundDecimal (f : Float, places : Int) : Float

View source

static roundTo (num : Float, precision : Int = 0) : Float

Round a float to the nearest decimal


Parameters:
num   

The number to round,

precision   

The decimal place to round to.


Returns:

The rounded float.

View source

static scale (value : Float, min : Float, max : Float, min2 : Float, max2 : Float) : Float

Transfers a value from one scale to another scale. For example, scale(.5, 0, 1, 10, 20) == 15, and scale(3, 0, 5, 100, 0) == 40.


Parameters:
value   

The value on the first scale.

min   

The minimum range of the first scale.

max   

The maximum range of the first scale.

min2   

The minimum range of the second scale.

max2   

The maximum range of the second scale.


Returns:

The scaled value.

View source

static scaleClamp (value : Float, min : Float, max : Float, min2 : Float, max2 : Float) : Float

Transfers a value from one scale to another scale, but clamps the return value within the second scale.


Parameters:
value   

The value on the first scale.

min   

The minimum range of the first scale.

max   

The maximum range of the first scale.

min2   

The minimum range of the second scale.

max2   

The maximum range of the second scale.


Returns:

The scaled and clamped value.

View source

static sign (value : Float) : Int

Finds the sign of the provided value.


Parameters:
value   

The Float to evaluate.


Returns:

1 if value > 0, -1 if value < 0, and 0 when value == 0.

View source

static sin (theta : Float) : Float

View source

static sqrt (f : Float) : Float

View source

static stepTowards (object : Position, x : Float, y : Float, distance : Float = 1) : Void

Steps the object towards a point.


Parameters:
object   

Object to move (must have an x and y property).

x   

X position to step towards.

y   

Y position to step towards.

distance   

The distance to step (will not overshoot target).

View source

static tan (theta : Float) : Float