指定的经纬度是否落在多边形内 java版

这个想法算法就是判断一个点向左的射线跟一个多边形的交叉点有几个,如果结果为奇数的话那么说明这个点落在多边形中,反之则不在。

A:

B:

C:

D:

E:

no1:

no2:

y1:

y2:

以上的ABCDE,分别是以下数组里面的数据

1 Point[] ps = new Point[] { new Point(120.2043 , 30.2795), new Point(120.2030 , 30.2511), new Point(120.1810 , 30.2543), new Point(120.1798 , 30.2781), new Point(120.1926,30.2752) };
 1 package com.cmcc.monitor.test;
 2 
 3 public class GisTest {
 4 
 5     public static void main(String[] args) {
 6         Point[] ps = new Point[] { new Point(120.2043 , 30.2795), new Point(120.2030 , 30.2511), new Point(120.1810 , 30.2543), new Point(120.1798 , 30.2781), new Point(120.1926,30.2752) };
 7         Point n1 = new Point(120.1936 , 30.2846);
 8         Point n2 = new Point(120.1823 , 30.2863);
 9         Point n3 = new Point(120.2189 , 30.2712);
10         Point y1 = new Point(120.1902 , 30.2712);
11         Point y2 = new Point(120.1866 , 30.2672);
12         Point y4 = new Point(120.1869 , 30.2718);
13         System.out.println( "n1:" + isPtInPoly(n1.getX() , n1.getY() , ps));
14         System.out.println( "n2:" + isPtInPoly(n2.getX() , n2.getY() , ps));
15         System.out.println( "n3:" + isPtInPoly(n3.getX() , n3.getY() , ps));
16         System.out.println( "y1:" + isPtInPoly(y1.getX() , y1.getY() , ps));
17         System.out.println( "y2:" + isPtInPoly(y2.getX() , y2.getY() , ps));
18         System.out.println( "y4:" + isPtInPoly(y4.getX() , y4.getY() , ps));
19     }
20     public static boolean isPtInPoly (double ALon , double ALat , Point[] ps) {
21         int iSum, iCount, iIndex;
22         double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
23         if (ps.length < 3) {
24             return false;
25         }
26         iSum = 0;
27         iCount = ps.length;
28         for (iIndex = 0; iIndex<iCount;iIndex++) {
29             if (iIndex == iCount - 1) {
30                 dLon1 = ps[iIndex].getX();
31                 dLat1 = ps[iIndex].getY();
32                 dLon2 = ps[0].getX();
33                 dLat2 = ps[0].getY();
34             } else {
35                 dLon1 = ps[iIndex].getX();
36                 dLat1 = ps[iIndex].getY();
37                 dLon2 = ps[iIndex + 1].getX();
38                 dLat2 = ps[iIndex + 1].getY();
39             }
40             // 以下语句判断A点是否在边的两端点的水平平行线之间,在则可能有交点,开始判断交点是否在左射线上
41             if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {
42                 if (Math.abs(dLat1 - dLat2) > 0) {
43                     //得到 A点向左射线与边的交点的x坐标:
44                     dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat) ) / (dLat1 - dLat2);
45                     // 如果交点在A点左侧(说明是做射线与 边的交点),则射线与边的全部交点数加一:
46                     if (dLon < ALon) {
47                         iSum++;
48                     }
49                 }
50             }
51         }
52         if ((iSum % 2) != 0) {
53             return true;
54         }
55         return false;
56     }
57 }
 1 package com.cmcc.monitor.test;
 2 
 3 public class Point {
 4     private Double x;
 5     private Double y;
 6     public Point (Double x , Double y) {
 7         this.x = x;
 8         this.y = y;
 9     }
10     public Double getX() {
11         return x;
12     }
13     public void setX(Double x) {
14         this.x = x;
15     }
16     public Double getY() {
17         return y;
18     }
19     public void setY(Double y) {
20         this.y = y;
21     }
22     
23 }

原文链接:https://blog.csdn.net/qq_22929803/article/details/46818009

原文地址:https://www.cnblogs.com/tyjsjl/p/8760061.html