选择范围+毕达哥拉斯定理的结果

时间:2009-10-07 23:03:16

标签: php mysql math

我有一个适用于英国人的mysql表,其中包括:

  • 邮政编码开始(即BB2)
  • latitude(int)
  • 经度(int)
  • range(int,in miles,1-20)

http://www.easypeasy.com/guides/article.php?article=64 - 我的表格基于

的sql文件的文章

现在它说我可以使用毕达哥拉斯定理来计算基于经度和纬度的距​​离。

所以我想说我想选择所有在范围内的人(基于他们在范围字段中输入的1-20)开始搜索的邮政编码

例如,假设我搜索“BB2”,我想要一个查询,它将选择所有邮政编码开头为“BB2”的人,以及所有BB2范围内的人(往1-20英里)数据库字段中的范围。)

有些数学专家可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

Google透露this - PERL中的代码,但您可以找出逻辑。

  

基于两个纬度/经度位置计算距离的公式和代码

     

以下是我使用的公式   perl做计算。 Perl的   期望所有的角度都在   弧度。

return &acos(cos($a1)*cos($b1)*cos($a2)*cos($b2) + cos($a1)*sin($b1)*cos($a2)*sin($b2) + sin($a1)*sin($a2)) * $r;


Where:

$a1 = lat1 in radians
$b1 = lon1 in radians
$a2 = lat2 in radians
$b2 = lon2 in radians
$r = radius of the earth in whatever units you want
     

我用于半径的值   地球是:

     

3963.1法定里程   3443.9海里6378公里

     

将十进制度转换为   弧度使用以下perl。

# define an accurate value for PI

$pi = atan2(1,1) * 4;

#
# make sure the sign of the angle is correct for the direction
# West an South are negative angles
#

$degrees = $degrees * -1 if $direction =~ /[WwSs]/;
$radians = $degrees*($pi/180);
     

转换度数分钟和秒   到十进制度使用以下   perl公式。

$dec_deg = $deg + ($min + $sec/60)/60;
     

最后,没有acos功能   perl所以这里是我使用的功能。一世   不记得我在哪里得到数学   对此。

# subroutine acos
#
# input: an angle in radians
#
# output: returns the arc cosine of the angle
#
# description: this is needed because perl does not provide an arc cosine function
sub acos {
    my($x) = @_;
    my $ret = atan2(sqrt(1 - $x**2), $x);        return $ret;
}

答案 1 :(得分:0)

没有它说你可以根据x,y坐标计算距离,而不是经度 - 纬度(使用毕达哥拉斯定理)。

计算2点(x1,y1)和2之间的距离。 (X2,Y2):

d = SquareRoot((x2-x1)^2 + (y2-y1)^2)

使用经度纬度,您可以在此处看到:Calculate distance, bearing and more between two Latitude/Longitude points