package com.sqx.modules.utils;

public class LocationUtils {

    // 计算两点连线与正东方向的夹角（0 - 360度）
    public static double calculateAngle(double[] start, double[] end) {
        double dx = end[1] - start[1];
        double dy = end[0] - start[0];
        double angle = Math.toDegrees(Math.atan2(dy, dx));
        if (angle < 0) {
            angle += 360;
        }
        return angle;
    }

    // 计算两个角度的相似度（0 - 1）
    public static double calculateSimilarity(double angle1, double angle2) {
        double diff = Math.abs(angle1 - angle2);
        if (diff > 180) {
            diff = 360 - diff;
        }
        return 1 - (diff / 180);
    }

    // 计算两组经纬度顺路百分比
    public static double calculateRouteSimilarity(double[] start1, double[] end1, double[] start2, double[] end2) {
        double angle1 = calculateAngle(start1, end1);
        double angle2 = calculateAngle(start2, end2);
        return calculateSimilarity(angle1, angle2) * 100;
    }
}

class Main {
    public static void main(String[] args) {
        double[] start1 = {34.238703, 108.898002};
        double[] end1 = {34.223569, 108.948363};
        double[] start2 = {34.222045, 108.94857};
        double[] end2 = {34.222465, 108.94859};

        double similarity = LocationUtils.calculateRouteSimilarity(start1, end1, start2, end2);
        System.out.println("顺路百分比: " + similarity + "%");
    }

}
