Skip to content

Instantly share code, notes, and snippets.

@jhsu
Last active August 3, 2016 09:15
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 jhsu/ff8bd4b3373b2fd27a158c02ac4ca949 to your computer and use it in GitHub Desktop.
Save jhsu/ff8bd4b3373b2fd27a158c02ac4ca949 to your computer and use it in GitHub Desktop.
function toRad(deg) {
return deg * (Math.PI / 180);
}
function toDeg(rad) {
return rad / (Math.PI / 180);
}
const EARTH_R = 6378.1;
function getNewCoords(initLoc, distance, bearing) {
const bearingR = toRad(bearing);
const initCoords = {lat: toRad(initLoc.lat), lng: toRad(initLoc.lng)};
const newLat = Math.asin(Math.sin(initCoords.lat) * Math.cos(distance/EARTH_R) +
Math.cos(initCoords.lat) * Math.sin(distance/EARTH_R) * Math.cos(bearingR));
const newLng = initCoords.lng + Math.atan2(Math.sin(bearingR) * Math.sin(distance/EARTH_R) *
Math.cos(initCoords.lat), Math.cos(distance/EARTH_R) - Math.sin(initCoords.lat) * Math.sin(newLat));
return {lat: toDeg(newLat), lng: toDeg(newLng)};
}
function generateLocationSteps(initialLocation, stepCount=12) {
const NORTH = 0,
EAST = 90,
SOUTH = 180,
WEST = 270;
const pulseRadius = 0.07; // km - radius of players heartbeat is 70m
const xdist = Math.sqrt(3)*pulseRadius; // dist between column centers
const ydist = 3*(pulseRadius/2); // dist between row centers
let locations = [initialLocation];
let ring = 1;
let loc = initialLocation;
while (ring < stepCount) {
loc = getNewCoords(loc, ydist, NORTH);
loc = getNewCoords(loc, xdist/2, WEST);
for (let direction=0; direction < 6; direction++) {
for (let i=0; i < ring ; i++) {
if (direction === 0) { // RIGHT
loc = getNewCoords(loc, xdist, EAST)
}
if (direction === 1) { // DOWN + RIGHT
loc = getNewCoords(loc, ydist, SOUTH)
loc = getNewCoords(loc, xdist/2, EAST)
}
if (direction === 2) { // DOWN + LEFT
loc = getNewCoords(loc, ydist, SOUTH)
loc = getNewCoords(loc, xdist/2, WEST)
}
if (direction === 3) { // LEFT
loc = getNewCoords(loc, xdist, WEST)
}
if (direction === 4) { // UP + LEFT
loc = getNewCoords(loc, ydist, NORTH)
loc = getNewCoords(loc, xdist/2, WEST)
}
if (direction === 5) { // UP + RIGHT
loc = getNewCoords(loc, ydist, NORTH)
loc = getNewCoords(loc, xdist/2, EAST)
}
locations.push(loc);
}
}
ring = ring + 1;
}
return locations;
}
@jhsu
Copy link
Author

jhsu commented Aug 3, 2016

example usage:

const coords = generateLocationSteps({lat: 1234, lng: 1234});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment