Variable math

math: {
    addVec: ((a, b) => Point);
    angleVec: ((a, b) => number);
    approxEqual: {
        (a, b, epsilon?): boolean;
        (a, b, epsilon?): boolean;
    };
    clamp: ((v, min?, max?) => number);
    dist: ((a, b) => number);
    evalBezier: ((p0, p1, p2, p3, t) => Point);
    invlerp: ((n1, n2, v) => number);
    lerp: ((n1, n2, p) => number);
    midpoint: ((a, b) => Point);
    min: {
        (arr): number;
        (arr, idx): number;
        (a, b, idx): number;
    };
    multScalar: ((v, s) => Point);
    remap: ((oFrom, oTo, tFrom, tTo, v) => number);
    rotateAboutPoint: ((p, angle, center) => Point);
    subVec: ((a, b) => Point);
    unitVec: ((a, b) => Point);
}

Type declaration

  • addVec: ((a, b) => Point)
      • (a, b): Point
      • Adds two points/vectors together

        Parameters

        Returns Point

        the component-wise sum of the two points

  • angleVec: ((a, b) => number)
      • (a, b): number
      • Computes the angle between two points

        Parameters

        Returns number

        the angle between the two points (in radians)

        Example

        const a: Point = [0, 0];
        const b: Point = [2, 2];

        angleVec(a, b); // > returns Math.PI / 4
  • approxEqual: {
        (a, b, epsilon?): boolean;
        (a, b, epsilon?): boolean;
    }
      • (a, b, epsilon?): boolean
      • Determines if the values in the two arrays are approximately equal (within epsilon). It evaulates the arrays index by index.

        Parameters

        • a: number[]

          the first array

        • b: number[]

          the second array

        • Optional epsilon: number

          the epsilon value to determine if the values are approximately equal (default 1e-6)

        Returns boolean

      • (a, b, epsilon?): boolean
      • Determines if two numbers are approximately equal (within epsilon)

        Parameters

        • a: number

          the first number

        • b: number

          the second number

        • Optional epsilon: number

          the epsilon value to determine if the values are approximately equal (default 1e-6)

        Returns boolean

  • clamp: ((v, min?, max?) => number)
      • (v, min?, max?): number
      • Parameters

        • v: number

          Constrain a number between a min and max value

        • min: number = 0

          the lower bound of the range to constrain

        • max: number = 1

          the upper bound of the range to constrain

        Returns number

  • dist: ((a, b) => number)
      • (a, b): number
      • Computes the distance between two points

        Parameters

        • a: [number, number]

          the first point

        • b: [number, number]

          the second point

        Returns number

        the distance between the two points

  • evalBezier: ((p0, p1, p2, p3, t) => Point)
      • (p0, p1, p2, p3, t): Point
      • Evaluate a bezier curve a given t value

        Parameters

        • p0: Point

          the start of the curve

        • p1: Point

          the first control point

        • p2: Point

          the second control point

        • p3: Point

          the end of the curve

        • t: number

          the value to evaluate the curve at

        Returns Point

        the point on the curve at t

  • invlerp: ((n1, n2, v) => number)
      • (n1, n2, v): number
      • invlerp (inverse linear interpolation) produces the percentage of v between n1 and n2.

        Parameters

        • n1: number

          the left side of the range

        • n2: number

          the right side of the range

        • v: number

          the value to find the percentage of

        Returns number

        the percentage of v between n1 and n2

        Example

        invlerp(0, 10, 5)  // > returns 0.5
        
  • lerp: ((n1, n2, p) => number)
      • (n1, n2, p): number
      • lerp (linear interpolation) produces a value beteween p% between n1 and n2.

        Parameters

        • n1: number

          the left side of the range

        • n2: number

          the right side of the range

        • p: number

          the percentage to interpolate

        Returns number

        the interpolated value between n1 and n2

        Example

        lerp(0, 10, 0.5)  // > returns 5
        
  • midpoint: ((a, b) => Point)
      • (a, b): Point
      • Computes the midpoint between two points

        Parameters

        Returns Point

        a point that is the midpoint between a and b

  • min: {
        (arr): number;
        (arr, idx): number;
        (a, b, idx): number;
    }
      • (arr): number
      • Find the minimum value in an array of numbers

        Parameters

        • arr: number[]

          the array of numbers to find the minimum within

        Returns number

        the minimum value in the array

      • (arr, idx): number
      • Find the minimum value in 2D array of numbers in a specific column

        Parameters

        • arr: number[][]

          the 2D array of numbers to find the minimum within

        • idx: number

          the index of the column to find the minimum within

        Returns number

        the minimum value in the column of the array

      • (a, b, idx): number
      • Given two arrays, find the minimum value at the same index in both arrays

        Parameters

        • a: number[]

          the first array

        • b: number[]

          the second array

        • idx: number

          the minimum value of either a[idx] or b[idx]

        Returns number

  • multScalar: ((v, s) => Point)
      • (v, s): Point
      • Scales a point/vector by a specified amount

        Parameters

        • v: Point

          the vector to multiply

        • s: number

          the scalar to multiply the vector by

        Returns Point

        the scaled vector

  • remap: ((oFrom, oTo, tFrom, tTo, v) => number)
      • (oFrom, oTo, tFrom, tTo, v): number
      • Converts a value from one range to another.

        Parameters

        • oFrom: number

          the left side of the original range

        • oTo: number

          the right side of the original range

        • tFrom: number

          the left side of the target range

        • tTo: number

          the right side of the target range

        • v: number

          the value to convert

        Returns number

        the value converted from the original range to the target range

  • rotateAboutPoint: ((p, angle, center) => Point)
      • (p, angle, center): Point
      • Rotates a point, p, about a center point, center, by angle in radians.

        Parameters

        • p: Point

          the point to rotate

        • angle: number

          the angle to rotate the point by (in radians)

        • center: Point

          the center point to rotate the point about

        Returns Point

        the rotated point

  • subVec: ((a, b) => Point)
      • (a, b): Point
      • Subtracts two points/vectors (i.e., a - b)

        Parameters

        Returns Point

        the component-wise difference of the two points

  • unitVec: ((a, b) => Point)
      • (a, b): Point
      • Computes the unit vector between two points

        Parameters

        Returns Point

        a point representing the standard unit vector between the two points (i.e., starting at the origin)