大龙的博客

常用链接

统计

最新评论

判断点是否在任意多边形内(java)

Java代码  收藏代码
  1. import java.util.ArrayList;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void main(String[] args) {  
  6.         double px = 113.0253;  
  7.         double py = 23.98049;  
  8.         ArrayList<Double> polygonXA = new ArrayList<Double>();  
  9.         ArrayList<Double> polygonYA = new ArrayList<Double>();  
  10.         polygonXA.add(113.0253);  
  11.         polygonXA.add(113.4121);  
  12.         polygonXA.add(113.37109);  
  13.         polygonXA.add(113.02148);  
  14.         // 113.18359,23.8496  
  15.   
  16.         // 113.0253,23.98049 113.4121,23.9687 113.37109,2.73828  
  17.   
  18.         // 113.02148,23.7539C  
  19.   
  20.         polygonYA.add(23.98049);  
  21.         polygonYA.add(23.9687);  
  22.         polygonYA.add(23.73828);  
  23.         polygonYA.add(23.7539);  
  24.         Test test = new Test();  
  25.         System.out.println(test.isPointInPolygon(px, py, polygonXA, polygonYA));  
  26.     }  
  27.   
  28.     public boolean isPointInPolygon(double px, double py,  
  29.             ArrayList<Double> polygonXA, ArrayList<Double> polygonYA) {  
  30.         boolean isInside = false;  
  31.         double ESP = 1e-9;  
  32.         int count = 0;  
  33.         double linePoint1x;  
  34.         double linePoint1y;  
  35.         double linePoint2x = 180;  
  36.         double linePoint2y;  
  37.   
  38.         linePoint1x = px;  
  39.         linePoint1y = py;  
  40.         linePoint2y = py;  
  41.   
  42.         for (int i = 0; i < polygonXA.size() - 1; i++) {  
  43.             double cx1 = polygonXA.get(i);  
  44.             double cy1 = polygonYA.get(i);  
  45.             double cx2 = polygonXA.get(i + 1);  
  46.             double cy2 = polygonYA.get(i + 1);  
  47.             if (isPointOnLine(px, py, cx1, cy1, cx2, cy2)) {  
  48.                 return true;  
  49.             }  
  50.             if (Math.abs(cy2 - cy1) < ESP) {  
  51.                 continue;  
  52.             }  
  53.   
  54.             if (isPointOnLine(cx1, cy1, linePoint1x, linePoint1y, linePoint2x,  
  55.                     linePoint2y)) {  
  56.                 if (cy1 > cy2)  
  57.                     count++;  
  58.             } else if (isPointOnLine(cx2, cy2, linePoint1x, linePoint1y,  
  59.                     linePoint2x, linePoint2y)) {  
  60.                 if (cy2 > cy1)  
  61.                     count++;  
  62.             } else if (isIntersect(cx1, cy1, cx2, cy2, linePoint1x,  
  63.                     linePoint1y, linePoint2x, linePoint2y)) {  
  64.                 count++;  
  65.             }  
  66.         }  
  67.         if (count % 2 == 1) {  
  68.             isInside = true;  
  69.         }  
  70.   
  71.         return isInside;  
  72.     }  
  73.   
  74.     public double Multiply(double px0, double py0, double px1, double py1,  
  75.             double px2, double py2) {  
  76.         return ((px1 - px0) * (py2 - py0) - (px2 - px0) * (py1 - py0));  
  77.     }  
  78.   
  79.     public boolean isPointOnLine(double px0, double py0, double px1,  
  80.             double py1, double px2, double py2) {  
  81.         boolean flag = false;  
  82.         double ESP = 1e-9;  
  83.         if ((Math.abs(Multiply(px0, py0, px1, py1, px2, py2)) < ESP)  
  84.                 && ((px0 - px1) * (px0 - px2) <= 0)  
  85.                 && ((py0 - py1) * (py0 - py2) <= 0)) {  
  86.             flag = true;  
  87.         }  
  88.         return flag;  
  89.     }  
  90.   
  91.     public boolean isIntersect(double px1, double py1, double px2, double py2,  
  92.             double px3, double py3, double px4, double py4) {  
  93.         boolean flag = false;  
  94.         double d = (px2 - px1) * (py4 - py3) - (py2 - py1) * (px4 - px3);  
  95.         if (d != 0) {  
  96.             double r = ((py1 - py3) * (px4 - px3) - (px1 - px3) * (py4 - py3))  
  97.                     / d;  
  98.             double s = ((py1 - py3) * (px2 - px1) - (px1 - px3) * (py2 - py1))  
  99.                     / d;  
  100.             if ((r >= 0) && (r <= 1) && (s >= 0) && (s <= 1)) {  
  101.                 flag = true;  
  102.             }  
  103.         }  
  104.         return flag;  
  105.     }  
  106. }  

posted on 2011-08-22 03:27 大龙 阅读(3482) 评论(1)  编辑 收藏 引用

评论

# re: 判断点是否在任意多边形内(java)[未登录] 2012-06-25 19:46 wei

不行呀 误差好大呀 囧....  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理