Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rlemon
Created April 15, 2014 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlemon/10783047 to your computer and use it in GitHub Desktop.
Save rlemon/10783047 to your computer and use it in GitHub Desktop.
midpoint 2d displacement in js
function midpoint(arr, p1, p2, max, jitter) {
var mid = Math.round((p1 + p2) / 2);
if (p2 - p1 <= 1 || p2 === mid || p1 === mid) {
return;
}
arr[mid] = ((arr[p1] + arr[p2]) / 2) + (max * (Math.random() * (jitter * 2) - jitter));
midpoint(arr, p1, mid, max / 2, jitter);
midpoint(arr, mid, p2, max / 2, jitter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment