UNPKG

1.29 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * Oscillate between -1 and 1 given a value and a period. This is basically a simplification on using Math.sin().
5 * @alias Splat.math.oscillate
6 * @param {number} current The current value of the number you want to oscillate.
7 * @param {number} period The period, or how often the number oscillates. The return value will oscillate between -1 and 1, depending on how close current is to a multiple of period.
8 * @returns {number} A number between -1 and 1.
9 * @example
10Splat.math.oscillate(0, 100); // returns 0
11Splat.math.oscillate(100, 100); // returns 0-ish
12Splat.math.oscillate(50, 100); // returns 1
13Splat.math.oscillate(150, 100); // returns -1
14Splat.math.oscillate(200, 100); // returns 0-ish
15 */
16function oscillate(current, period) {
17 return Math.sin(current / period * Math.PI);
18}
19
20/**
21 * @namespace Splat.math
22 */
23module.exports = {
24 oscillate: oscillate,
25 /**
26 * A seedable pseudo-random number generator. Currently a Mersenne Twister PRNG.
27 * @constructor
28 * @alias Splat.math.Random
29 * @param {number} [seed] The seed for the PRNG.
30 * @see [mersenne-twister package at github]{@link https://github.com/boo1ean/mersenne-twister}
31 * @example
32var rand = new Splat.math.Random(123);
33var val = rand.random();
34 */
35 Random: require("mersenne-twister")
36};