用c语言实现已知一个点的经纬度、方位角、距离,求另一点经纬度
时间: 2023-08-29 12:09:14 浏览: 210
decode-C,由经纬度计算距离c语言源码,c语言项目
您好!要实现已知一个点的经纬度、方位角和距离,求另一点的经纬度,可以使用以下代码实现:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
#define EARTH_RADIUS 6371.0
typedef struct {
double latitude;
double longitude;
} Point;
Point calculateNewPoint(Point startPoint, double bearing, double distance) {
Point newPoint;
double angularDistance = distance / EARTH_RADIUS;
double startLatRad = startPoint.latitude * PI / 180.0;
double startLonRad = startPoint.longitude * PI / 180.0;
double sinStartLat = sin(startLatRad);
double cosStartLat = cos(startLatRad);
double sinAngularDistance = sin(angularDistance);
double cosAngularDistance = cos(angularDistance);
double endLatRad = asin(sinStartLat * cosAngularDistance +
cosStartLat * sinAngularDistance * cos(bearing));
double endLonRad = startLonRad + atan2(sin(bearing) * sinAngularDistance * cosStartLat,
cosAngularDistance - sinStartLat * sin(endLatRad));
newPoint.latitude = endLatRad * 180.0 / PI;
newPoint.longitude = endLonRad * 180.0 / PI;
return newPoint;
}
int main() {
Point startPoint;
startPoint.latitude = 40.712776;
startPoint.longitude = -74.005974;
double bearing = 45.0;
double distance = 100.0;
Point newPoint = calculateNewPoint(startPoint, bearing, distance);
printf("New Point Latitude: %lf\n", newPoint.latitude);
printf("New Point Longitude: %lf\n", newPoint.longitude);
return 0;
}
```
在上述代码中,我们使用了 Haversine 公式来计算两个经纬度之间的距离。`calculateNewPoint` 函数接受一个起始点 `startPoint`、方位角 `bearing` 和距离 `distance` 作为输入,并返回一个新的点 `newPoint` 的经纬度。
您可以根据需要修改 `startPoint`、`bearing` 和 `distance` 的值,并在程序运行后得到新点的经纬度。请注意,经纬度以度为单位表示。
阅读全文