package com.ztmap.project.demo.utils;

import java.util.ArrayList;
import java.util.List;

 class Point {

    /**
     * 判断某一个经纬度点是否在一组经纬度范围内
     *
     * @param ALon A点经度
     * @param ALat A点纬度
     * @param ps   范围多边形经纬度集合
     * @author Klay
     * @date 2023/2/8 18:06
     */
    public static boolean isPtInPoly(double ALon, double ALat, List<JqPoint> ps) {
        int iSum, iCount, iIndex;
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
        if (ps.size() < 3) {
            return false;
        }
        iSum = 0;
        iCount = ps.size();
        for (iIndex = 0; iIndex < iCount; iIndex++) {
            if (iIndex == iCount - 1) {
                dLon1 = ps.get(iIndex).getX();
                dLat1 = ps.get(iIndex).getY();
                dLon2 = ps.get(0).getX();
                dLat2 = ps.get(0).getY();
            } else {
                dLon1 = ps.get(iIndex).getX();
                dLat1 = ps.get(iIndex).getY();
                dLon2 = ps.get(iIndex + 1).getX();
                dLat2 = ps.get(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 Boolean checkCircle(){
        int r=1;
        double x = 116.402544;
        double y = 39.916706;
        double x1 = 116.612963;
        double y2 = 40.0516;

        // 两点间距离公式
        int result=(int)Math.sqrt((x-x1)+(y-y2));
        double sqrt = Math.sqrt((x - x1) * (x - x1) + (y - y2) * (y - y2));

        if(result<=r)return true;

        else return false;
    }



     public static void main(String[] args) {
         Boolean aBoolean = checkCircle();
         System.out.println(aBoolean);
         List<JqPoint> ps = new ArrayList<>();
        JqPoint jqPoint1 = new JqPoint(119.297608,35.394288);
        JqPoint jqPoint2 = new JqPoint(119.300485,35.39499);
        JqPoint jqPoint3 = new JqPoint(119.300559,35.392497);
        JqPoint jqPoint4 = new JqPoint(119.297561,35.392225);
        JqPoint jqPoint5 = new JqPoint(119.297343,35.392892);

        ps.add(jqPoint1);
        ps.add(jqPoint2);
        ps.add(jqPoint3);
        ps.add(jqPoint4);
        ps.add(jqPoint5);
        JqPoint n1 = new JqPoint(119.298758,35.393748);
        JqPoint n2 = new JqPoint(116.300784,39.985292);
        System.out.println("n1:" + isPtInPoly(n1.getX(), n1.getY(), ps));
        System.out.println("n2:" + isPtInPoly(n2.getX(), n2.getY(), ps));
    }
}
