1

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 && ((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.

4
  • 1
    Can you provide the return results of both? Commented Sep 15, 2011 at 19:14
  • 3
    That is not vanilla JavaScript. Perhaps ActionScript? Commented Sep 15, 2011 at 19:16
  • Agreed with Matt, Please confirm your code because Its not working from my fiddle... Commented Sep 15, 2011 at 19:18
  • personally, I am confused why the javascript would return 20.80 for an int Commented Sep 15, 2011 at 19:22

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 mostly for the ternary hate. One of my coworkers, otherwise an awesome guy whose code is always great and easy to follow, loves throwing in ternary operators. /pet peeve
ternary operators are awesome - they just suck if the check's get too long. I defy you to find an easier way to, say, clamp a value: value = ( value < 0.0 ) ? 0.0 : ( value > 1.0 ) ? 1.0 : value;
@divillysausages - that's pretty neat, but still nearly impossible to interpret without knowing ahead of time what it does. When you spend a lot of time debugging somebody else's code, you begin to appreciate simplicity/readability ahead of code size efficiency.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.