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对象考虑的面,所以并未考虑重叠面的问题。

你可能感兴趣的:(C#)