java 根据经纬度判断定位是否在电子围栏内.

定位点要按顺时针或者逆时针顺序上传

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
 
public class PolygonUtil {
    /**
     * 判断点是否在多边形内
     * @Title: IsPointInPoly
     * @Description: TODO()
     * @param point 测试点
     * @param pts 多边形的点
     * @return
     * @return boolean
     * @throws
     */
    public static boolean isInPolygon(Point2D.Double point, List<Point2D.Double> pts){
        int N = pts.size();
        boolean boundOrVertex = true;
        int intersectCount = 0;//交叉点数量
        double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
        Point2D.Double p1, p2;//临近顶点
        Point2D.Double p = point; //当前点
 
        p1 = pts.get(0);
        for(int i = 1; i <= N; ++i){
            if(p.equals(p1)){
                return boundOrVertex;
            }
 
            p2 = pts.get(i % N);
            if(p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)){
                p1 = p2;
                continue;
            }
 
            //射线穿过算法
            if(p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)){
                if(p.y <= Math.max(p1.y, p2.y)){
                    if(p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)){
                        return boundOrVertex;
                    }
 
                    if(p1.y == p2.y){
                        if(p1.y == p.y){
                            return boundOrVertex;
                        }else{
                            ++intersectCount;
                        }
                    }else{
                        double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;
                        if(Math.abs(p.y - xinters) < precision){
                            return boundOrVertex;
                        }
 
                        if(p.y < xinters){
                            ++intersectCount;
                        }
                    }
                }
            }else{
                if(p.x == p2.x && p.y <= p2.y){
                    Point2D.Double p3 = pts.get((i+1) % N);
                    if(p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)){
                        ++intersectCount;
                    }else{
                        intersectCount += 2;
                    }
                }
            }
            p1 = p2;
        }
        if(intersectCount % 2 == 0){//偶数在多边形外
            return false;
        } else { //奇数在多边形内
            return true;
        }
    }
    public static void main(String[] args) {
        Point2D.Double point = new Point2D.Double();
        point.setLocation(116.52759064648436,39.88327456483143);
        List<Point2D.Double> pts = new ArrayList<>();
        Point2D.Double point1 = new Point2D.Double();
        point1.setLocation(116.48965348217772,39.90816602515441);
        Point2D.Double point2 = new Point2D.Double();
        point2.setLocation(116.54338349316404,39.9097461301072);
        Point2D.Double point3 = new Point2D.Double();
        point3.setLocation(116.54836167309568,39.8699690729595);
        Point2D.Double point4 = new Point2D.Double();
        point4.setLocation(116.4889668366699,39.87181355267268);
        pts.add(point1);
        pts.add(point2);
        pts.add(point3);
        pts.add(point4);
        boolean inPolygon = isInPolygon(point,pts);
        System.out.println(inPolygon);
    }
 
}
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐