已知两个经纬度坐标点A和B,直线AB作为矩形的中心线,垂直于AB的边长是100米,求矩形四个角点的经纬度,请使用Java写出完整逻辑
时间: 2024-03-16 18:41:43 浏览: 110
好的,以下是Java代码实现:
```
import java.util.*;
public class RectangleCoordinates {
public static void main(String[] args) {
//坐标点A和B的经纬度
double lonA = 116.407395;
double latA = 39.904211;
double lonB = 116.432464;
double latB = 39.914418;
//计算AB中心点的经纬度
double centerLon = (lonA + lonB) / 2;
double centerLat = (latA + latB) / 2;
//计算垂直于AB的边长
double width = 100;
//计算AB的长度
double length = Math.sqrt(Math.pow(lonB - lonA, 2) + Math.pow(latB - latA, 2));
//计算AB的方向角
double direction = Math.atan2(latB - latA, lonB - lonA);
//计算左上角
double leftTopLon = centerLon - (width / 2) * Math.cos(direction) - (length / 2) * Math.sin(direction);
double leftTopLat = centerLat + (width / 2) * Math.sin(direction) - (length / 2) * Math.cos(direction);
//计算右上角
double rightTopLon = centerLon + (width / 2) * Math.cos(direction) - (length / 2) * Math.sin(direction);
double rightTopLat = centerLat - (width / 2) * Math.sin(direction) - (length / 2) * Math.cos(direction);
//计算右下角
double rightBottomLon = centerLon + (width / 2) * Math.cos(direction) + (length / 2) * Math.sin(direction);
double rightBottomLat = centerLat - (width / 2) * Math.sin(direction) + (length / 2) * Math.cos(direction);
//计算左下角
double leftBottomLon = centerLon - (width / 2) * Math.cos(direction) + (length / 2) * Math.sin(direction);
double leftBottomLat = centerLat + (width / 2) * Math.sin(direction) + (length / 2) * Math.cos(direction);
//输出四个角点的经纬度
System.out.printf("左上角经度:%f,左上角纬度:%f\n", leftTopLon, leftTopLat);
System.out.printf("右上角经度:%f,右上角纬度:%f\n", rightTopLon, rightTopLat);
System.out.printf("右下角经度:%f,右下角纬度:%f\n", rightBottomLon, rightBottomLat);
System.out.printf("左下角经度:%f,左下角纬度:%f\n", leftBottomLon, leftBottomLat);
}
}
```
这个程序输入坐标点 A 和 B 的经纬度,然后计算矩形的四个角点的经纬度,并将结果输出。注意,这个程序没有进行输入合法性检查,需要确保输入的经纬度是合法的。
阅读全文