2012-07-30 56 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