当前位置 : 主页 > 编程语言 > c语言 >

c# 判断点是否在多边形内

来源:互联网 收集:自由互联 发布时间:2021-06-25
/// summary /// 判断点是否在多边形内 /// /summary /// param name="checkPoint" 需要判断的点 /param /// param name="polygonPoints" 组成多边形点的集合 /param /// returns/returns public static bool IsInPolygon2(PointLat
 /// <summary>
        /// 判断点是否在多边形内
        /// </summary>
        /// <param name="checkPoint">需要判断的点</param>
        /// <param name="polygonPoints">组成多边形点的集合</param>
        /// <returns></returns>
        public static bool IsInPolygon2(PointLatLng checkPoint, List<PointLatLng> polygonPoints)
        {
            int counter = 0;
            int i;
            double xinters;
            PointLatLng p1, p2;
            int pointCount = polygonPoints.Count;
            p1 = polygonPoints[0];
            for (i = 1; i <= pointCount; i++)
            {
                p2 = polygonPoints[i % pointCount];
                if (checkPoint.Lng > Math.Min(p1.Lng, p2.Lng)//校验点的Y大于线段端点的最小Y
                    && checkPoint.Lng <= Math.Max(p1.Lng, p2.Lng))//校验点的Y小于线段端点的最大Y
                {
                    if (checkPoint.Lat <= Math.Max(p1.Lat, p2.Lat))//校验点的X小于等线段端点的最大X(使用校验点的左射线判断).
                    {
                        if (p1.Lng != p2.Lng)//线段不平行于X轴
                        {
                            xinters = (checkPoint.Lng - p1.Lng) * (p2.Lat - p1.Lat) / (p2.Lng - p1.Lng) + p1.Lat;
                            if (p1.Lat == p2.Lat || checkPoint.Lat <= xinters)
                            {
                                counter++;
                            }
                        }
                    }

                }
                p1 = p2;
            }

            if (counter % 2 == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
View Code
网友评论