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

射线法与转角法
ExpandedBlockStart.gif代码
//点在多边形内 射线法
        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;
           }

 

ExpandedBlockStart.gif代码
 //点在多边形内 转角法
        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;
        }

 

 

转载于:https://www.cnblogs.com/LuGang/archive/2010/06/02/1749966.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值