02-23-2012, 01:55 PM
There's a much faster distance formula, which doesn't require square roots, albeit it's not as precise, but it works. It's what I use in my games - I can't afford a square root per frame so I just...
Roughly translated from C code, but should work:
Code:
Function distance (x1 as uByte, y1 as uByte, x2 as uByte, y2 as uByte)
dim dx as uByte
dim dy as uByte
dim mn as uByte
dx = Abs (x2 - x1)
dy = Abs (y2 - y1)
If dx < dy Then
mn = dx
Else
mn = dy
End If
Return (dx + dy - (mn >> 1) - (mn >> 2) + (mn >> 4))
End Function
Roughly translated from C code, but should work:
Code:
unsigned char distance (unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2) {
unsigned char dx = abs (x2 - x1);
unsigned char dy = abs (y2 - y1);
unsigned char mn = dx < dy ? dx : dy;
return (dx + dy - (mn >> 1) - (mn >> 2) + (mn >> 4));
}