判断点是否在多边形范围内

       判断点与多边形的关系(在平面上),不能用简单的向量叉乘来判断,特别是在有凹边形的情况下,在进行判断前,建议先进行范围大致判断,在许多情况下,应该说大部分情况下,我们进行判断的对象在空间上相差可能很远,如果一开始就直接用算法去计算,这样会浪费大量的计算时间和空间,所以在使用具体算法前,先进行一个大致范围的判断。先判断它是否有可能在这个区域,如果不可能就直接out,如果有可能,再用算法去计算具体是否在范围内。

/**
	 * 
	 * @Title: 判断点是否在多边形的范围内
	 * @param: @param point 点坐标,长度为2
	 * @param: @param polyline 多边形节点坐标,长度为2*n,其中n应大于或等于3,即至少为三角形
	 * @param: @return   
	 * @return: int  值为1表示点在多边形范围内;  
	 * 				  值为0表示点在多边形边上; 
	 * 				  值为-1表示点不在多边形范围内。
	 * @throws
	 */
	public static int PolygonIsContainPoint(double[] point, double[] polyline) {
		int result = -1, count = 0, pointcount = 0, tempI;
		double maxx = 0, minx = 0, maxy = 0, miny = 0;
		if (polyline != null) {
			int i;
			pointcount = polyline.length / 2;
			maxx = minx = polyline[0];
			maxy = miny = polyline[1];
			for (i = 0; i < pointcount; i++) {
				tempI = i + i;
				if (maxx < polyline[tempI])
					maxx = polyline[tempI];
				if (minx > polyline[tempI])
					minx = polyline[tempI];
				if (maxy < polyline[tempI + 1])
					maxy = polyline[tempI + 1];
				if (miny > polyline[tempI + 1])
					miny = polyline[tempI + 1];
			}
		}
		if (point != null) {
			// 首先判断是否在面的外框范围内
			if (point[0] < minx || point[0] > maxx || point[1] < miny
					|| point[1] > maxy) {
				return result;
			} else {
				int i, j;
				j = pointcount - 1;
				double[] point1, point2;
				double tempValue;
				for (i = 0; i < pointcount; i++) {
					point1 = new double[2];
					point2 = new double[2];
					tempI = i + i;
					point1[0] = polyline[tempI];
					point1[1] = polyline[tempI + 1];
					tempI = j + j;
					point2[0] = polyline[tempI];
					point2[1] = polyline[tempI + 1];
					if ((point1[0] < point[0] && point2[0] >= point[0])
							|| (point2[0] < point[0] && point1[0] >= point[0])) {
						tempValue = point1[1] + (point[0] - point1[0])
								/ (point2[0] - point1[0])
								* (point2[1] - point1[1]);
						if (tempValue < point[1]) {
							count++;
						} else if (tempValue == point[1]) {
							count = -1;
							break;
						}
					}
					j = i;
				}
			}
		}
		if (count == -1) {
			result = 0;// 点在线段上
		} else {
			tempI = count % 2;
			if (tempI == 0)// 为偶数
			{
				result = -1;
			} else {
				result = 1;
			}
		}
		if(polyline != null && point != null){
			if(point[0] ==polyline[0] && point[1] ==polyline[1]){
				result = 0;
			}
		}
		return result;
	}

	public static void main(String[] args) {

		int result =  PolygonIsContainPoint(new double[]{106.61502,35.84901}, new double[]{106.61502,35.84901, 
                106.61605,35.85133, 
                106.62112,35.85416, 
                106.62635,35.85305, 
                106.63725,35.85219, 
                106.64867,35.84953, 
                106.65021,35.84627, 
                106.65202,35.84052, 
                106.64472,35.84352, 
                106.63991,35.84781, 
                106.63399,35.84798, 
                106.62807,35.84549, 
                106.62258,35.84704, 
                106.61674,35.84841}); 
		System.out.println(result); 
	}

输出:0

转:http://peizhiinfo.iteye.com/blog/1237481


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值