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