This was bloody annoying to do... I shouldn't have neglected my math studies... but this seems to work, generally speaking, even though it's not perfect mathematically, and the code is a bit ugly as well.
Lua Code:
function smooth_helper (a, b, weight)
-- Assuming both a and b are in 0..359 range
local direction = 1
if (a-b)%360 < 180 then
direction = -1
end
local delta = math.abs(a-b) % 360
if delta > 180 then
delta = 360 - delta
end
return (a + direction * delta * weight) % 360
end
function round (n)
return math.floor(n+0.5)
end
function smooth (tbl)
ret = {}
local avg = 0
-- To make the averaging less ugly, make a padded copy of tbl
local tmp = {tbl[1], tbl[1]}
for i=1,#tbl do
table.insert (tmp, tbl[i])
end
table.insert (tmp, tbl[#tbl])
table.insert (tmp, tbl[#tbl])
for i=3,#tbl+2 do
avg = tmp[i]
avg = smooth_helper (avg, tmp[i-1], 0.15)
avg = smooth_helper (avg, tmp[i+1], 0.15)
avg = smooth_helper (avg, tmp[i-2], 0.05)
avg = smooth_helper (avg, tmp[i+2], 0.05)
table.insert (ret, round(avg))
end
return ret
end
You can play around with the weights, naturally, and I suspect that changing the order of those smoothing lines would change the outcome in a way that would make a mathematician cringe
[Edit]
A mathematician, or someone who takes their computer graphics seriously, would have done it with vectors