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



整體思路:射線法。
①:先判斷點的X和Y坐標和多邊形的Xmin,Xmax,Ymin,Ymax的關系。若超出了這四個值,則一定在多邊形外;
②:若不符合上述條件,則繼續。向左做線段,線段的左頂點的X坐標值一定要小於Xmin。然后判斷線段和多邊形的交點;
若交點為偶數個,則在多邊形外;若交點為奇數個,則點在多邊形內;
③:因為最后取的交點是按照Ipolyline和多邊形Polygon作得拓撲關系的交集,交集是IGeometry對象,其向IgeometryCollection對象跳轉,最后得到一個點擊IpointCollection,根據PointCount屬性可得到相交點的個數。
 
 
得到最小X坐標值的函數:

    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對象考慮的面,所以並未考慮重疊面的問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM