bobzhang/p5/math does not have a README file

    Matrix

    pub struct Matrix {
    dim : Int
    // private fields
    }

    A square matrix in column-major order (p5.Matrix).

    Matrix::apply

    fn Matrix::apply(self : Matrix, src : ArrayView[Double]) -> Matrix

    Applies src (16 entries) on the left, like p5's apply.

    Matrix::copy

    fn Matrix::copy(self : Matrix) -> Matrix

    Matrix::floats

    fn Matrix::floats(self : Matrix) -> FixedArray[Float]

    The entries as 32-bit floats (what is sent to shaders).

    Matrix::from_array

    fn Matrix::from_array(values : ArrayView[Double]) -> Matrix

    A matrix from column-major entries (16 for 4x4, 9 for 3x3).

    Matrix::get

    fn Matrix::get(self : Matrix, i : Int) -> Double

    Entry i (column-major).

    Matrix::inverse_transpose4x4

    fn Matrix::inverse_transpose4x4(self : Matrix, m : Matrix) -> Matrix

    For a 3x3 self: the inverse transpose of the upper-left 3x3 of the 4x4 m (the normal matrix), or zeros when singular.

    Matrix::invert_from

    fn Matrix::invert_from(self : Matrix, src : Matrix) -> Matrix?

    Inverts src into self; None (and self unchanged) when singular.

    Matrix::mult

    fn Matrix::mult(self : Matrix, other : Matrix) -> Matrix

    self = self × other in p5's convention (mult).

    Matrix::multiply_vec4

    fn Matrix::multiply_vec4(self : Matrix, x : Double, y : Double, z : Double, w : Double) -> (Double, Double, Double, Double)

    m × (x, y, z, w) as four doubles.

    Matrix::new

    fn Matrix::new(dim : Int) -> Matrix

    The identity matrix of side dim.

    Matrix::reset

    fn Matrix::reset(self : Matrix) -> Unit

    Resets to the identity.

    Matrix::rotate

    fn Matrix::rotate(self : Matrix, a : Double, x : Double, y : Double, z : Double) -> Matrix

    Rotates by a radians about the axis (x, y, z) (rotate4x4).

    Matrix::scale

    fn Matrix::scale(self : Matrix, x : Double, y : Double, z : Double) -> Matrix

    Matrix::set

    fn Matrix::set(self : Matrix, other : Matrix) -> Unit

    Copies the entries of other.

    Matrix::set_element

    fn Matrix::set_element(self : Matrix, i : Int, v : Double) -> Unit

    Matrix::set_values

    fn Matrix::set_values(self : Matrix, values : ArrayView[Double]) -> Unit

    Sets the entries from doubles (column-major).

    Matrix::to_array

    fn Matrix::to_array(self : Matrix) -> Array[Double]

    The entries as doubles.

    Matrix::translate

    fn Matrix::translate(self : Matrix, x : Double, y : Double, z : Double) -> Matrix

    Matrix::transpose_from

    fn Matrix::transpose_from(self : Matrix, src : Matrix) -> Matrix

    Transposes src (a 3x3 or 4x4 matrix's entries) into self.

    Matrix2D

    pub(all) struct Matrix2D {
    a : Double
    b : Double
    c : Double
    d : Double
    e : Double
    f : Double
    } derive(Eq,
    Debug
    )

    A 2D affine transformation, using the same layout as the canvas setTransform(a, b, c, d, e, f):

    | a c e | | b d f | | 0 0 1 |

    Matrix2D::apply

    fn Matrix2D::apply(self : Matrix2D, x : Double, y : Double) -> (Double, Double)

    Applies the transform to a point.

    Matrix2D::invert

    fn Matrix2D::invert(self : Matrix2D) -> Matrix2D?

    Inverse transform (None when singular).

    Matrix2D::multiply

    fn Matrix2D::multiply(self : Matrix2D, m : Matrix2D) -> Matrix2D

    Returns self * other (apply other first, then self).

    Matrix2D::rotate

    fn Matrix2D::rotate(self : Matrix2D, angle : Double) -> Matrix2D

    Like canvas rotate(angle) (radians, clockwise in screen space).

    Matrix2D::scale

    fn Matrix2D::scale(self : Matrix2D, x : Double, y : Double) -> Matrix2D

    Like canvas scale(x, y).

    Matrix2D::shear_x

    fn Matrix2D::shear_x(self : Matrix2D, angle : Double) -> Matrix2D

    Shear along the x axis by angle radians.

    Matrix2D::shear_y

    fn Matrix2D::shear_y(self : Matrix2D, angle : Double) -> Matrix2D

    Shear along the y axis by angle radians.

    Matrix2D::translate

    fn Matrix2D::translate(self : Matrix2D, x : Double, y : Double) -> Matrix2D

    Like canvas translate(x, y): post-multiplies a translation.

    Noise

    pub struct Noise {
    // private fields
    }

    Perlin noise generator compatible with p5's noise(), noiseDetail() and noiseSeed().

    Noise::amp_falloff

    fn Noise::amp_falloff(self : Noise) -> Double

    Amplitude falloff per octave.

    Noise::detail

    fn Noise::detail(self : Noise, lod : Int, falloff? : Double) -> Unit

    Adjusts the character of the noise, like p5's noiseDetail(lod, falloff).

    Noise::from_random

    fn Noise::from_random(rng : Random) -> Noise

    Creates a noise generator whose lattice is filled from rng.

    Noise::new

    fn Noise::new(seed : Double) -> Noise

    Creates a noise generator seeded like noiseSeed(seed).

    Noise::noise

    fn Noise::noise(self : Noise, x : Double, y? : Double, z? : Double) -> Double

    Returns Perlin noise in [0, 1) at the coordinates (x, y, z).

    test {
    let n = @math.Noise::new(42)
    let v = n.noise(0.5, y=0.25)
    assert_true(v >= 0 && v < 1)
    }

    Noise::octaves

    fn Noise::octaves(self : Noise) -> Int

    Number of octaves used.

    Noise::set_seed

    fn Noise::set_seed(self : Noise, seed : Double) -> Unit

    Re-seeds the lattice, like p5's noiseSeed(seed).

    Random

    pub struct Random {
    // private fields
    }

    A seedable pseudo random number generator, compatible with p5.js randomSeed() / random() / randomGaussian().

    Random::below

    fn Random::below(self : Random, max : Double) -> Double

    Returns a number in [0, max).

    Random::choice

    fn[T] Random::choice(self : Random, choices : ArrayView[T]) -> T

    Returns a uniformly chosen element of choices.

    Panics

    Panics if choices is empty.

    Random::gaussian

    fn Random::gaussian(self : Random, mean? : Double, sd? : Double) -> Double

    Returns a number from a normal distribution (Marsaglia polar method, exactly as p5's randomGaussian(mean, sd)).

    Random::new

    fn Random::new(seed : Double) -> Random

    Creates a generator with the given seed.

    Random::next

    fn Random::next(self : Random) -> Double

    Returns a number in [0, 1).

    test {
    let r = @math.Random::new(99)
    // Same as `randomSeed(99); random()` in p5.js
    inspect(
    r.next(),
    content=(
    #|0.27443566313013434
    ),
    )
    }

    Random::range

    fn Random::range(self : Random, min : Double, max : Double) -> Double

    Returns a number in [min, max). The bounds are swapped when min > max.

    Random::set_seed

    fn Random::set_seed(self : Random, seed : Double) -> Unit

    Re-seeds the generator, like p5's randomSeed(seed).

    Vector

    pub struct Vector {
    x : Double
    y : Double
    z : Double
    // private fields
    }

    A 2D or 3D Euclidean vector.

    Vectors created by a sketch (create_vector) follow the sketch's angle mode in heading, rotate and angle_between, like p5.js; other vectors always use radians.
    impl Add for Vector
    impl Eq for Vector
    impl Neg for Vector
    impl Show for Vector
    impl Sub for Vector

    Vector::Vector

    fn Vector::Vector(x : Double, y : Double, z? : Double) -> Vector

    Creates a new vector. z defaults to 0.

    test {
    let v = @math.Vector(3, 4)
    inspect(v.mag(), content="5")
    inspect(v, content="vector[3, 4, 0]")
    }

    Vector::add

    fn Vector::add(self : Vector, other : Vector) -> Unit

    Adds other to this vector (in place).

    Vector::add_xy

    fn Vector::add_xy(self : Vector, x : Double, y : Double, z? : Double) -> Unit

    Adds the given components to this vector (in place).

    Vector::angle_between

    fn Vector::angle_between(self : Vector, v : Vector) -> Double

    Signed angle between two vectors (radians, or the sketch's angle mode; NaN if one is zero).

    Vector::array

    fn Vector::array(self : Vector) -> Array[Double]

    Components as an array [x, y, z].

    Vector::clamp_to_zero

    fn Vector::clamp_to_zero(self : Vector) -> Unit

    Sets components very close to zero to exactly zero (in place).

    Vector::copy

    fn Vector::copy(self : Vector) -> Vector

    Returns a copy of the vector.

    Vector::cross

    fn Vector::cross(self : Vector, v : Vector) -> Vector

    Cross product, returned as a new vector.

    Vector::dist

    fn Vector::dist(self : Vector, other : Vector) -> Double

    Distance between two points represented as vectors.

    Vector::div

    fn Vector::div(self : Vector, n : Double) -> Unit

    Divides this vector by a scalar (in place). Dividing by 0 is ignored, as in p5.js.

    Vector::dot

    fn Vector::dot(self : Vector, other : Vector) -> Double

    Dot product.

    Vector::from_angle

    fn Vector::from_angle(angle : Double, length? : Double) -> Vector

    Creates a 2D unit vector (times length) pointing at angle radians.

    Vector::from_angles

    fn Vector::from_angles(theta : Double, phi : Double, length? : Double) -> Vector

    Creates a 3D vector from spherical angles theta and phi.

    Vector::heading

    fn Vector::heading(self : Vector) -> Double

    Angle of rotation of a 2D vector (radians, or the sketch's angle mode).

    Vector::lerp

    fn Vector::lerp(self : Vector, other : Vector, amt : Double) -> Unit

    Linearly interpolates this vector towards other (in place).

    Vector::limit

    fn Vector::limit(self : Vector, max : Double) -> Unit

    Limits the magnitude of this vector to max (in place).

    Vector::mag

    fn Vector::mag(self : Vector) -> Double

    Magnitude (length) of the vector.

    Vector::mag_sq

    fn Vector::mag_sq(self : Vector) -> Double

    Squared magnitude of the vector.

    Vector::mult

    fn Vector::mult(self : Vector, n : Double) -> Unit

    Multiplies this vector by a scalar (in place).

    Vector::mult_vec

    fn Vector::mult_vec(self : Vector, other : Vector) -> Unit

    Multiplies this vector component-wise by other (in place).

    Vector::normalize

    fn Vector::normalize(self : Vector) -> Unit

    Scales this vector to length 1 (in place). The zero vector is unchanged.

    Vector::normalized

    fn Vector::normalized(self : Vector) -> Vector

    Returns a normalized copy of the vector.

    Vector::random2d

    fn Vector::random2d(rng : Random) -> Vector

    A random 2D unit vector.

    Vector::random3d

    fn Vector::random3d(rng : Random) -> Vector

    A random 3D unit vector.

    Vector::reflect

    fn Vector::reflect(self : Vector, surface_normal : Vector) -> Unit

    Reflects this vector about a surface normal (in place).

    Vector::rem

    fn Vector::rem(self : Vector, n : Double) -> Unit

    Remainder of this vector divided by n (in place, JavaScript %).

    Vector::rotate

    fn Vector::rotate(self : Vector, a : Double) -> Unit

    Rotates a 2D vector by a (radians, or the sketch's angle mode).

    Vector::scaled

    fn Vector::scaled(self : Vector, n : Double) -> Vector

    Returns a new vector multiplied by n.

    Vector::set

    fn Vector::set(self : Vector, x : Double, y : Double, z? : Double) -> Unit

    Sets the components of the vector.

    Vector::set_heading

    fn Vector::set_heading(self : Vector, a : Double) -> Unit

    Rotates a 2D vector to the given heading (radians), keeping its magnitude.

    Vector::set_mag

    fn Vector::set_mag(self : Vector, n : Double) -> Unit

    Sets the magnitude of this vector (in place).

    Vector::set_vec

    fn Vector::set_vec(self : Vector, other : Vector) -> Unit

    Copies the components of other into self.

    Vector::slerp

    fn Vector::slerp(self : Vector, v : Vector, amt : Double) -> Unit

    Spherical linear interpolation towards v (in place).

    Vector::sub

    fn Vector::sub(self : Vector, other : Vector) -> Unit

    Subtracts other from this vector (in place).

    Vector::sub_xy

    fn Vector::sub_xy(self : Vector, x : Double, y : Double, z? : Double) -> Unit

    Subtracts the given components from this vector (in place).

    Vector::with_angle_mode

    fn Vector::with_angle_mode(x : Double, y : Double, z? : Double, degrees~ :
    Ref
    [Bool]) -> Vector

    A vector whose angles follow degrees (true: degrees, false: radians).

    DEG_TO_RAD

    let DEG_TO_RAD : Double

    Multiply an angle in degrees by this value to convert it to radians.

    HALF_PI

    let HALF_PI : Double

    Half of the mathematical constant π (≈ 1.5708).
    let PI : Double

    The mathematical constant π (≈ 3.14159).

    QUARTER_PI

    let QUARTER_PI : Double

    A quarter of π (≈ 0.7854).

    RAD_TO_DEG

    let RAD_TO_DEG : Double

    Multiply an angle in radians by this value to convert it to degrees.

    TAU

    let TAU : Double

    An alias for TWO_PI.

    TWO_PI

    let TWO_PI : Double

    Twice π (≈ 6.28319).

    constrain

    fn constrain(n : Double, low : Double, high : Double) -> Double

    Constrains n between low and high.

    test {
    inspect(@math.constrain(15, 0, 10), content="10")
    inspect(@math.constrain(-1, 0, 10), content="0")
    }

    degrees

    fn degrees(rad : Double) -> Double

    Converts an angle from radians to degrees.

    dist

    fn dist(x1 : Double, y1 : Double, x2 : Double, y2 : Double) -> Double

    Distance between two 2D points.

    dist3

    fn dist3(x1 : Double, y1 : Double, z1 : Double, x2 : Double, y2 : Double, z2 : Double) -> Double

    Distance between two 3D points.

    fract

    fn fract(n : Double) -> Double

    Fractional part of a number: fract(3.75) == 0.75, fract(-1.25) == 0.75.

    identity

    let identity : Matrix2D

    The identity transform.

    js_rem

    fn js_rem(a : Double, b : Double) -> Double

    JavaScript % on doubles (result has the sign of the dividend).

    js_round

    fn js_round(n : Double) -> Double

    JavaScript-compatible Math.round: rounds half-way cases towards +∞.

    lerp

    fn lerp(start : Double, stop : Double, amt : Double) -> Double

    Linear interpolation: amt * (stop - start) + start.

    mag

    fn mag(x : Double, y : Double) -> Double

    Length of the 2D vector (x, y).

    map

    fn map(n : Double, start1 : Double, stop1 : Double, start2 : Double, stop2 : Double, within_bounds? : Bool) -> Double

    Re-maps n from the range [start1, stop1] to [start2, stop2]. When within_bounds is true the result is constrained to the target range.

    test {
    inspect(@math.map(5, 0, 10, 0, 100), content="50")
    inspect(@math.map(20, 0, 10, 0, 100, within_bounds=true), content="100")
    }

    max_of

    fn max_of(values : ArrayView[Double]) -> Double

    Largest value of an array (-inf when empty).

    min_of

    fn min_of(values : ArrayView[Double]) -> Double

    Smallest value of an array (inf when empty).

    norm

    fn norm(n : Double, start : Double, stop : Double) -> Double

    Normalizes n from the range [start, stop] to [0, 1].

    radians

    fn radians(deg : Double) -> Double

    Converts an angle from degrees to radians.

    round

    fn round(n : Double, decimals? : Int) -> Double

    Rounds n to the given number of decimals (0 by default), with JavaScript Math.round semantics.

    test {
    inspect(@math.round(2.5), content="3")
    inspect(@math.round(-2.5), content="-2")
    inspect(@math.round(3.14159, decimals=2), content="3.14")
    }
    fn sq(n : Double) -> Double

    Squares a number.

    to_uint32

    fn to_uint32(value : Double) -> UInt

    JavaScript ToUint32 (the >>> 0 operator).