Java或Android中计算某点是否在多边形中

public boolean isPointInPolygon(double px, double py, ArrayList<Double> polygonXA, ArrayList<Double> polygonYA) {
    boolean isInside = false;
    double ESP = 1e-9;
    int count = 0;
    double linePoint1x;
    double linePoint1y;
    double linePoint2x = 180;
    double linePoint2y;
    linePoint1x = px;
    linePoint1y = py;
    linePoint2y = py;
    for (int i = 0; i < polygonXA.size() - 1; i++) {
        double cx1 = polygonXA.get(i);
        double cy1 = polygonYA.get(i);
        double cx2 = polygonXA.get(i + 1);
        double cy2 = polygonYA.get(i + 1);
        if (isPointOnLine(px, py, cx1, cy1, cx2, cy2)) {
            return true;
        }
        if (Math.abs(cy2 - cy1) < ESP) {
            continue;
        }

        if (isPointOnLine(cx1, cy1, linePoint1x, linePoint1y, linePoint2x, linePoint2y)) {
            if (cy1 > cy2)
                count++;
        } else if (isPointOnLine(cx2, cy2, linePoint1x, linePoint1y, linePoint2x, linePoint2y)) {
            if (cy2 > cy1)
                count++;
        } else if (isIntersect(cx1, cy1, cx2, cy2, linePoint1x, linePoint1y, linePoint2x, linePoint2y)) {
            count++;
        }
    }
    if (count % 2 == 1) {
        isInside = true;
    }
    return isInside;
}
public static double Multiply(double px0, double py0, double px1, double py1, double px2, double py2) {
    return ((px1 - px0) * (py2 - py0) - (px2 - px0) * (py1 - py0));
}
public boolean isPointOnLine(double px0, double py0, double px1, double py1, double px2, double py2) {
    boolean flag = false;
    double ESP = 1e-9;
    if ((Math.abs(Multiply(px0, py0, px1, py1, px2, py2)) < ESP) && ((px0 - px1) * (px0 - px2) <= 0)
            && ((py0 - py1) * (py0 - py2) <= 0)) {
        flag = true;
    }
    return flag;
}
public boolean isIntersect(double px1, double py1, double px2, double py2, double px3, double py3, double px4,
                           double py4) {
    boolean flag = false;
    double d = (px2 - px1) * (py4 - py3) - (py2 - py1) * (px4 - px3);
    if (d != 0) {
        double r = ((py1 - py3) * (px4 - px3) - (px1 - px3) * (py4 - py3)) / d;
        double s = ((py1 - py3) * (px2 - px1) - (px1 - px3) * (py2 - py1)) / d;
        if ((r >= 0) && (r <= 1) && (s >= 0) && (s <= 1)) {
            flag = true;
        }
    }
    return flag;
}
下面方法调用:
public void isInply(){
    double px = 113.705835;  
    double py = 34.787479;  
    ArrayList<Double> polygonXA = new ArrayList<Double>();  
    ArrayList<Double> polygonYA = new ArrayList<Double>();  
    polygonXA.add(113.700213);// 北京    
    polygonXA.add(113.706651);// 石家庄    
    polygonXA.add(113.706608);// 德州    
    polygonXA.add(113.707337);// 天津    
    polygonXA.add(113.701587);// 烟台    
    polygonXA.add(113.700128);// 唐山jdal    
    polygonYA.add(34.791532);  
    polygonYA.add(34.791568);  
    polygonYA.add(34.789242);  
    polygonYA.add(34.786633);  
    polygonYA.add(34.786669);  
    polygonYA.add(34.789242); 
    boolean isFlag = isPointInPolygon(px,py,polygonXA,polygonYA);
}
 
出自:http://blog.163.com/kangle0925@126/blog/static/27758198201181484115912/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值