Skip to main content

MathUtils

Clamp

Clamp(x, min, max) => number

Clamps a number between a given min and max.

Parameters

  •  x: number – The number to clamp.

  •  min: number – The minimum.

  •  max: number – The maximum.

Returns

  •   number – The clamped number.

GetNearestPointOnRect

GetNearestPointOnRect(bl, tr, pos) => Vector

Returns the nearest point on the edge of the given rectangle.

Parameters

  •  bl: Vector – Bottom left corner of the rectangle.

  •  tr: Vector – Top right corner of the rectangle.

  •  pos: Vector – The position to get the nearest point on.

Returns

  •   Vector – The closest position on the edge of the rectangle from 'pos'.

RectContains

RectContains(transform, pos) => false | true

Determines whether the given point is within the rectangle defined by the given transform.

Parameters

  •  transform: Transform – The transform that represents the rectangle.

  •  pos: Vector – Must be in world coordinates * The point to determine whether or not it's within the rectangle.

Returns

  •   false | true – True if the point is within the rectangle, false otherwise.

CircleContains

CircleContains(pos1, r, pos2) => false | true

Determines whether the given point is within the circle defined by the given transform.

Parameters

  •  pos1: Vector – The center of the circle in world coordinates.

  •  r: number – The radius of the circle in world units.

  •  pos2: Vector – Must be in world coordinates * The point to determine whether or not it's within the circle.

Returns

  •   false | true – True if the point is within the rectangle, false otherwise.

TransformContains

TransformContains(A, B) => false | true

Compares two transforms to see if they overlap. First tests it using a quick circle-circle intersection using the 'radius' of the transform.

Then uses a SAT (Separating Axis Theorem) method to determine whether or not the two transforms are intersecting.

Parameters

Returns

  •   false | true – True if the two transforms are overlapping, false otherwise.

FindRoots

FindRoots(iterations, t0, x, y, f, df) => number

Uses Newton's method to find the roots of the function 'f' given a derivative 'df'.

Parameters

  •  iterations: number – The number of iterations to perform Newton's method with; the smaller the better but less accurate.

  •  t0: number – The starting root value parameter.

  •  x: number – Parameter 1 for the function.

  •  y: number – Parameter 2 for the function.

  •  f: (t: number, x: number, y: number) => number – The function to find the roots of. In the form f(t, x, y) = ....

  •  df: (t: number, x: number, y: number) => number – The derivative of the function In the form of df(t, x, y).

Returns

  •   number – The parameter 't' that results in f(t, x, y) = 0.

BezierContains

BezierContains(curve, pos) => false | true

Finds if the given position is within the given bezier curve.

Parametric function defined by X(t) = t(p2.x - p1.x) + p1.x and Y(t) = t(p2.y - p1.y) + p1.y.

Solves for 't' from root of the derivative of the distance function between the line and pos. D(t) = sqrt((X(t) - mx)^2 + (Y(t) - my)^2).

Parameters

Returns

  •   false | true – True if position is within the bezier curve, false otherwise.

CalculateMidpoint

CalculateMidpoint(positions) => Vector

Finds the midpoint from a list of positions.

Parameters

  •  positions: Vector[] – The list of positions.

Returns

  •   Vector – The midpoint of all the given positions.

BCDtoDecimal

BCDtoDecimal(bcd) => number

Calculates the decimal value of a binary-coded-decimal represented by a list of booleans.

Parameters

  •  bcd: (false | true)[] – The binary-coded-decimal as a list of booleans.

Returns

  •   number – The decimal equivalent of the binary-coded-decimal.

DecimalToBCD

DecimalToBCD(decimal) => (false | true)[]

Calculates the BCD representation of the input number.

Parameters

  •  decimal: number – The number to convert (decimal >= 0).

Returns

  •   (false | true)[] – The BCD representation of the input.

linspace

linspace(x0, xf, n) => number[]

Creates a "linear space" or uniform/collocated grid from [x0, xf] with n points uniformly between them.

Parameters

  •  x0: number – Start point (inclusive).

  •  xf: number – End point (inclusive).

  •  n: number – The number of points in the space.

Returns

  •   number[] – An array of n uniform points on the domain [x0, xf].

linspaceDX

linspaceDX(x0, xf, dx) => number[]

Creates a "linear space" or uniform/staggered grid from [x0, xf) with spacing dx.

Parameters

  •  x0: number – Start point (inclusive).

  •  xf: number – End point (exclusive).

  •  dx: number – The spacing between each point.

Returns

  •   number[] – An array of n uniform points on the domain [x0, xf).