package com.ruoyi.common.utils.map;


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

public class MapUtils {
    public static final class MapU {
        public static final double EARTH_RADIUS = 6378.137;
        public static final int N_2 = 2;
        public static final int N_1000 = 1000;
        public static final double N_180 = 180.0;
    }

    /**
     * 判断一个点是否在圆形区域内
     * @param pointLon  判断点经度
     * @param pointLat  判断点维度
     * @param centerLon 圆心经度
     * @param centerLat 圆心维度
     * @param radius    半径
     * @return boolean
     */
    public static boolean isInCircle(double pointLon, double pointLat, double centerLon, double centerLat, double radius) {

        return getDistance(pointLon, pointLat, centerLon, centerLat) < radius;
    }


    /**
     * 判断是否在多边形区域内
     *
     * @param pointLon 要判断的点的经度
     * @param pointLat 要判断的点的维度
     * @param lon      区域各顶点的经度数组
     * @param lat      区域各顶点的维度数组
     * @return
     */
    public static boolean isInPolygon(double pointLon, double pointLat, double[] lon, double[] lat) {
        // 将要判断的横纵坐标组成一个点
        Point2D.Double point = new Point2D.Double(pointLon, pointLat);
        // 将区域各顶点的横纵坐标放到一个点集合里面
        List<Point2D.Double> pointList = new ArrayList<>();
        double polygonPoint_x, polygonPoint_y;
        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);
        }
        return areaCheck(point, pointList);
    }

    /**
     * 判断是否在多边形区域内
     *
     * @param point     要判断的点
     * @param polygon   区域点集合
     * @return
     */
    public static boolean areaCheck(Point2D.Double point, List<Point2D.Double> polygon) {
        java.awt.geom.GeneralPath generalPath = new java.awt.geom.GeneralPath();
        Point2D.Double first = polygon.get(0);
        // 通过移动到指定坐标（以双精度指定），将一个点添加到路径中
        generalPath.moveTo(first.x, first.y);
        polygon.remove(0);
        for (Point2D.Double d : polygon) {
            // 通过绘制一条从当前坐标到新指定坐标（以双精度指定）的直线，将一个点添加到路径中。
            generalPath.lineTo(d.x, d.y);
        }
        // 将几何多边形封闭
        generalPath.lineTo(first.x, first.y);
        generalPath.closePath();
        // 测试指定的 Point2D 是否在 Shape 的边界内。
        return generalPath.contains(point);
    }

    /**
     * 获取两个点间的距离
     *
     * @param lonA1 A1点的经度
     * @param latA1 A1点的纬度
     * @param lonA2 A2点的经度
     * @param latA2 A2点的纬度
     * @return
     */
    public static double getDistance(double lonA1, double latA1, double lonA2, double latA2) {
        // 单位(米)
        double lon1 = lonA1 * Math.PI / MapU.N_180;
        double lat1 = latA1 * Math.PI / MapU.N_180;
        double lon2 = lonA2 * Math.PI / MapU.N_180;
        double lat2 = latA2 * Math.PI / MapU.N_180;
        double dlon = lon2 - lon1;
        double dlat = lat2 - lat1;
        double a = Math.pow(Math.sin(dlat / MapU.N_2), MapU.N_2) +
                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon / MapU.N_2), MapU.N_2);
        double c = MapU.N_2 * Math.asin(Math.sqrt(a));
        return c * MapU.EARTH_RADIUS * MapU.N_1000;
    }

    public static boolean isPtInPoly(double ALon , double ALat , Point[] ps) {
        int iSum, iCount, iIndex;
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
        if (ps.length < 3) {
            return false;
        }
        iSum = 0;
        iCount = ps.length;
        for (iIndex = 0; iIndex<iCount;iIndex++) {
            if (iIndex == iCount - 1) {
                dLon1 = ps[iIndex].getX();
                dLat1 = ps[iIndex].getY();
                dLon2 = ps[0].getX();
                dLat2 = ps[0].getY();
            } else {
                dLon1 = ps[iIndex].getX();
                dLat1 = ps[iIndex].getY();
                dLon2 = ps[iIndex + 1].getX();
                dLat2 = ps[iIndex + 1].getY();
            }
            // 以下语句判断A点是否在边的两端点的水平平行线之间，在则可能有交点，开始判断交点是否在左射线上
            if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {
                if (Math.abs(dLat1 - dLat2) > 0) {
                    //得到 A点向左射线与边的交点的x坐标：
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat) ) / (dLat1 - dLat2);
                    // 如果交点在A点左侧（说明是做射线与 边的交点），则射线与边的全部交点数加一：
                    if (dLon < ALon) {
                        iSum++;
                    }
                }
            }
        }
        if ((iSum % 2) != 0) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        //114.331951,30.64091#114.341049,30.610185#114.331436,30.588058#114.312038,30.56393#114.293498,30.558609#114.267922,30.563784#114.231185,30.57945#114.212303,30.601616#114.235649,30.626878#114.280624,30.646818#
        Map[] map=new Map[]{};
        Point[] ps = new Point[] { new Point(114.309914,30.599556),//114.309914,30.599556
                new Point(114.295688,30.592879),//114.295688,30.592879
                new Point(114.292812,30.587726), //114.292812,30.587726
                new Point(114.292812,30.587726), //114.292812,30.587726
                new Point(114.30058,30.580318),//114.30058,30.580318
                new Point(114.303606,30.586959),//114.303606,30.586959
                new Point(114.304534,30.594751),//114.304534,30.594751
                new Point(114.30838,30.590131),//114.30838,30.590131
                new Point(114.308651,30.584182),//114.308651,30.584182
                new Point(114.304495,30.584015),//114.304495,30.584015
                new Point(114.301301,30.578759),//114.301301,30.578759
                new Point(114.309437,30.578528),//114.309437,30.578528
                new Point(114.323282,30.592786)};//114.323282,30.592786
        Point n1 = new Point(114.303217,30.583553);
        Point n2 = new Point(114.307336,30.597592);
        Point n3 = new Point(114.286565,30.590056);
        Point y1 = new Point(114.227342,30.587987);
        Point y2 = new Point(120.1866 , 30.2672);
        Point y4 = new Point(120.1869 , 30.2718);
        System.out.println( "n1:" + isPtInPoly(n1.getX() , n1.getY() , ps));
        System.out.println( "n2:" + isPtInPoly(n2.getX() , n2.getY() , ps));
        System.out.println( "n3:" + isPtInPoly(n3.getX() , n3.getY() , ps));
        System.out.println( "y1:" + isPtInPoly(y1.getX() , y1.getY() , ps));
        System.out.println( "y2:" + isPtInPoly(y2.getX() , y2.getY() , ps));
        System.out.println( "y4:" + isPtInPoly(y4.getX() , y4.getY() , ps));
    }
}
