📄
字号:
typedef struct
{
char btEorW; // 取值为1,东经; 0,西经;
char btNorS; // 取值为1,北纬; 0,南纬;
double dLong; // 经度,取值范围[0.0,180.0)
double dLat; // 纬度,取值范围[0.0,90.0]
}ST_GlobePnt;
const char EAST = 1;
const char WEST = 0;
const char NORTH = 1;
const char SOUTH = 0;
const int EXC_OK = 0; // 0:执行OK,返回值正确;
const int EorW_ERROR = 1; // 1:东经、西经取值错误;
const int NorS_ERROR = 2; // 2:南纬、北纬取值错误;
const int LONG_ERROR = 3; // 3:经度范围越界;
const int LAT_ERROR = 4; // 4: 纬度范围越界;
const int RADIUS_ERROR = 10; // 10:半径取值错误(不能小于0)。
const double ACCURACY = 0.00001; // 精确度
const double PI = 3.14159265359;
const double radius = 1000.0; // 地球半径
//函数实现
//辅助函数int CheckGPnt(const ST_GlobePnt &a_stPnt)
//功能判断点是否符合要求,实现如下:
int CheckGPnt(const ST_GlobePnt &a_stPnt)
{
int iRet = EXC_OK;
// check if east or west
if(a_stPnt.btEorW != EAST && a_stPnt.btEorW != WEST)
{
iRet = EorW_ERROR;
return iRet;
}
// check if nort or south
if(a_stPnt.btNorS != NORTH && a_stPnt.btNorS != SOUTH)
{
iRet = EorW_ERROR;
return iRet;
}
// check the range of Long.
if(a_stPnt.dLong - 180.0 > ACCURACY || a_stPnt.dLong <= - ACCURACY)
{
iRet = LONG_ERROR;
return iRet;
}
// check the range of Lat
if(a_stPnt.dLat - 90.0 > ACCURACY || a_stPnt.dLat <= -ACCURACY)
{
iRet = LAT_ERROR;
return iRet;
}
return iRet;
}
//接口函数如下:
int CalGlobeDistance(const ST_GlobePnt &a_stPntA,
const ST_GlobePnt &a_stPntB,
const double &a_dRadius,
double &a_dDistance)
{
int iRet = EXC_OK;
a_dDistance = -1.0;
double dTheta; // the angle of the two Longs
double dAlpha; // the angle of a_stPntA's Lat
double dBeta; // the angle of a_stPntB's Lat
double dResult; // the angle of the two lines
// which one is through the given point and the center
double dCosResult; // = cos(dResult);
// Check if the given points is OK --Begin-------
iRet = CheckGPnt(a_stPntA);
if(iRet != EXC_OK)
{
return iRet;
}
iRet = CheckGPnt(a_stPntB);
if(iRet != EXC_OK)
{
return iRet;
}
// Check if the given points is OK --End---------
// check the range of Radius
if(a_dRadius < -ACCURACY)
{
iRet = RADIUS_ERROR;
return iRet;
}
dAlpha = a_stPntA.dLat;
dBeta = a_stPntB.dLat;
// Calculate the the angle of the two Longs --- Begin --------
if(a_stPntA.btEorW == a_stPntB.btEorW)
{
dTheta = fabs(a_stPntA.dLong - a_stPntB.dLong);
}else // !=
{
dTheta = a_stPntA.dLong + a_stPntB.dLong;
}
if(dTheta - 180.00 > -ACCURACY)
{
dTheta = 360.0 - dTheta;
}
// Calculate the the angle of the two Longs --- End --------
// Change DEGREE to RADIAN --- Begin ------
dAlpha = dAlpha / 180.0 * PI;
dBeta = dBeta / 180.0 * PI;
dTheta = dTheta / 180.0 * PI;
// Change DEGREE to RADIAN --- End ------
// Calculate the angle of the two lines --- Begin ---------
if(a_stPntA.btNorS != a_stPntB.btNorS)
{
dCosResult = cos(dAlpha) * cos(dBeta) * cos(dTheta) - sin(dAlpha) * sin(dBeta);
}else // !=
{
dCosResult = cos(dAlpha) * cos(dBeta) * cos(dTheta) + sin(dAlpha) * sin(dBeta);
}
dResult = acos(dCosResult);// dResult ranges [0,Pi],needn't change
// Calculate the angle of the two lines --- End ---------
// Get the distance around the globe
a_dDistance = a_dRadius * dResult;
return iRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -