Java 已知两点经纬度以及方向角,不知道距离,判断过两点的直线是否相交,若相交求交点经纬度
时间: 2023-10-02 19:04:19 浏览: 37
判断两条直线是否相交可以使用向量叉积的方法。若两条直线的向量叉积不为零,则相交。
求交点经纬度,可以使用以下步骤:
1. 将两点的经纬度转换为直角坐标系下的三维向量。
2. 根据方向角计算出两个点的方向向量。
3. 将两个向量叉积得到一条新的向量,表示两条直线的交点在地球表面的投影向量。
4. 将该向量转换为经纬度坐标系下的向量。
5. 根据该向量的经纬度坐标,得到交点的经纬度。
以下是 Java 代码实现:
```java
import java.util.Scanner;
public class IntersectionPoint {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 输入点1的经纬度
System.out.print("请输入点1的经度:");
double lon1 = scanner.nextDouble();
System.out.print("请输入点1的纬度:");
double lat1 = scanner.nextDouble();
// 输入点2的经纬度
System.out.print("请输入点2的经度:");
double lon2 = scanner.nextDouble();
System.out.print("请输入点2的纬度:");
double lat2 = scanner.nextDouble();
// 输入方向角
System.out.print("请输入方向角(单位:度):");
double angle = scanner.nextDouble();
// 将经纬度转换为向量
double[] v1 = toCartesian(lon1, lat1);
double[] v2 = toCartesian(lon2, lat2);
// 计算方向向量
double[] d = directionVector(angle);
// 计算两个向量的叉积
double[] crossProduct = crossProduct(v1, v2);
// 计算交点的向量
double[] intersection = crossProduct(crossProduct, d);
// 将交点的向量转换为经纬度
double[] lonlat = toLonLat(intersection);
// 输出交点的经纬度
System.out.println("交点经度:" + lonlat[0]);
System.out.println("交点纬度:" + lonlat[1]);
}
/**
* 将经纬度转换为直角坐标系下的三维向量
*/
public static double[] toCartesian(double lon, double lat) {
double x = Math.cos(Math.toRadians(lat)) * Math.cos(Math.toRadians(lon));
double y = Math.cos(Math.toRadians(lat)) * Math.sin(Math.toRadians(lon));
double z = Math.sin(Math.toRadians(lat));
return new double[]{x, y, z};
}
/**
* 计算方向向量
*/
public static double[] directionVector(double angle) {
double x = Math.cos(Math.toRadians(angle));
double y = Math.sin(Math.toRadians(angle));
double z = 0;
return new double[]{x, y, z};
}
/**
* 计算向量叉积
*/
public static double[] crossProduct(double[] v1, double[] v2) {
double x = v1[1] * v2[2] - v1[2] * v2[1];
double y = v1[2] * v2[0] - v1[0] * v2[2];
double z = v1[0] * v2[1] - v1[1] * v2[0];
return new double[]{x, y, z};
}
/**
* 将向量转换为经纬度坐标系下的向量
*/
public static double[] toLonLat(double[] v) {
double lon = Math.toDegrees(Math.atan2(v[1], v[0]));
double lat = Math.toDegrees(Math.asin(v[2]));
return new double[]{lon, lat};
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)