Created
June 10, 2016 08:31
-
-
Save netgfx/0f25a166fa0354049cd02c8d6c768e50 to your computer and use it in GitHub Desktop.
random point between two points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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