rewriting a ActionScript function to php
i am trying to write a ActionScript function with some math to php, its not coming out with the same results, could you give me a hand. This function calculates the distance between 2 coordinates for a hex map game.
The ActionScript funcion:
function tellDistance(p1:Point,p2:Point,debug:Boolean=false):Number{
var dx:int=Math.abs(p1.x-p2.x);
var dy:int=Math.abs(p1.y-p2.y);
var auxY:int=dy;
dy=Math.max(dy-Math.ceil(dx*.5),0);
dy+=(auxY>=dx*.5 && dx%2==1 &am开发者_开发百科p;& ((p1.x%2==1 && p1.y>p2.y) || (p1.x%2==0 && p1.y<p2.y)))?1:0;
return dx+dy;
}
My version in php (broken):
function tiles_distance($start_x, $start_y, $dest_x, $dest_y)
{
$x_dif = abs($start_x-$dest_x);
$y_dif = abs($start_y-$dest_y);
$y_dif_backup = intval($y_dif);
$y_dif = max($y_dif-ceil($x_dif*0.5),0);
$y_dif = $y_dif+($y_dif_backup>=$x_dif*.5 && $x_dif%2==1 && (($start_x%2==1 && $start_y>$dest_y) || ($start_x%2==0 && $start_y< $dest_y)))?1:0;
return $x_dif+$y_dif;
}
EDIT:
on ActionScript the coords would look like 20.80 and 32.81. on php i am giving the x and y coordinates separated. They would return an int value like 1 or 40.
It is probably because in your javascript you are immediately casting to int
whereas in PHP you are not. The PHP code is probably giving you more accurate answers.
Also, I'm going to give you my favorite quote:
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." - Brian Kernighan
Cut out that ternary if
statement crap so you can see what's going on.
精彩评论