Java判断一个点是否在一个多边形内

Java根据几个点坐标,画一个区域,判断其他坐标是否在区域内

调用isInPolygon方法,带上参数就可以,参数接收可以根据自己需要替换掉。
代码如下:

package com.maptest.util;

import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

import com.maptest.entity.AllUser;

public class GetRegionUsersUtil {

	/**
	 * 判断是否在多边形区域内
	 *
	 * @param lonandlat要判断的点的横坐标 经度,纵坐标 维度
	 * @param lon      区域各顶点的横坐标数组
	 * @param lat      区域各顶点的纵坐标数组
	 * @return
	 */
	public static List<Integer> isInPolygon(List<AllUser> lonandlat, double[] lon, double[] lat) {

		// 符合区域内的信息id
		List<Integer> idList = new ArrayList<Integer>();
		// 将区域各顶点的横纵坐标放到一个点集合里面
		List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
		double polygonPoint_x = 0.0, polygonPoint_y = 0.0;
		for (int i = 0; i < lon.length; i++) {
			polygonPoint_x = lon[i];
			polygonPoint_y = lat[i];
			Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);
			pointList.add(polygonPoint);
		}
		Point2D.Double first = pointList.get(0);
		GeneralPath peneralPath = area(pointList);
		// 将要判断的横纵坐标组成一个点
		for (AllUser map : lonandlat) {
			Point2D.Double point = new Point2D.Double(map.getLongitude(), map.getLatitude());
			//System.out.println("测试点:" + point);
			//System.out.println("测试区域:" + pointList);
			// 判断是否在多边形区域边上
			if (isOnPolygon(pointList, first, point)) {
				idList.add(map.getId());
			}
			// 判判断是否在多边形区域内部
			if (check(point, peneralPath)) {
				idList.add(map.getId());
				//System.out.println("成功:" + point);
			}
		}

		return idList;
	}

	/**
	 * @param point   要判断的点的横纵坐标
	 * @param polygon 组成的顶点坐标集合
	 * @return
	 */
	private static GeneralPath area(List<Point2D.Double> polygon) {
		java.awt.geom.GeneralPath peneralPath = new java.awt.geom.GeneralPath();

		Point2D.Double first = polygon.get(0);
		// 通过移动到指定坐标(以双精度指定),将一个点添加到路径中
		peneralPath.moveTo(first.x, first.y);
		polygon.remove(0);
		for (Point2D.Double d : polygon) {
			// 通过绘制一条从当前坐标到新指定坐标(以双精度指定)的直线,将一个点添加到路径中。
			peneralPath.lineTo(d.x, d.y);
		}
		// 将几何多边形封闭
		peneralPath.lineTo(first.x, first.y);
		peneralPath.closePath();
		return peneralPath;
		// 测试指定的 Point2D 是否在 Shape 的边界内。
		// return peneralPath.contains(point);
	}

	private static boolean check(Point2D.Double point, GeneralPath peneralPath) {
		// 测试指定的 Point2D 是否在 Shape 的边界内。
		return peneralPath.contains(point);
	}

	/**
	 * 判断是否在多边形区域边上
	 *
	 * @return
	 */
	public static boolean isOnPolygon(List<Point2D.Double> polygon, Point2D.Double first, Point2D.Double point) {
		boolean rtn = false;
		for (int i = 1; i < polygon.size(); i++) {
			Point2D.Double next = polygon.get(i);
			boolean pdline = (point.x - first.getX()) * (first.getY() - next.getY()) == (first.getX() - next.getX())
					* (point.y - first.getY());
			first = next;
			if (pdline) {
				rtn = pdline;
			}
		}
		return rtn;

	}
}
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱衣点头戈子衫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值