Re: NPC chasing the player
Posted: Sun Feb 13, 2011 12:24 am
Aha, I didn't know about unpack. Very handy. So here's what I have now:
Anyway, I tested that against trig. Trig was about 3 times as fast as the list-using normalizer, and about 1.5 times as fast as the table-using normalizer.
Code: Select all
-- Normalizes a list of numbers
function math.normalize(...)
local t,m,n = {}, 0, select('#',...)
for i=1, n do local x=select(i,...) t[i],m=x,m+x end
m=1/m
for i=1, n do t[i] = t[i]*m end
return unpack(t)
end
-- Normalizes a TABLE of numbers (faster, but more annoying to use...)
function math.normalize(t) local n,m = #t,0 for i=1,n do m=m+t[i] end m=1/m for i=1,n do t[i]=t[i]*m end return t end