package com.lutao.cloud.oil.util;

/**
 * @description:
 * @author: vincent.zhao
 * @create: 2024-09-04 15:49
 **/
public class DoubleUtils {

    /**
     * 计算两个坐标点的用时
     *
     * @param lat1  起始坐标点横坐标
     * @param lon1  起始坐标点纵坐标
     * @param lat2  结束坐标点横坐标
     * @param lon2  结束坐标点纵坐标
     * @param speed 速度 以km计算
     * @author xudong
     * @since 2024-07-25
     */
    public static double calculateTravelTime(double lat1, double lon1, double lat2, double lon2, double speed) {
        return calculateDistance(lat1, lon1, lat2, lon2) / speed;
    }

    public static Double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        final double R = 6371; // Radius of the earth in km
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = R * c; // Distance in km
        return distance;
    }
}
