如何在谷歌地图中定义多边形的自相交(flutter)



我正在制作一个应用程序,用户可以在其中通过点在地图上绘制多边形。我需要以某种方式确保多边形没有自相交。我知道可以手动检查每一行。对此有多种方法。但我注意到谷歌地图自动填充没有自相交的多边形。有可能以某种方式从插件中获得这个值吗?

我正在使用google_maps_flutter

无自交叉

具有自交叉

也许这对某人有用。我在问题中列出的插件中没有找到内置函数。所以我写了我自己的函数:

真的-存在自交叉

bool isNotSimplePolygon(List<LatLng> polygon){
if(polygon.length <= 3)
return false;
for(int i = 0; i < polygon.length - 2; i++){
double x1 = polygon[i].latitude;
double y1 = polygon[i].longitude;
double x2 = polygon[i + 1].latitude;
double y2 = polygon[i + 1].longitude;
double maxx1 = max(x1, x2), maxy1 = max(y1, y2);
double minx1 = min(x1, x2), miny1 = min(y1, y2);
for (int j = i + 2; j < polygon.length; j++) {
double x21 = polygon[j].latitude;
double y21 = polygon[j].longitude;
double x22 = polygon[(j + 1) == polygon.length ? 0 : (j + 1)].latitude;
double y22 = polygon[(j + 1) == polygon.length ? 0 : (j + 1)].longitude;
double maxx2 = max(x21, x22), maxy2 = max(y21, y22);
double minx2 = min(x21, x22), miny2 = min(y21, y22);
if ((x1 == x21 && y1 == y21) || (x2 == x22 && y2 == y22) || (x1 == x22 && y1 == y22) || (x2 == x21 && y2 == y21))
continue;
if (minx1 > maxx2 || maxx1 < minx2 || miny1 > maxy2 || maxy1 < miny2)
continue;  // The moment when the lines have one common vertex...

double dx1 = x2-x1, dy1 = y2-y1; // The length of the projections of the first line on the x and y axes
double dx2 = x22-x21, dy2 = y22-y21; // The length of the projections of the second line on the x and y axes
double dxx = x1-x21, dyy = y1-y21;
double div = dy2 * dx1 - dx2 * dy1;
double mul1 = dx1 * dyy - dy1 * dxx;
double mul2 = dx2 * dyy - dy2 * dxx;
if (div == 0)
continue; // Lines are parallel...
if (div > 0) {
if (mul1 < 0 || mul1 > div)
continue; // The first segment intersects beyond its boundaries...
if (mul2 < 0 || mul2 > div)
continue; // // The second segment intersects beyond its borders...
}
else{
if (-mul1 < 0 || -mul1 > -div)
continue; // The first segment intersects beyond its boundaries...
if (-mul2 < 0 || -mul2 > -div)
continue; // The second segment intersects beyond its borders...
}
return true;
}
}
return false;
}

最新更新