double northDistance = distance * Math.cos(Math.toRadians(90 - lat2));是什么意思
时间: 2024-05-27 15:09:22 浏览: 10
这段代码是用来计算地球表面两点之间在北方向(即沿着经线方向)的距离的,其中:
- `distance` 表示地球表面两点之间的大圆距离(即球面距离);
- `lat2` 表示第二个点的纬度,单位为度。
首先,假设地球是一个完美的球体,将地球切成许多小块,每个小块都是一个球冠,球冠的底部是一个小圆,而球冠的侧面是一个圆锥。球冠的半径是地球的半径,球冠的高度是两个点之间在球面上的距离。球冠的面积可以用余弦公式计算。
然后,计算两个点之间的纬度差(即两点之间的纬度角度差),并将其转换为弧度。这个值可以用来计算两个点之间的经线距离,即沿着纬线方向的距离。因为地球是一个球体,所以两个点之间的距离可以通过沿着地球表面的大圆弧线计算。
最后,将大圆距离乘以两个点纬度之间的余弦值,得到两个点之间在北方向(即沿着经线方向)的距离。
相关问题
Java 已知两个坐标经纬度以及两个坐标方向(应该是方向角度数)求两点相交得交点经纬度
这个问题需要用到一些基本的地理学知识和公式,以下是一个简单的 Java 实现:
```java
public class IntersectionPoint {
// 地球半径,单位为米
private static final double EARTH_RADIUS = 6378137;
// 计算两点间的距离,返回单位为米
private static double distance(double lat1, double lng1, double lat2, double lng2) {
double radLat1 = Math.toRadians(lat1);
double radLat2 = Math.toRadians(lat2);
double a = radLat1 - radLat2;
double b = Math.toRadians(lng1) - Math.toRadians(lng2);
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
return s * EARTH_RADIUS;
}
// 计算方向角,返回值为角度数
private static double direction(double lat1, double lng1, double lat2, double lng2) {
double dx = Math.cos(Math.toRadians(lat2)) * Math.sin(Math.toRadians(lng2 - lng1));
double dy = Math.cos(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) -
Math.sin(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.cos(Math.toRadians(lng2 - lng1));
double angle = Math.atan2(dx, dy);
return (Math.toDegrees(angle) + 360) % 360;
}
// 计算交点坐标,返回值为经纬度数组
private static double[] intersection(double lat1, double lng1, double dir1,
double lat2, double lng2, double dir2) {
double d = distance(lat1, lng1, lat2, lng2);
double radDir1 = Math.toRadians(dir1);
double radDir2 = Math.toRadians(dir2);
double radLat1 = Math.toRadians(lat1);
double radLat2 = Math.toRadians(lat2);
double radLng1 = Math.toRadians(lng1);
double radLng2 = Math.toRadians(lng2);
double x = Math.sin(radDir1) * Math.cos(radDir2) * Math.sin(radLat2 - radLat1);
double y = Math.sin(radDir1) * Math.cos(radDir2) * Math.cos(radLat2 - radLat1) * Math.sin(radLng2 - radLng1);
double z = Math.cos(radDir1) * Math.sin(radDir2) * Math.sin(radLat2 - radLat1) * Math.cos(radLng2 - radLng1);
double a = Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2);
double b = 2 * EARTH_RADIUS * (x * (radLng2 - radLng1) + y * (radLat2 - radLat1) + z * (Math.PI / 2 - radLat2));
double c = Math.pow(EARTH_RADIUS, 2) * Math.pow(d, 2) - Math.pow(x * EARTH_RADIUS * (radLng2 - radLng1), 2) -
Math.pow(y * EARTH_RADIUS * (radLat2 - radLat1), 2) - Math.pow(z * EARTH_RADIUS * (Math.PI / 2 - radLat2), 2);
double delta = Math.pow(b, 2) - 4 * a * c;
if (delta < 0) {
return null;
}
double t = (-b + Math.sqrt(delta)) / (2 * a);
double lat = Math.asin(Math.sin(radLat1) * Math.cos(t) + Math.cos(radLat1) * Math.sin(t) * Math.cos(radDir1));
double lng = radLng1 + Math.atan2(Math.sin(t) * Math.sin(radDir1) * Math.sin(radLat1),
Math.cos(t) - Math.sin(radLat1) * Math.sin(lat));
return new double[]{Math.toDegrees(lat), Math.toDegrees(lng)};
}
public static void main(String[] args) {
double lat1 = 39.9042;
double lng1 = 116.4074;
double dir1 = 30;
double lat2 = 31.2304;
double lng2 = 121.4737;
double dir2 = 220;
double[] intersection = intersection(lat1, lng1, dir1, lat2, lng2, dir2);
if (intersection == null) {
System.out.println("No intersection point.");
} else {
System.out.printf("Intersection point: (%f, %f)\n", intersection[0], intersection[1]);
}
}
}
```
以上代码假设输入的经纬度为 WGS84 坐标系下的经纬度,计算结果也是 WGS84 坐标系下的经纬度。如果需要用其他坐标系,需要进行转换。
问题: A地点 给定经纬度a1 b1,B地点 给定经纬度a2 b2.一个车 时速 30公里每小时匀速前进.需要 多久从A开到B? 要求: 基于上面的问题 编写一个java的方法, 定义5个参数 分别是 a1 b1 a2 b2 以及 speed. 结果返回一个时间
### 回答1:
根据给定的经纬度及车速,可以使用以下java方法计算从A地点到B地点所需要的时间:public static double calculateTime(double a1, double b1, double a2, double b2, double speed) {
double distance = Math.sqrt(Math.pow(a2 - a1, 2) + Math.pow(b2 - b1, 2));
double time = distance / speed;
return time;
}
### 回答2:
根据给定的经纬度,在球面上计算A地点到B地点的直线距离。首先,根据经度和纬度计算出A地点和B地点的弧度值。
double lat1 = Math.toRadians(a1);
double lon1 = Math.toRadians(b1);
double lat2 = Math.toRadians(a2);
double lon2 = Math.toRadians(b2);
然后使用Haversine公式计算距离。
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = Math.pow(Math.sin(dlat / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon / 2), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c;
其中,R为地球的半径,取平均值6371公里(注意:单位都是弧度,不是角度)。
最后,根据速度计算时间。
double time = distance / speed;
将以上代码整合到一个Java方法中,代码如下:
public double calculateTime(double a1, double b1, double a2, double b2, double speed) {
double R = 6371.0; // 地球半径,单位:公里
double lat1 = Math.toRadians(a1);
double lon1 = Math.toRadians(b1);
double lat2 = Math.toRadians(a2);
double lon2 = Math.toRadians(b2);
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = Math.pow(Math.sin(dlat / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon / 2), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c;
double time = distance / speed;
return time;
}
调用该方法可以得到从A地点到B地点需要的时间:
double a1 = 40.7128; // A地点的纬度
double b1 = -74.0060; // A地点的经度
double a2 = 34.0522; // B地点的纬度
double b2 = -118.2437; // B地点的经度
double speed = 30; // 车速,单位:公里/小时
double time = calculateTime(a1, b1, a2, b2, speed);
结果中的time即为从A地点到B地点需要的时间,单位为小时。
### 回答3:
根据给定的经纬度和车辆时速,我们可以使用以下的步骤来计算从A地点到B地点所需的时间:
1. 根据经纬度 a1 和 b1,计算出A地点的位置。
2. 根据经纬度 a2 和 b2,计算出B地点的位置。
3. 使用Haversine公式来计算A地点和B地点之间的球面距离。Haversine公式计算如下:
a. 将经纬度转换成弧度制。
b. 使用以下公式计算两个位置之间的球面距离:
distance = 2 * r * arcsin(sqrt(sin((b2 - b1) / 2)^2 + cos(b1) * cos(b2) * sin((a2 - a1) / 2)^2))
其中,r为地球的半径,取平均半径6371公里。
4. 将球面距离转换成时间,根据车辆的时速。时间可以通过以下公式计算:
time = distance / speed
其中,distance为球面距离,speed为车辆的时速。
5. 返回计算所得的时间。
以下是基于上述步骤编写的Java方法:
```java
public static double calculateTime(double a1, double b1, double a2, double b2, double speed) {
double r = 6371.0; // 地球的半径,单位:公里
// 将经纬度转换成弧度制
double radianA1 = Math.toRadians(a1);
double radianB1 = Math.toRadians(b1);
double radianA2 = Math.toRadians(a2);
double radianB2 = Math.toRadians(b2);
// 使用Haversine公式计算两个位置之间的球面距离
double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((radianB2 - radianB1) / 2), 2) + Math.cos(radianB1) * Math.cos(radianB2) * Math.pow(Math.sin((radianA2 - radianA1) / 2), 2)));
// 将球面距离转换成时间
double time = distance / speed;
return time;
}
```
使用该方法,可以传入A地点和B地点的经纬度以及车辆的时速,即可得到从A地点到B地点所需的时间。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)