java怎么判断一个点是否在一个多边形内

方法主体

/** 
		* 判断点是否在多边形内 
		* @param point 检测点 
		* @param pts   多边形的顶点 
		* @return      点在多边形内返回true,否则返回false 
		*/  
		public static boolean IsPtInPoly(Point2D.Double point, List<Point2D.Double> pts){
		   int N = pts.size();  
		   boolean boundOrVertex = true; //如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true  
		   int intersectCount = 0;
		   double precision = 2e-10; //浮点类型计算时候与0比较时候的容差  
		   Point2D.Double p1, p2;
		   Point2D.Double p = point; //当前点  
		     
		   p1 = pts.get(0);   
		   for(int i = 1; i <= N; ++i){      
		       if(p.equals(p1)){  
		           return boundOrVertex;
		       }  
		       p2 = pts.get(i % N);        
		       if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){                
		           p1 = p2;   
		           continue;
		       }  
		         
		       if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){
		           if(p.y <= Math.max(p1.y, p2.y)){                 
		               if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){
		                   return boundOrVertex;  
		               }  
		                 
		               if(p1.y == p2.y){                       
		                   if(p1.y == p.y){
		                       return boundOrVertex;  
		                   }else{
		                       ++intersectCount;  
		                   }   
		               }else{
		                   double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;                       
		                   if(Math.abs(p.y - xinters) < precision){
		                       return boundOrVertex;  
		                   }  
		                     
		                   if(p.y < xinters){ 
		                       ++intersectCount;  
		                   }   
		               }  
		           }  
		       }else{                 
		           if(p.x == p2.x && p.y <= p2.y){                    
		               Point2D.Double p3 = pts.get((i+1) % N);                      
		               if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){
		                   ++intersectCount;  
		               }else{  
		                   intersectCount += 2;  
		               }  
		           }  
		       }              
		       p1 = p2;  
		   }  
		     
		   if(intersectCount % 2 == 0){
		       return false;  
		   } else { 
		       return true;  
		   }  
		     
		}

测试的main函数

public static void main(String[] args) {
			List<Point2D.Double> list = new ArrayList<Point2D.Double>();
			Point2D.Double p1 = new Point2D.Double(121.463273,31.231155);
			Point2D.Double p2 = new Point2D.Double(121.463895,31.22754);
			Point2D.Double p3 = new Point2D.Double(121.47177,31.231889);
			Point2D.Double p4 = new Point2D.Double(121.47177,31.231889);
			Point2D.Double p5 = new Point2D.Double(121.461728,31.233595);
			Point2D.Double p6 = new Point2D.Double(122.461728,32.233595);
			list.add(p1);
			list.add(p2);
			list.add(p3);
			list.add(p4);
			list.add(p5);
			System.out.println(IsPtInPoly(p6,list));
		}

猜你喜欢

转载自blog.csdn.net/minolk/article/details/85012014