java 根据两个点,生成环绕地球一圈的环

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

public class GreatCircleGenerator {

    public static void main(String[] args) {
        // 示例:北京(116.4, 39.9) 和 纽约(-74.0, 40.7)
        double lon1 = 116.4, lat1 = 39.9;
        double lon2 = -74.0, lat2 = 40.7;
        
        List<double[]> circle = generateGreatCircle(lon1, lat1, lon2, lat2, 360);
        System.out.println("生成了 " + circle.size() + " 个点");
        for (double[] point : circle) {
            System.out.printf("经度: %.6f, 纬度: %.6f%n", point[0], point[1]);
        }
    }

    /**
     * 生成通过两个点的大圆路径
     * @param lon1 起点经度(度)
     * @param lat1 起点纬度(度)
     * @param lon2 终点经度(度)
     * @param lat2 终点纬度(度)
     * @param numPoints 生成的总点数
     * @return 经纬度坐标列表,每个元素为[经度, 纬度]
     */
    public static List<double[]> generateGreatCircle(
            double lon1, double lat1, 
            double lon2, double lat2, 
            int numPoints) {
        
        // 转换为弧度
        double lambda1 = Math.toRadians(lon1);
        double phi1 = Math.toRadians(lat1);
        double lambda2 = Math.toRadians(lon2);
        double phi2 = Math.toRadians(lat2);
        
        // 计算起点和终点的直角坐标
        double[] p1 = toCartesian(lambda1, phi1);
        double[] p2 = toCartesian(lambda2, phi2);
        
        // 计算大圆平面的法向量
        double[] normal = crossProduct(p1, p2);
        double norm = norm(normal);
        
        // 处理对跖点(法向量为零的情况)
        if (norm < 1e-10) {
            double[] arbitrary = {1, 0, 0};
            if (Math.abs(dotProduct(p1, arbitrary)) > 0.9) {
                arbitrary = new double[]{0, 1, 0};
            }
            normal = crossProduct(p1, arbitrary);
            norm = norm(normal);
        }
        normalize(normal); // 归一化法向量
        
        // 计算旋转轴和参考向量
        double[] u = crossProduct(normal, p1);
        normalize(u);
        
        List<double[]> points = new ArrayList<>();
        double angleStep = 2 * Math.PI / numPoints;
        
        for (int i = 0; i <= numPoints; i++) {
            double theta = i * angleStep;
            // 计算大圆上的点
            double x = p1[0] * Math.cos(theta) + u[0] * Math.sin(theta);
            double y = p1[1] * Math.cos(theta) + u[1] * Math.sin(theta);
            double z = p1[2] * Math.cos(theta) + u[2] * Math.sin(theta);
            
            // 转换为经纬度
            double[] latLon = toLatLon(x, y, z);
            points.add(latLon);
        }
        
        return points;
    }

    // 球面坐标转直角坐标
    private static double[] toCartesian(double lambda, double phi) {
        double x = Math.cos(phi) * Math.cos(lambda);
        double y = Math.cos(phi) * Math.sin(lambda);
        double z = Math.sin(phi);
        return new double[]{x, y, z};
    }

    // 直角坐标转球面坐标
    private static double[] toLatLon(double x, double y, double z) {
        double lambda = Math.atan2(y, x);
        double phi = Math.atan2(z, Math.sqrt(x * x + y * y));
        return new double[]{Math.toDegrees(lambda), Math.toDegrees(phi)};
    }

    // 向量叉积
    private static double[] crossProduct(double[] a, double[] b) {
        return new double[]{
            a[1] * b[2] - a[2] * b[1],
            a[2] * b[0] - a[0] * b[2],
            a[0] * b[1] - a[1] * b[0]
        };
    }

    // 向量点积
    private static double dotProduct(double[] a, double[] b) {
        return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
    }

    // 向量归一化
    private static void normalize(double[] v) {
        double norm = Math.sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
        v[0] /= norm;
        v[1] /= norm;
        v[2] /= norm;
    }

    // 向量模长
    private static double norm(double[] v) {
        return Math.sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值