Skip to content

Commit

Permalink
feat: random walk #46
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Aug 31, 2022
1 parent 4bee1fe commit 56eebca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { radiansToDegrees } from "./radiansToDegrees.js";
import { randomColor, randomColor0X, randomRGBColor, randomRGBAColor } from "./randomColor.js";
import { randomNumber, randomInt, randomFloat } from "./randomNumber.js";
import { randomString } from "./randomString.js";
import { randomWalk1D, randomWalk2D, randomWalk3D } from "./randomWalk.js";
import { range } from "./range.js";
import { stringToScreamingSnakeCase, screamingSnakeCaseToNormal } from "./screamingSnakeCase.js";
import { stringToSnakeCase, snakeCaseToNormal } from "./snakeCase.js";
Expand Down Expand Up @@ -113,6 +114,7 @@ export {
randomColor, randomColor0X, randomRGBColor, randomRGBAColor,
randomNumber, randomInt, randomFloat,
randomString,
randomWalk1D, randomWalk2D, randomWalk3D,
range,
stringToScreamingSnakeCase, screamingSnakeCaseToNormal,
stringToSnakeCase, snakeCaseToNormal,
Expand Down
40 changes: 40 additions & 0 deletions src/randomWalk.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
/**
* Returns a random value -1 or 1
*
* @see {@link https://pt.wikipedia.org/wiki/Passeio_aleat%C3%B3rio}
*
* @returns {number[]}
*/
export function randomWalk1D() {
return [Math.random() < 0.5 ? -1 : 1];
}

/**
* Return a changed random axis (x or y)
*
* @see {@link https://pt.wikipedia.org/wiki/Passeio_aleat%C3%B3rio}
*
* @returns {number[]}
*/
export function randomWalk2D() {
if (Math.random() < 0.5) return [...randomWalk1D(), 0];
else return [0, ...randomWalk1D()];
}

/**
* Return a changed random axis (x, y or z)
*
* @see {@link https://pt.wikipedia.org/wiki/Passeio_aleat%C3%B3rio}
*
* @returns {number[]}
*/
export function randomWalk3D() {
const rdn = Math.random();
if (rdn < 0.333) return [...randomWalk1D(), 0, 0];
if (rdn < 0.666) return [0, ...randomWalk1D(), 0];
else return [0, 0, ...randomWalk1D()];
}


// TODO: Update this function
export function randomWalk(
numberOfAxes = 2,
trend = {
left: 0.25, right: 0.25,
up: 0.25, down: 0.25,
front: 0, backward: 0,
},
bounds = {
x: { min: -Infinity, max: Infinity, },
y: { min: -Infinity, max: Infinity, },
z: { min: -Infinity, max: Infinity, },
}) {
if (numberOfAxes = 1) {

}
}

0 comments on commit 56eebca

Please sign in to comment.