SQL或PHP错误?距离计算

时间:2011-09-25 00:08:13

标签: php mysql html math

我的文件有问题。我正在制作Travian Clone脚本,我们走得很远。现在我们决定将人工制品添加到游戏中。

目标是向我们所在的当前村庄展示最接近的人工制品。代码为:

function getDistance($coorx1, $coory1, $coorx2, $coory2) {
          $max = 2 * WORLD_MAX + 1;
          $x1 = intval($coorx1);
          $y1 = intval($coory1);
          $x2 = intval($coorx2);
          $y2 = intval($coory2);
          $distanceX = min(abs($x2 - $x1), $max - abs($x2 - $x1));
          $distanceY = min(abs($y2 - $y1), $max - abs($y2 - $y1));
          $dist = sqrt(pow($distanceX, 2) + pow($distanceY, 2));

          return round($dist, 1);
      }


        unset($reqlvl);
        unset($effect);
        $arts = mysql_query("SELECT * FROM ".TB_PREFIX."artefacts WHERE id > 0");
        $rows = array();
        while($row = mysql_fetch_array($arts)) {
            $query = mysql_query('SELECT * FROM `' . TB_PREFIX . 'wdata` WHERE `id` = ' . $row['vref']);
            $coor2 = mysql_fetch_assoc($query);

            $wref = $village->wid;
            $coor = $database->getCoor($wref);
            $dist = getDistance($coor['x'], $coor['y'], $coor2['x'], $coor2['y']);

            $rows[$dist] = $row;

        }
        ksort($rows, SORT_ASC);
        foreach($rows as $row) {
            echo '<tr>';
            echo '<td class="icon"><img class="artefact_icon_'.$row['type'].'" src="img/x.gif" alt="" title=""></td>';
            echo '<td class="nam">';
            echo '<a href="build.php?id='.$id.'">'.$row['name'].'</a> <span class="bon">'.$row['effect'].'</span>';
            echo '<div class="info">';
            if($row['size'] == 1){
                   $reqlvl = 10;
                   $effect = "village";
               }elseif($row['size'] == 2 OR $row['size'] == 3){
                   $reqlvl = 20;
                   $effect = "account";
               }
            echo '<div class="info">Treasury <b>'.$reqlvl.'</b>, Effect <b>'.$effect.'</b>';
            echo '</div></td><td class="pla"><a href="karte.php?d='.$row['vref'].'&c='.$generator->getMapCheck($row['vref']).'">'.$database->getUserField($row['owner'],"username",0).'</a></td>';
            echo '<td class="dist">'.getDistance($coor['x'], $coor['y'], $coor2['x'], $coor2['y']).'</td>';
            echo '</tr>';
        }
?>

但代码似乎错了,因为它显示了所有相同的距离。 14.8给我。我知道我可能有不好的解释,但你可能会理解我的需要。

1 个答案:

答案 0 :(得分:3)

我无法帮助您解决当前的代码,但您可以尝试使用Haversine Formula代替:

// Where: 
// $l1 ==> latitude1 
// $o1 ==> longitude1 
// $l2 ==> latitude2 
// $o2 ==> longitude2 
function haversine ($l1, $o1, $l2, $o2) { 
  $l1 = deg2rad ($l1); 
  $sinl1 = sin ($l1); 
  $l2 = deg2rad ($l2); 
  $o1 = deg2rad ($o1); 
  $o2 = deg2rad ($o2); 

  $distance = (7926 - 26 * $sinl1) * asin (min (1, 0.707106781186548 * sqrt ((1 - (sin ($l2) * $sinl1) - cos ($l1) * cos ($l2) * cos ($o2 - $o1))))); 

  return round($distance, 2);
}  

来自go4expert.com的this post,我过去曾使用过这个功能,发现效果非常好。