Skip to content

Instantly share code, notes, and snippets.

@clecidor
Last active September 13, 2019 07:32
Show Gist options
  • Save clecidor/808266b31cfe6223822a361d4109e1bd to your computer and use it in GitHub Desktop.
Save clecidor/808266b31cfe6223822a361d4109e1bd to your computer and use it in GitHub Desktop.
oscillate.js
function oscillate(intervals, threshhold) {
intervals = intervals || (-1*intervals) || 1;
threshhold = threshhold || (-1*threshhold) || 1;
const distance = threshhold * 2;
const minute = 60;
const rateOfChange = (distance / minute);
console.log({ intervals, threshhold, distance, rateOfChange });
const UP = 1;
const DOWN = -1;
let trajectory = UP;
let value = 0;
let graph = [[0, value]]; // start at (0, 0) for 0th interval
let interation = 0;
while(++interation <= intervals) {
if (trajectory === UP) {
value += rateOfChange;
trajectory = value >= threshhold ? DOWN : UP;
}
else { // going DOWN
value -= rateOfChange;
trajectory = value <= (-1*threshhold) ? UP : DOWN;
}
graph.push([interation, value]);
}
return graph;
}
const LOVE = oscillate(143, 2);
const GOD = oscillate(777, 3);
console.log({ LOVE, GOD })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment