码迷,mamicode.com
首页 > 编程语言 > 详细

多边形范围点判定算法

时间:2015-02-15 16:38:17      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:范围   算法   

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

 

技术分享

 

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);
	    }
	    
	    
		
	}



 

多边形范围点判定算法

标签:范围   算法   

原文地址:http://blog.csdn.net/liuzhoulong/article/details/43835715

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!