判断点是否在凹多边形或交叉多边形内~

在java中:

public static boolean checkWithJdkGeneralPath(Point2D.Double point, List<Point2D.Double> polygon) {
    java.awt.geom.GeneralPath p = new java.awt.geom.GeneralPath();

    Point2D.Double first = polygon.get(0);
    p.moveTo(first.x, first.y);

    for (Point2D.Double d : polygon) {
        p.lineTo(d.x, d.y);
    }

    p.lineTo(first.x, first.y);
    p.closePath();

    return p.contains(point);
}

 

例子:

public static void main(String[] args) {
    Point2D.Double pd  =  new Point2D.Double();
    // 区域外的点
    pd.x = 123.394535;
    pd.y = 41.856486;
    // 区域内的点
//  pd.x = 123.37790583;
//  pd.y = 41.8402475;
        
    List<Point2D.Double> polygon = new ArrayList<Point2D.Double>();

    // 描绘区域的各点坐标
    Point2D.Double point1 = new Point2D.Double();
    point1.x = 123.372104;
    point1.y = 41.84894027;
            
    Point2D.Double point2 = new Point2D.Double();
    point2.x = 123.372104;
    point2.y = 41.8302372;
            
    Point2D.Double point3 = new Point2D.Double();
    point3.x = 123.406135;
    point3.y = 41.84729972;
            
    Point2D.Double point4 = new Point2D.Double();
    point4.x = 123.4055556;
    point4.y = 41.82859638;
            
    Point2D.Double point5 = new Point2D.Double();
    point5.x = 123.36920305;
    point5.y = 41.84680694;
            
    Point2D.Double point6 = new Point2D.Double();
    point6.x = 123.347353;
    point6.y = 41.86403361;
    polygon.add(point1);
    polygon.add(point2);
    polygon.add(point3);
    polygon.add(point4);
    polygon.add(point5);
    polygon.add(point6);
        
    boolean b = checkWithJdkGeneralPath(pd,polygon);
    System.out.println(b);
}
public static boolean checkWithJdkGeneralPath(Point2D.Double point, List<Point2D.Double> polygon) {
    java.awt.geom.GeneralPath p = new java.awt.geom.GeneralPath();

    Point2D.Double first = polygon.get(0);
    p.moveTo(first.x, first.y);

    for (Point2D.Double d : polygon) {
        p.lineTo(d.x, d.y);
    }

    p.lineTo(first.x, first.y);
    p.closePath();

    return p.contains(point);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值