已知两个经纬度坐标点A和B,直线AB作为矩形的中心线,矩形的长等于直线AB的长,矩形的宽等于100m,求矩形四个角点的经纬度,请使用Java写出完整逻辑
时间: 2024-03-16 18:44:25 浏览: 108
好的,根据您的要求,我将以Java语言为例,为您提供完整逻辑。首先,我们需要使用Haversine公式计算出点A和点B之间的距离和方位角,然后根据方位角和距离计算出矩形四个角点的经纬度。
下面是完整代码:
```java
import java.util.*;
public class Rectangle {
// 地球半径,单位米
private static final double EARTH_RADIUS = 6371000;
// 计算距离和方位角
private static double[] calculateDistanceAndBearing(double lat1, double lon1, double lat2, double lon2) {
double[] result = new double[2];
double lat1Rad = Math.toRadians(lat1);
double lat2Rad = Math.toRadians(lat2);
double dLonRad = Math.toRadians(lon2 - lon1);
double y = Math.sin(dLonRad) * Math.cos(lat2Rad);
double x = Math.cos(lat1Rad) * Math.sin(lat2Rad) - Math.sin(lat1Rad) * Math.cos(lat2Rad) * Math.cos(dLonRad);
double distance = Math.sqrt(x * x + y * y) * EARTH_RADIUS;
double bearing = Math.atan2(y, x);
result[0] = distance;
result[1] = bearing;
return result;
}
// 计算矩形四个角点的经纬度
private static List<double[]> calculateRectangle(double lat1, double lon1, double lat2, double lon2) {
List<double[]> result = new ArrayList<>();
double[] distanceAndBearing = calculateDistanceAndBearing(lat1, lon1, lat2, lon2);
double lat1Rad = Math.toRadians(lat1);
double lon1Rad = Math.toRadians(lon1);
double bearingRad = distanceAndBearing[1];
double halfWidth = distanceAndBearing[0] / 2;
double halfHeight = 50;
double latNW = Math.asin(Math.sin(lat1Rad) * Math.cos(halfWidth / EARTH_RADIUS)
+ Math.cos(lat1Rad) * Math.sin(halfWidth / EARTH_RADIUS) * Math.cos(bearingRad));
double lonNW = lon1Rad + Math.atan2(Math.sin(bearingRad) * Math.sin(halfWidth / EARTH_RADIUS) * Math.cos(lat1Rad),
Math.cos(halfWidth / EARTH_RADIUS) - Math.sin(lat1Rad) * Math.sin(latNW));
double latSE = Math.asin(Math.sin(lat2) * Math.cos(halfWidth / EARTH_RADIUS)
- Math.cos(lat2) * Math.sin(halfWidth / EARTH_RADIUS) * Math.cos(bearingRad + Math.PI));
double lonSE = lon2 + Math.atan2(Math.sin(bearingRad + Math.PI) * Math.sin(halfWidth / EARTH_RADIUS) * Math.cos(lat2),
Math.cos(halfWidth / EARTH_RADIUS) - Math.sin(lat2) * Math.sin(latSE));
double latNE = Math.asin(Math.sin(latNW) * Math.cos(halfHeight / EARTH_RADIUS)
+ Math.cos(latNW) * Math.sin(halfHeight / EARTH_RADIUS));
double lonNE = lonNW + Math.atan2(Math.sin(Math.PI / 2 - latNW) * Math.sin(halfHeight / EARTH_RADIUS),
Math.cos(halfHeight / EARTH_RADIUS) - Math.sin(latNW) * Math.sin(latNE));
double latSW = Math.asin(Math.sin(latSE) * Math.cos(halfHeight / EARTH_RADIUS)
- Math.cos(latSE) * Math.sin(halfHeight / EARTH_RADIUS));
double lonSW = lonSE + Math.atan2(Math.sin(Math.PI / 2 - latSE) * Math.sin(halfHeight / EARTH_RADIUS),
Math.cos(halfHeight / EARTH_RADIUS) - Math.sin(latSE) * Math.sin(latSW));
result.add(new double[] { Math.toDegrees(latNW), Math.toDegrees(lonNW) });
result.add(new double[] { Math.toDegrees(latNE), Math.toDegrees(lonNE) });
result.add(new double[] { Math.toDegrees(latSE), Math.toDegrees(lonSE) });
result.add(new double[] { Math.toDegrees(latSW), Math.toDegrees(lonSW) });
return result;
}
public static void main(String[] args) {
double lat1 = 31.204692;
double lon1 = 121.441038;
double lat2 = 31.203485;
double lon2 = 121.442985;
List<double[]> rectangle = calculateRectangle(lat1, lon1, lat2, lon2);
System.out.println("矩形四个角点的经纬度:");
for (double[] point : rectangle) {
System.out.println("(" + point[0] + ", " + point[1] + ")");
}
}
}
```
你可以将点A和点B的经纬度作为输入,该程序将输出矩形四个角点的经纬度。请注意,程序中的经纬度都是以度为单位的,而不是以弧度为单位的。
阅读全文