|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
[appleeaters]Distance Formula
The distance formula used in the library and in sample code is incorrect...
Although the following is the standard distance formula (represented by "sqrt((x1-x2)^2 + (y1-y2)^2)") it does not take in to account that diagonal moves are the same distances (number of moves) as verticle and horizontal moves... (you are using the pythagorean theorem to compute distanced walked which assigns a value of 1 to each horizontal moves and a value of sqrt(2) for each diagonal move). that is: it fails to show the distance beteen "(1,1) (1,2)" is the same distance as "(1,1) (2,2)". Here is the line used in question: $distance = sqrt(pow($robot[0]-$this->game["player"][0],2)+pow($robot[1]-$this->game["player"][1],2)); Instead, try max(abs(x1-x2), abs(y1-x2)) which assumes one will walk diagonal when it makes sense... or: $distance = max(abs($robot[0]-$game["player"][0]), abs($robot[1]-$game["player"][1])); it's fast and cheap too! Pax, Keith |
|
#2
|
|||
|
|||
|
[appleeaters]RE: Distance Formula
You're right, that does make more sense. However, the distance calculation that the library does should not affect any distance calculations that your script does. You can't use that calculation that the library does anyway since it's not mentioned in the rules.
Anyway, you're right the library should probably be updated. BTW: Mind if I use your formula in my script? |
|
#3
|
|||
|
|||
|
[appleeaters]RE: Distance Formula
Thanks for pointing that out, Keith!
Yes, it would be nice if the library was updated so that it reports the distance to the closest robot/apple correctly. - Chris B, chris@mretc.net |
|
#4
|
|||
|
|||
|
[appleeaters]RE: Distance Formula
hehe... or I could just update my own copy of the library and be happy 8-)
- Chris B |
|
#5
|
|||
|
|||
|
[appleeaters]RE: Distance Formula
That's what I did
|
![]() |
| Viewing: Codewalkers Forums > PHP Contests > Older Contests > [appleeaters]Distance Formula |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|