View Single Post
02-11-14, 03:11 PM   #14
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
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:
  1. function smooth_helper (a, b, weight)
  2.    -- Assuming both a and b are in 0..359 range
  3.    local direction = 1
  4.    if (a-b)%360 < 180 then
  5.       direction = -1
  6.    end
  7.    
  8.    local delta = math.abs(a-b) % 360
  9.    if delta > 180 then
  10.       delta = 360 - delta
  11.    end
  12.    
  13.    return (a + direction * delta * weight) % 360
  14. end
  15.  
  16. function round (n)
  17.    return math.floor(n+0.5)
  18. end
  19.  
  20. function smooth (tbl)
  21.    ret =  {}
  22.    local avg = 0
  23.    
  24.    -- To make the averaging less ugly, make a padded copy of tbl
  25.    local tmp = {tbl[1], tbl[1]}
  26.    for i=1,#tbl do
  27.       table.insert (tmp, tbl[i])
  28.    end
  29.    table.insert (tmp, tbl[#tbl])
  30.    table.insert (tmp, tbl[#tbl])
  31.    
  32.    for i=3,#tbl+2 do
  33.       avg = tmp[i]
  34.       avg = smooth_helper (avg, tmp[i-1], 0.15)
  35.       avg = smooth_helper (avg, tmp[i+1], 0.15)
  36.       avg = smooth_helper (avg, tmp[i-2], 0.05)
  37.       avg = smooth_helper (avg, tmp[i+2], 0.05)
  38.      
  39.       table.insert (ret, round(avg))
  40.    end
  41.    
  42.    return ret
  43. 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
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!

Last edited by Malsomnus : 02-11-14 at 03:22 PM.
  Reply With Quote