の一部がPolygon
画面に表示されているかどうかを知る必要があります。ArrayList
の2 つがありますLatLng
。1 つは を形成するポイントのリストを含みPolygon
、2 つ目は画面の 4 つのコーナーを含みます。
これは私のコードです:
protected boolean doPolygonsHaveAnyCoincidingArea(ArrayList<LatLng> polygon1, final ArrayList<LatLng> polygon2) {
for (LatLng point : polygon1) {
if (isPointInsidePolygon(point, polygon2)) {
return true;
}
}
for (LatLng point : polygon2) {
if (isPointInsidePolygon(point, polygon1)) {
return true;
}
}
return false;
}
private boolean isPointInsidePolygon(final LatLng tap, final 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;
}
private boolean rayCastIntersect(final LatLng tap, final LatLng vertA, final LatLng vertB) {
final double aY = vertA.latitude;
final double bY = vertB.latitude;
final double aX = vertA.longitude;
final double bX = vertB.longitude;
final double pY = tap.latitude;
final double pX = tap.longitude;
if ((aY > pY && bY > pY) || (aY < pY && bY < pY) || (aX < pX && bX < pX)) {
return false;
}
final double m = (aY - bY) / (aX - bX);
final double bee = (-aX) * m + aY;
final double x = (pY - bee) / m;
return x > pX;
}
doPolygonsHaveAnyCoincidingArea
ただし、共有領域が小さな三角形である場合があり、そのうちの 1 つだけisPointInsidePolygon
が true を返すことがあるため、可能性よりも遅いと思います。
2 つのポリゴンが衝突するか、1 つにもう 1 つのポリゴンが含まれているかを判断するより速い方法はありますか?