Skip to content

Instantly share code, notes, and snippets.

@wbills
Last active July 26, 2016 05:10
Show Gist options
  • Save wbills/babaa2c0c81ebe3c8e4936d2751bdf55 to your computer and use it in GitHub Desktop.
Save wbills/babaa2c0c81ebe3c8e4936d2751bdf55 to your computer and use it in GitHub Desktop.
hexagonal circle packing
//wrote this for more efficient scanning of nearby pokemon,
//gets centroids for 7 minimally overlapping circles in a hexagon
//shape around an origin point
//to get a bigger area, just recurse this and de-dupe returned centroids
//for each pass
//this was in java for android, but python code is nearly identical
public static List<LatLng> pointsToScan(LatLng origin) {
double origin_lat = origin.latitude;
double origin_lng = origin.longitude;
List<LatLng> points = new ArrayList<>();
points.add(origin);
double scanRadius = 0.1; //100m
float R = 6379.1f;
int[] nearby_dirs = {0, 60, 120, 180, 240, 300};
float d = (float) (Math.sqrt(3) * scanRadius); //dist to center of next circle
for (int direction: nearby_dirs) {
double bearing = Math.toRadians(direction);
double lat1 = Math.toRadians(origin_lat);
double lng1 = Math.toRadians(origin_lng);
double lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) +
Math.cos(lat1)*Math.sin(d/R)*Math.cos(bearing));
double lng2 = lng1 + Math.atan2(Math.sin(bearing)*Math.sin(d/R)*Math.cos(lat1),
Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));
lat2 = Math.toDegrees(lat2);
lng2 = Math.toDegrees(lng2);
points.add(new LatLng(lat2, lng2));
}
return points;
}
@wbills
Copy link
Author

wbills commented Jul 26, 2016

Example of circles generated from these points:
screenshot_20160725-235726

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