Skip to content

Instantly share code, notes, and snippets.

@rayansostenes
Created June 1, 2018 22:47
Show Gist options
  • Save rayansostenes/775e6e46620f0052181719143c91acfa to your computer and use it in GitHub Desktop.
Save rayansostenes/775e6e46620f0052181719143c91acfa to your computer and use it in GitHub Desktop.
Random Location Inside Circle
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