gpt4 book ai didi

java - Android 寻找多边形的交集?

转载 作者:行者123 更新时间:2023-12-01 15:15:11 25 4
gpt4 key购买 nike

我目前有一些多边形形状,如下所示,正在使用以下代码将其绘制到我的 map View 上

enter image description here

    CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
{

GeoPoint sector1 = new GeoPoint((int)(customPolygon.getCorrdinateList().get(n).getLatitude()*1e6), (int)((customPolygon.getCorrdinateList().get(n).getLongitude())*1e6));
if(n==0){
mapView.getProjection().toPixels(sector1, point1_draw);
path.moveTo(point1_draw.x,point1_draw.y);
}else
{
mapView.getProjection().toPixels(sector1, point1_draw);
path.lineTo(point1_draw.x,point1_draw.y);
}
}

path.close();
canvas.drawPath(path, paint);

现在我正在考虑如何知道 ontap 按钮是否与任何多边形相交。例如,如果它与其中一个多边形相交,则会显示一条消息,显示当前多边形。

我被困在叠加层的 ontap 部分。

    @Override
public boolean onTap(GeoPoint p, MapView ) {



Point point1_draw = new Point();
mapView.getProjection().toPixels(p, point1_draw);


for (int i =0;i<data.getCustomPolygonList().size();i++)
{
CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
{
}

}

return true;
}

最佳答案

我假设您需要一些代码来检查按钮是否在多边形内,对吗?

我现在无法给你代码,但这是一个粗略的算法:

for each line segment in the polygon
calculate the dot product for the line segment and the line formed by the starting vertex and the point to check
if all dot products have the same sign, the point is inside the polygon

请注意,要使其工作,您需要多边形连续缠绕,即所有顶点都按顺时针或逆时针添加。此外,这种方法可能并不总是适用于凹多边形。因此,您可能希望将凹多边形分割成一系列凸多边形。

有关更通用(但也更昂贵)的算法,请查看此 wiki 页面:http://en.wikipedia.org/wiki/Point_in_polygon

另一个信息来源(以及一些代码):How can I determine whether a 2D Point is within a Polygon?

关于java - Android 寻找多边形的交集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11717809/

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