2012-07-30 58 views
0

我现在已经几多边形如下图所示,与下面的代码Android查找多边形交集?

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); 

我现在就盘算上被绘制到我的MapView我怎么知道如果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; 
} 
+0

你有一个类型:'getCorrdinateList'应该是'getCoordinateList'。要修复它,请点击它并在大多数IDE中按下Ctrl-Shift-R。 – 2012-07-30 08:33:30

+0

哦错字,这是一个用于存储多边形坐标的自定义类 – ericlee 2012-07-30 08:49:50

回答

1

我假设你需要一些代码来检查按钮是否在一个多边形右边?

我不能给你的代码的权利,但这里有一个粗略的算法:

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 

注意,对于这个工作,你需要的多边形有一个连续的绕组,即所有顶点要么添加顺时针或逆时针。此外,这种方法可能并不总是适用于凹多边形。因此,您可能想要将凹多边形分成一系列凸多边形。

对于更一般(但也更昂贵)的算法来看看这个维基页面:http://en.wikipedia.org/wiki/Point_in_polygon

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

+0

这仅适用于凸多边形。 – 2012-07-30 08:49:07

+0

@MarekR,你看到我的笔记吗? “另外,这种方法可能并不适用于凹多边形 - 它可能适用于某些点,并且不适用于其他点。 – Thomas 2012-07-30 08:50:06

+0

是的,我没有读全部。对于那个很抱歉。无论如何,你的算法是错误的,因为它不应该是一个点积,而是跨产品! – 2012-07-30 09:00:43

0

我很惊讶的是Android系统。 graphics.Path和android.graphics.PathMeasure没有为此提供API。这应该。

该算法是described here