Created
June 1, 2018 22:47
-
-
Save rayansostenes/775e6e46620f0052181719143c91acfa to your computer and use it in GitHub Desktop.
Random Location Inside Circle
This file contains 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
const RADIUS_EARTH = 6378000; | |
const randomInsideCircle = (center, radius) => { | |
const { lat, lng } = center; | |
const [ a, b ] = [ Math.random(), Math.random() ].sort(); | |
const dx = b * radius * Math.cos((2 * Math.PI * a) / b); | |
const dy = b * radius * Math.sin((2 * Math.PI * a) / b); | |
const new_lat = lat + (dy / RADIUS_EARTH) * (180 / Math.PI); | |
const new_lng = lng + ((dx / RADIUS_EARTH) * (180 / Math.PI)) / Math.cos((lat * Math.PI) / 180); | |
return { lat: new_lat, lng: new_lng }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment