Java生成两点间经纬度曲线代码


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

public class Test1 {


    /**
     * 生成两点间的平面半圆形路径
     * @param startLat 起点纬度
     * @param startLon 起点经度
     * @param endLat 终点纬度
     * @param endLon 终点经度
     * @param numPoints 中间点数
     * @param direction 半圆方向:1=向上(北),-1=向下(南)
     * @return 经纬度点列表
     */
    public static List<GeoPoint> generatePlanarSemicircle(double startLat, double startLon,
                                                          double endLat, double endLon,
                                                          int numPoints, int direction) {
        List<GeoPoint> points = new ArrayList<>();

        // 计算中点(直线中点)
        double midLat = (startLat + endLat) / 2;
        double midLon = (startLon + endLon) / 2;

        // 计算两点间的距离和角度
        double dx = endLon - startLon;
        double dy = endLat - startLat;
        double distance = Math.sqrt(dx * dx + dy * dy);
        double angle = Math.atan2(dy, dx);

        // 添加起点
        points.add(new GeoPoint(startLat, startLon));

        // 生成半圆形路径点
        for (int i = 1; i <= numPoints; i++) {
            double t = (double) i / (numPoints + 1);

            // 直线上的位置
            double linearLon = startLon + dx * t;
            double linearLat = startLat + dy * t;

            // 半圆偏移量
            double semicircleOffset = distance / 2 * Math.sin(Math.PI * t);

            // 计算垂直方向(顺时针旋转90度)
            double offsetX = -dy * direction;
            double offsetY = dx * direction;

            // 归一化垂直向量
            double length = Math.sqrt(offsetX * offsetX + offsetY * offsetY);
            offsetX /= length;
            offsetY /= length;

            // 应用半圆偏移
            double semicircleLon = linearLon + offsetX * semicircleOffset;
            double semicircleLat = linearLat + offsetY * semicircleOffset;

            points.add(new GeoPoint(semicircleLat, semicircleLon));
        }

        // 添加终点
        points.add(new GeoPoint(endLat, endLon));

        return points;
    }

    public static void main(String[] args) {
        // 示例:从A点到B点的半圆形路径
        double startLat = 40.7128;
        double startLon = -74.006;
        double endLat = 40.395628571;
        double endLon = -76.112557143;

        int numPoints = 10;
        int direction = -1; // 1=向上(北),-1=向下(南)

        List<GeoPoint> path = generatePlanarSemicircle(startLat, startLon,
                endLat, endLon,
                numPoints, direction);

        // 输出生成的路径点
        System.out.println("Generated Planar Semicircle Path Points:");
        for (GeoPoint point : path) {
//            System.out.printf("Lat: %.6f, Lon: %.6f%n", point.latitude, point.longitude);
            System.out.printf( point.longitude+","+point. latitude+",0"+"\n");
        }

        // 可视化检查(可以在地图工具中绘制这些点)
//        System.out.println("\nVisualization coordinates:");
//        for (GeoPoint point : path) {
//            System.out.printf("[%.6f, %.6f],%n", point.longitude, point.latitude);
//        }
    }

    static class GeoPoint {
        double latitude;
        double longitude;

        public GeoPoint(double latitude, double longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值