王朝网络
分享
 
 
 

球面两点间的球面距离的计算(2)

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

球面两点间的球面距离的计算(2)

1.计算

上篇中提到了两点间球面距离的计算的理论基础,在这篇中,进行计算。

同样,假设点A的经度为东经a1度,纬度为北纬b1度;点B的经度为东经a2度,纬度为南纬b2度。

所以α = b1, β = b2, 设∠ACE的值为θ,那么θ = |a1 – a2|。

可以计算出

|OC| = R * sin(α) |OD| = R * sin(β)

|AC| = R * cos(α) |BD| = R * cos(β)

由于|CE| = |BD|,|CD| = |OD| + |OC|

由余弦定理可以知道

|AE|2 = |AC|2 + |CE|2 – 2 * |AC| *|CE| * cos(θ)

ð |AE|2 = R2 * cos2 (α) + R2 * cos2 (β) - 2 * R2 * cos(α)cos(β) * cos(θ)

|BE| = |CD|

由|AB|2 = |AE|2 + |BE|2

ð |AB|2 = R2 * cos2 (α) + R2 * cos2 (β) - 2 * R2 * cos(α)cos(β) * cos(θ) + R2 * sin2 (α) + R2 * sin2 (β) + 2 * R2 * sin(α) sin(β)

ð |AB|2 = 2R2 (1 + sin(α) sin(β) - cos2 (α) cos2 (β) cos(θ))

在三角形AOB中,亦可用余弦定理得到

ð |OA|2 + |OB|2– 2 |OA| * |OB| * cos(AOB)

ð cos(AOB) = cos(α) * cos(β) * cos(θ) - sin(α) sin(β)

这样可以求出AOB的值(单位为弧度),最后球面距离为R * (AOB)。

完毕,呵呵。

注意到,

(1)当A、B同为东经或同为西经时,θ = |a1 – a2|,那么当 A、B不是同为东经或同为西经时,θ = |a1 + a2|,若θ > 180度,θ = 360 -θ(假设单位是角度,而不是弧度)。

(2)当A、B同为南半球或同为北半球时,|CD| = | |OC| - |OD| |,当A、B在不同的南北半球时,才是|CD| = |OC| + |OD|。

2.编程实现

(1)函数接口定义

int CalGlobeDistance(const ST_GlobePnt a_stPntA,

const ST_GlobePnt a_stPntB,

const double a_dRadius,

double &a_dDistance)

返回值:

0:执行OK,返回值正确;

1:a_stPntA 或a_stPntB 的东经、西经取值错误;

2:a_stPntA 或a_stPntB的南纬、北纬取值错误;

3:a_stPntA 或a_stPntB的经度范围越界;

4:a_stPntA 或a_stPntB的纬度范围越界;

10:半径取值错误(不能小于0)。

参数:

a_stPntA

给定的第一个点的坐标,关于结构体ST_GlobePnt的请参照以下的ST_GlobePnt定义;

a_stPntB

给定的第二个点的坐标,关于结构体ST_GlobePnt的请参照以下的ST_GlobePnt定义;

a_dRadius

给定的球体的半径,取值不能小于0;

a_dDistance

最后求得的球面距离,如果在计算过程中出现错误,则其值为负数。

结构体ST_GlobePnt

typedef struct

{

byte btEorW; // 取值为1,东经; 0,西经;

byte btNorS; // 取值为1,北纬; 0,南纬;

double dLong; // 经度,取值范围[0.0,180.0)

double dLat; // 纬度,取值范围[0.0,90.0]

}ST_GlobePnt;

(2)程序代码

常量定义

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;

函数实现

辅助函数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;

}

(3)补充说明

利用程序计算,可能存在一定的误差,减少误差的方法可能用到计算方法中提到的算法,呼呼,先告一段落,暂不考虑这个吧。

(The End)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有