Skip to content

Instantly share code, notes, and snippets.

@elmissouri16
Last active May 9, 2021 22:20
Show Gist options
  • Select an option

  • Save elmissouri16/716d337e91f59a70217c22c55f73e6bb to your computer and use it in GitHub Desktop.

Select an option

Save elmissouri16/716d337e91f59a70217c22c55f73e6bb to your computer and use it in GitHub Desktop.
function to check if cordinates inside polygone for dart and flutter framework
import 'dart:math';
// function to check if cordinates inside polygone
bool polygoneContains(List<LatLng> points, num lat, num lng) {
List<Point<num>> _points =
points.map((e) => Point<num>(e.latitude, e.longitude)).toList();
num ax = 0;
num ay = 0;
num bx = _points[points.length - 1].x - lat;
num by = _points[points.length - 1].y - lng;
int depth = 0;
for (int i = 0; i < _points.length; i++) {
ax = bx;
ay = by;
bx = _points[i].x - lat;
by = _points[i].y - lng;
if (ay < 0 && by < 0) continue; // both "up" or both "down"
if (ay > 0 && by > 0) continue; // both "up" or both "down"
if (ax < 0 && bx < 0) continue; // both points on left
num lx = ax - ay * (bx - ax) / (by - ay);
if (lx == 0) return true; // point on edge
if (lx > 0) depth++;
}
return (depth & 1) == 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment