Skip to content

Instantly share code, notes, and snippets.

@patrickhammond
Last active December 16, 2019 20:44
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 patrickhammond/e0401badf2a550cf0353258e647480fb to your computer and use it in GitHub Desktop.
Save patrickhammond/e0401badf2a550cf0353258e647480fb to your computer and use it in GitHub Desktop.
polygon generator
import 'dart:math';
// -----------------------------------
// -----------------------------------
// These are the values that you will want to adjust before hitting run.
// Take note that this is in a lat/long order (what you would normally get from Google Maps, etc)
const centerPoint = [39.1094384, -84.5094552];
// 1 mile
const radiusInMeters = 1609.34;
// a hexagon has 6 sidees
const serviceAreaSides = 6;
// If you wanted a flat top, use "360 / serviceAreaSides" as the value
const serviceAreaRotationDeg = 360 / serviceAreaSides;
// -----------------------------------
// -----------------------------------
// Don't try to land a rocket based on this math (favors simplicity over accuracy)
// https://stackoverflow.com/a/50506609
const earthRadiusInMeters = 6378137.0;
const degreesPerRadians = pi / 180.0;
const radiansPerDegress = 180 / pi;
const metersPerDegree = (1 / ((2 * pi / 360) * earthRadiusInMeters));
void main() {
var centerLat = centerPoint[0];
var centerLong = centerPoint[1];
var points = [];
for (var i = 0; i < serviceAreaSides; i++) {
var pointOffsetDegrees =
serviceAreaRotationDeg + ((360 / serviceAreaSides) * i);
var dNorthMeters =
radiusInMeters * sin(pointOffsetDegrees * degreesPerRadians);
var dEastMeters =
radiusInMeters * cos(pointOffsetDegrees * degreesPerRadians);
var adjustedLat = centerLat + (dNorthMeters * metersPerDegree);
var adjustedLong = centerLong +
(dEastMeters * metersPerDegree) / cos(centerLat * (pi / 180));
points.add([adjustedLat, adjustedLong]);
}
points.add(points[0]);
for (var i = 0; i < points.length; i++) {
var trailing = ((i + 1) == points.length) ? '' : ',';
print('[${points[i][1]}, ${points[i][0]}]${trailing}'); // long,lat...argh
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment