多边形范围点判定算法

判断一个二维坐标点是否在一个多边形范围框内,首先给出范围框各顶点的坐标(按顺时针方向给出),分别放到两个数组中再比较大小范围从而判定点是否在多边形返回框中

 

技术分享

 

code如下:

public class RangePoint  {

	double[] x_points;
	double[] y_points;
	public RangePoint(){}
	public RangePoint(double[] x_points,double[] y_points) {
		this.x_points = x_points;
		this.y_points = y_points;
	}
	
	public boolean RangeMatch(double x, double y) {
		int j = x_points.length - 1;
		boolean odd_nodes = false;
		for (int i = 0; i < x_points.length; i++){
			 if (((y_points[i] < y && y_points[j] >= y)
		                || (y_points[j] < y && y_points[i] >= y))
		                && (x_points[i] <= x || x_points[j] <= x))
		        {
		            odd_nodes ^= (x_points[i] 
		                    + (y - y_points[i]) / (y_points[j] - y_points[i]) 
		                    * (x_points[j] - x_points[i]) < x);
		        }
		        j = i;
		}
        if (odd_nodes==true) {
        	return true;
        }
        return false;
	}


	public static void main(String[] args) {
		
		double[] x_points = {0, 0, 2, 2 };
		double[] y_points = {0, 1.8, 2, 0 };
		double x = 1.9;
		double y = 1.8;

		RangePoint rp = new RangePoint(x_points,y_points);
		
	    if (rp.RangeMatch(x, y)) {
	    	System.out.println("This Range include Point:" + x +","+ y);
	    }

	}

}

 好吧,这个东西有啥用呢??

 看到如下测试代码你可能会觉得有用了,这个是判断某个经纬度坐标是否在给定的范围区域内,下面是故宫顶点的经纬度(可以通过ditu.google.cn点击查看到具体经纬度的值)

	public static void main(String[] args) {
		
	
		
		double[] x_points = {39.922886, 39.923264, 39.913275, 39.912929 };
		double[] y_points = {116.391517, 116.40199, 116.402505, 116.392034 };
		
	     double x = 39.922804;
	  	double y = 116.391581;
	  	
		
		RangePoint rp = new RangePoint(x_points,y_points);
		
	    if (rp.RangeMatch(x, y)) {
	    	System.out.println("This Range include Point:" + x +","+ y);
	    }
	    
	    
		
	}



 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。