gpt4 book ai didi

android - 确定点是否在多边形中

转载 作者:IT老高 更新时间:2023-10-28 23:18:05 25 4
gpt4 key购买 nike

根据我的要求,我正在谷歌地图上绘制多边形,如下图所示。(使用 map v2)enter image description here

现在我需要在用户输入特定多边形时显示警报。

如何确定我当前的位置是否在多边形中。(需要优化方式而不耗尽电池)

提前致谢。

最佳答案

刚刚尝试了识别多边形中的点的光线转换算法。这很完美。

请参阅 http://en.wikipedia.org/wiki/Point_in_polygon用于光线转换的论文

private boolean isPointInPolygon(LatLng tap, ArrayList<LatLng> vertices) {
int intersectCount = 0;
for (int j = 0; j < vertices.size() - 1; j++) {
if (rayCastIntersect(tap, vertices.get(j), vertices.get(j + 1))) {
intersectCount++;
}
}

return ((intersectCount % 2) == 1); // odd = inside, even = outside;
}

private boolean rayCastIntersect(LatLng tap, LatLng vertA, LatLng vertB) {

double aY = vertA.latitude;
double bY = vertB.latitude;
double aX = vertA.longitude;
double bX = vertB.longitude;
double pY = tap.latitude;
double pX = tap.longitude;

if ((aY > pY && bY > pY) || (aY < pY && bY < pY)
|| (aX < pX && bX < pX)) {
return false; // a and b can't both be above or below pt.y, and a or
// b must be east of pt.x
}

double m = (aY - bY) / (aX - bX); // Rise over run
double bee = (-aX) * m + aY; // y = mx + b
double x = (pY - bee) / m; // algebra is neat!

return x > pX;
}

关于android - 确定点是否在多边形中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26014312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com