c# 判斷點是否在多邊形內


 /// <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

 


免責聲明!

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



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