square root
Posted: 26 Aug 2009 09:33
How to get square root of sth? ( AITile.GetDistanceSquareToTile(...))
The place to talk about Transport Tycoon
https://www.tt-forums.net/
Code: Select all
/*
* Square root using Newton-Raphson method.
*/
::sqrtlut <- [0, 10, 100, 1000, 10000, 100000, 1000000, 10000000];
function sqrt(d) {
if (d == 0.0) {
return 0.0;
}
local nd = 0;
for(nd = 7; nd >= 0; nd--) {
if(nd <= ::sqrtlut[nd]) break;
}
nd++;
local x = pow(2.0, nd);
for(local i = 0; i < 5; i++) {
x = x - (x*x - d) / (2*x);
}
return x;
}
/*
* Raises num to the power p.
*/
function pow(num, p) {
return (p <= 0) ? 1 : num * pow(num, p - 1);
}
Ah! You need manhattan distance. In OpenTTD euclidean distance (i.e. sqrt of square distance) will give an underestimate becuase of the way the map works.Kogut wrote:When I get money (distanse * sth_depending_on_cargo_and_time_of_travel ), distance is manhattan distance or "normal" distance?