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;
}
}
}
Java生成两点间经纬度曲线代码
最新推荐文章于 2025-11-26 21:22:25 发布
1195

被折叠的 条评论
为什么被折叠?



