开发者

SQL or PHP mistake? Distance calculation

开发者 https://www.devze.com 2023-04-07 14:06 出处:网络
I have a problem with my file. I\'m making Travian Clone script and we went really far. Now we decided to add artefacts into game.

I have a problem with my file. I'm making Travian Clone script and we went really far. Now we decided to add artefacts into game.

Goal is to show closest artefacts to the current village we are in. The code is:

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>';
        }
?>

but the code seems to be wrong because its showing all same distances. 14.8 to me. I know i maybe have bad explanation but u will probably understand what I need.


I can't help with your current code I'm afraid but you could try using the Haversine Formula instead:

// 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);
}  

Credit goes to this post on go4expert.com, I've used this function in the past and found it works very well.

0

精彩评论

暂无评论...
验证码 换一张
取 消