1. 程式人生 > >C#+AE 判斷點是否在面內的方法

C#+AE 判斷點是否在面內的方法

    public double getXMinValue(IPolygon nPolygon)
        {
            IPolygon sPolygon;
            sPolygon = nPolygon;
            IPointCollection pPointCollection;
            pPointCollection = sPolygon as IPointCollection;

            int n = pPointCollection.PointCount;
            double[] coordX = new double[n];
          
            for (int i = 0; i < n; i++)
            {
                IPoint point = pPointCollection.get_Point(i);
                coordX[i] = point.X;
            }
            //對陣列進行從小到大排序
            System.Array.Sort(coordX);        

            Xmin = coordX[0];            
            return Xmin;
        }

向左畫射線並得到交點個數的程式碼:

try
                        {
 
                            ILine newLine = new LineClass();

                            IPoint toPoint = new PointClass();
                            toPoint.PutCoords(getXMinValue(pPolygon) - 20.0000000000000, pPoint.Y);

                            newLine.PutCoords(pPoint, toPoint);//給新的直線賦予起始座標

                            //苗師兄想法
                            IPolyline l = new PolylineClass();
                            l.FromPoint = pPoint;
                            l.ToPoint = toPoint;


                            IGeometryCollection pPolyline = new PolylineClass();
                            ISegmentCollection pPath;
                            pPath = new PathClass();
                            object missing1 = Type.Missing;
                            object missing2 = Type.Missing;

                            pPath.AddSegment(newLine as ISegment, ref missing1, ref missing2);
                            pPolyline.AddGeometry(pPath as IGeometry, ref missing1, ref missing2);

                            IElement element = DrawLineSymbol(pPolyline as IGeometry, pColor);
                            pGraph.AddElement(element, 0);
                            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

                         
                            ITopologicalOperator pTopo = pPolygon as ITopologicalOperator;//pPolygon多邊形
                            IGeometryCollection pGeoCol = pTopo.Intersect((IGeometry)l, esriGeometryDimension.esriGeometry0Dimension) as IGeometryCollection; //執行到這一句有問題;
                            IPointCollection pPointCol = pGeoCol as IPointCollection;
                            if ((pPointCol.PointCount) % 2 == 0)
                            {
                                MessageBox.Show("射線和多邊形的交點個數為:"+pPointCol.PointCount.ToString()+",點在多邊形外。");
                            }
                            else
                            {
                                MessageBox.Show("射線和多邊形的交點個數為:"+pPointCol.PointCount.ToString()+",點在多邊形內。");
                            }
                          

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

本文是按照Polygon物件考慮的面,所以並未考慮重疊面的問題。

最近幫別人解決了一個問題,如何判斷一個座標點,是否在多邊形區域內(二維)。

在網上搜索了一圈,都是自己寫程式碼,有多種演算法,分凸多邊形、凹多邊形,總之是麻煩。

繼續搜尋,瞭解到 Java/dotnet 自帶的類庫中,都有現成的類函式,可以解決這個問題。

考慮到了解的人不多,特將相關知識共享出來,也許大家以後也用得著。

a) dotnet 中,用 System.Drawing.Drawing2D.GraphicsPath 和 Region 類聯合起來,然後用 Region.IsVisible(point) 函式,可以判斷點是否在多邊形區域內。

b) Java 中,使用 java.awt.Polygon.contains(point) ,或者 java.awt.geom.GeneralPath.contains(point) 函式,都可以判斷點是否在多邊形區域內。

以下是程式碼示例:

code c#:

System.Drawing.Drawing2D.GraphicsPath myGraphicsPath=new System.Drawing.Drawing2D.GraphicsPath(); 
Region myRegion=new Region(); 
myGraphicsPath.Reset(); 

//添家多邊形點 
Point p1=new Point(x1,y1);
Point p2=new Point(x2,y2);
Point p3=new Point(x3,y3);
Point p4=new Point(x4,y4);

myGraphicsPath.AddPolygon(LoadPoint(p1,p2,p2,p4)); 
myRegion.MakeEmpty(); 
myRegion.Union(myGraphicsPath); 
//返回判斷點是否在多邊形裡
bool myPoint =myRegion.IsVisible(MousePoint);

code java 1:

public boolean checkWithJdkGeneralPath(Point2D.Double point, List<Point2D.Double> polygon) {
  java.awt.geom.GeneralPath p = new java.awt.geom.GeneralPath();

   Point2D.Double first = polygon.get(0);
   p.moveTo(first.x, first.y);

   for (Point2D.Double d : polygon) {
      p.lineTo(d.x, d.y);
   }

   p.lineTo(first.x, first.y);

   p.closePath();

   return p.contains(point);

}

code java 2:

public boolean checkWithJdkPolygon(Point2D.Double point, List<Point2D.Double> polygon) {
    java.awt.Polygon p = new Polygon();

    // java.awt.geom.GeneralPath
    final int TIMES = 1000;

    for (Point2D.Double d : polygon) {
        int x = (int) d.x * TIMES;
        int y = (int) d.y * TIMES;
        p.addPoint(x, y);
    }

    int x = (int) point.x * TIMES;
    int y = (int) point.y * TIMES;

    return p.contains(x, y);

}

java.awt.Polygon 好像只能處理整數座標值,不能處理浮點數。