#include <math.h>标签:兩點,int,double,計算,C++,brng,lat,lng,getangle1 From: https://blog.51cto.com/u_13472468/6113667
#include <iostream>
#include <stdio.h>
#define PI 3.141592654
#define EARTH_RADIUS 6378.137 //地球近似半徑
using namespace std;
double get_distance(double lat_b, double lng_b, double lat_a, double lng_a);//lat_b, lng_b為測速點經緯度,lat_a, lng_a為汽車坐標
int getangle1(double lat_b, double lng_b, double lat_a, double lng_a);
int main(int argc, char *argv[])
{
int anglee1 = getangle1(23.384720, 120.354828, 23.379593, 120.353493);//南部16度
cout << anglee1 << endl;
int anglee2 = getangle1(23.00083, 120.220245, 23.001230, 120.215630);//南部99度
cout << anglee2<< endl;
int anglee3 = getangle1(22.86454, 120.2860778, 22.868610, 120.284485);//南部159度
cout << anglee3 << endl;
int anglee4 = getangle1(22.67667, 120.3233194, 22.675587, 120.324677);//南部293度
cout << anglee4 << endl;
int anglee5 = getangle1(25.0284, 121.251423, 25.027662, 121.262108);//北部272度
cout << anglee5 << endl;
int anglee6 = getangle1(24.27358, 120.6931783, 24.2773881, 120.6935169);//中部187度
cout << anglee6 << endl;
int anglee7= getangle1(24.27198, 121.741965, 24.2688548, 121.7408256);//東部19度
cout << anglee7 << endl;
int anglee8 = getangle1(22.769, 121.120615, 22.7711695, 121.1114311);//東部103度
cout << anglee8 << endl;
int anglee9 = getangle1(24.45889, 118.43162, 24.4547165, 118.427789);//金門41度
cout << anglee9 << endl;
int anglee10 = getangle1(23.66487, 119.6, 23.6615943, 119.6000936);// 澎湖356度
cout << anglee10 << endl;
return 0;
}
//兩點的距離(緯度,經度)
double get_distance(double lat_b, double lng_b, double lat_a, double lng_a)
{
double radlat_b = lat_b * PI / 180.0;
double radlat_a = lat_a * PI / 180.0;
double a = radlat_b - radlat_a;//緯度之差
double b = lng_b * PI / 180.0 - lng_a* PI / 180.0; //經度之差
double dst = 2 * asin((sqrt(pow(sin(a / 2), 2) + cos(radlat_b) * cos(radlat_a) * pow(sin(b / 2), 2))));
dst = dst * EARTH_RADIUS;
dst = round(dst * 10000) / 10000;
return dst;
}
//計算兩點的角度
int getangle1(double lat_b, double lng_b, double lat_a, double lng_a)
{
double x = lat_b - lat_a;
double y = lng_b - lng_a;
int brng=-1;
if (y == 0 && x > 0) brng = 0;
if (y == 0 && x < 0) brng = 180;
if(x ==0 && y > 0) brng = 90;
if(x == 0 && y < 0) brng = 270;
if (brng == -1)
{
double dislat = get_distance(lat_b, lng_a, lat_a, lng_a);
double dislng = get_distance(lat_a, lng_b, lat_a, lng_a);
if (x > 0 && y > 0) brng = atan2(dislng, dislat) / PI * 180;
if (x < 0 && y > 0) brng = atan2(dislat, dislng) / PI * 180+90;
if (x < 0 && y < 0) brng = atan2(dislng, dislat) / PI * 180 + 180;
if (x > 0 && y < 0) brng = atan2(dislat, dislng) / PI * 180 + 270;
}
return brng;
}