GIS算法-点是否在多边形内

射线法与转角法
代码
//点在多边形内 射线法
        public static bool IsPointInPolygonRadial(Vertex vertexP, Polygon polygon)
           {
               
bool pResult = false;
             

               
int pCountInOut = 0;

                
//射线
               Point pt= new Point(100000,vertexP.CenterPoint.Y);
               ShapeCollection pTempShapeCol 
= new ShapeCollection(new Shape[] {vertexP,new Vertex(pt)});
               Segment pRadialY 
= new Segment(pTempShapeCol);

               

                
//多边形所有边
               List<AlgorithmDataStructure.Segment> pSegmentsOfPolygon = AlgorithmDataStructure.ShapeUtility.GetSegmentsOfPolygon(polygon);

               
for (int i = 0; i < pSegmentsOfPolygon.Count; i++)
               {
                   
                       
//水平边不参与相交测试
                       if (AlgorithmDataStructure.ShapeUtility.IsSegmentHorizontal(pSegmentsOfPolygon[i]))
                       {
                           
                           
continue;
                       }
                       
//交点存在
                       Vertex pIntersectVertexRadialWithSegment = IsSegmentsIntersectWithPolynomial(pRadialY, pSegmentsOfPolygon[i]);
                       
if (pIntersectVertexRadialWithSegment != null)
                       {
                           
//射线与边的交点控制在边P点右边
                           if (vertexP.CenterPoint.X < pIntersectVertexRadialWithSegment.CenterPoint.X && vertexP.CenterPoint.Y == pIntersectVertexRadialWithSegment.CenterPoint.Y)
                           {
                               
//方向向上的边
                               if (AlgorithmDataStructure.ShapeUtility.IsSegmentUpward(pSegmentsOfPolygon[i]))
                               {
                                   
//交点不能与终点相同
                                   if (pIntersectVertexRadialWithSegment.CenterPoint.X != pSegmentsOfPolygon[i].Vertex2.CenterPoint.X
                                       
|| pIntersectVertexRadialWithSegment.CenterPoint.Y != pSegmentsOfPolygon[i].Vertex2.CenterPoint.Y)
                                   {
                                       pCountInOut
++;
                                   }
                               }

                               
//方向向下的边
                               if (AlgorithmDataStructure.ShapeUtility.IsSegmentDownward(pSegmentsOfPolygon[i]))
                               {
                                   
//交点不能与起点相同
                                   if (pIntersectVertexRadialWithSegment.CenterPoint.X != pSegmentsOfPolygon[i].Vertex1.CenterPoint.X
                                       
|| pIntersectVertexRadialWithSegment.CenterPoint.Y != pSegmentsOfPolygon[i].Vertex1.CenterPoint.Y)
                                   {
                                       pCountInOut
++;
                                   }
                               }
                           }
                           
                       }

               }

               
if ((pCountInOut == 1|| (pCountInOut % 2 != 0))
               {
                   pResult 
= true
               }

               
return pResult;
           }

 

代码
 //点在多边形内 转角法
        public static bool IsPointInPolygonAngle(Vertex vertexP, Polygon polygon)
        {
            
bool pResult = false;


            
int pCountWN = 0;

            
//射线
            Point pt = new Point(100000, vertexP.CenterPoint.Y);
            ShapeCollection pTempShapeCol 
= new ShapeCollection(new Shape[] { vertexP, new Vertex(pt) });
            Segment pRadialY 
= new Segment(pTempShapeCol);



            
//多边形所有边
            List<AlgorithmDataStructure.Segment> pSegmentsOfPolygon = AlgorithmDataStructure.ShapeUtility.GetSegmentsOfPolygon(polygon);

            
for (int i = 0; i < pSegmentsOfPolygon.Count; i++)
            {

                
//水平边不参与相交测试
                if (AlgorithmDataStructure.ShapeUtility.IsSegmentHorizontal(pSegmentsOfPolygon[i]))
                {

                    
continue;
                }
                
if (ShapeUtility.IsSegmentUpward(pSegmentsOfPolygon[i]))
                {
                    
//P点严格在边的左边,矢量(v1,v2)前进方向左边
                    if (ShapeUtility.IsPointAtSegmentLeftSide(vertexP, pSegmentsOfPolygon[i]))
                    {
                        Vertex pIntersectVertexRadialWithSegment 
= IsSegmentsIntersectWithPolynomial(pRadialY, pSegmentsOfPolygon[i]);
                        
if (pIntersectVertexRadialWithSegment != null)
                        {
                            
//射线与边的交点控制在P点右边
                            if (vertexP.CenterPoint.X < pIntersectVertexRadialWithSegment.CenterPoint.X && vertexP.CenterPoint.Y == pIntersectVertexRadialWithSegment.CenterPoint.Y)
                            {
                                
//交点不能与终点相同
                                if (pIntersectVertexRadialWithSegment.CenterPoint.X != pSegmentsOfPolygon[i].Vertex2.CenterPoint.X
                                    
|| pIntersectVertexRadialWithSegment.CenterPoint.Y != pSegmentsOfPolygon[i].Vertex2.CenterPoint.Y)
                                {

                                    pCountWN
++;
                                }
                            }
                        }
                    }
                }
                
else if (ShapeUtility.IsSegmentDownward(pSegmentsOfPolygon[i]))
                {
                    
//P点严格在边的左边,矢量(v1,v2)前进方向左边
                    if (ShapeUtility.IsPointAtSegmentRightSide(vertexP, pSegmentsOfPolygon[i]))
                    {
                        Vertex pIntersectVertexRadialWithSegment 
= IsSegmentsIntersectWithPolynomial(pRadialY, pSegmentsOfPolygon[i]);
                        
if (pIntersectVertexRadialWithSegment != null)
                        {
                            
//射线与边的交点控制在P点右边
                            if (vertexP.CenterPoint.X < pIntersectVertexRadialWithSegment.CenterPoint.X && vertexP.CenterPoint.Y == pIntersectVertexRadialWithSegment.CenterPoint.Y)
                            {
                                
//交点不能与终点相同
                                if (pIntersectVertexRadialWithSegment.CenterPoint.X != pSegmentsOfPolygon[i].Vertex2.CenterPoint.X
                                    
|| pIntersectVertexRadialWithSegment.CenterPoint.Y != pSegmentsOfPolygon[i].Vertex2.CenterPoint.Y)
                                {

                                    pCountWN
--;
                                }
                            }
                        }
                    }
                }

              
            }

            
if (pCountWN!=0)
            {
                pResult 
= true;
            }

            
return pResult;
        }

 

 

posted on 2010-06-02 14:16  LuGang  阅读(682)  评论(2编辑  收藏  举报

导航