Skip to content

Instantly share code, notes, and snippets.

@netgfx
Created June 10, 2016 08:31
Show Gist options
  • Select an option

  • Save netgfx/0f25a166fa0354049cd02c8d6c768e50 to your computer and use it in GitHub Desktop.

Select an option

Save netgfx/0f25a166fa0354049cd02c8d6c768e50 to your computer and use it in GitHub Desktop.
random point between two points
var rand = Math.random;
var sin = Math.sin;
var cos = Math.cos;
var PI = Math.PI;
var TWO_PI = PI * 2;
function randomPoint(min, max) {
//generate a random distance between min and max
var dist = rand() * (max - min) + min;
//generate a random angle between 0 and PI * 2
var rads = rand() * TWO_PI;
// calculate the point on a cirlce with:
// radius: dist
// angle: rads
var x = sin(rads) * dist;
var y = cos(rads) * dist;
// return a simple point object
return {x: x, y: y};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment