-- voronoi diagram in regular grid -- 1. create a grid of some seed_window size -- 2. randomize x,y position inside grid cell and get r,g,b of image -- 3. calculate voronoi diagram and draw resultant image -- author: Sinisa Petric, (pixopedia@sigmapi-design.com), 2008 -- uses Pixopedia_24 vars and functions for images image_OUT = 0 image_INP = 1 r_seed = {} g_seed = {} b_seed = {} x_seed = {} y_seed = {} seed_window = 10 seed_dither = 10 -- seed dither must be less or equal seed_window -- generate random points inside image and get pixels rgb function generate_seeds() local x, y, ii, jj math.randomseed( os.time() ) ii = 0 for y = 0, vp24_height-1, seed_window do ii = ii + 1 x_seed[ii] = {} y_seed[ii] = {} r_seed[ii] = {} g_seed[ii] = {} b_seed[ii] = {} jj = 0 for x = 0, vp24_width-1, seed_window do jj = jj + 1 y_seed[ii][jj] = y + math.random(seed_dither) - 1 x_seed[ii][jj] = x + math.random(seed_dither) - 1 if y_seed[ii][jj] > vp24_height-1 then y_seed[ii][jj] = vp24_height-1 end if x_seed[ii][jj] > vp24_width-1 then x_seed[ii][jj] = vp24_width-1 end r_seed[ii][jj], g_seed[ii][jj], b_seed[ii][jj] = fp24_getImageRGB(image_INP, x_seed[ii][jj], y_seed[ii][jj]) end end return ii, jj end -- find closest seed function find_closest(x, y) local dist, min_dist, iis, jjs local row, col min_dist = 9999.0 row = math.floor(y / seed_window) + 1 col = math.floor(x / seed_window) + 1 if row >= max_i then row = max_i - 1 end if col >= max_j then col = max_j - 1 end if row <= 1 then row = 2 end if col <= 1 then col = 2 end iis = row jjs = col for ii = row-1, row + 1 do for jj = col-1,col + 1 do dist = math.sqrt((x-x_seed[ii][jj])^2+(y-y_seed[ii][jj])^2) if dist < min_dist then min_dist = dist iis = ii jjs = jj end end end return r_seed[iis][jjs], g_seed[iis][jjs], b_seed[iis][jjs] end -- and now the main routine max_i, max_j = generate_seeds() for y = 0, vp24_height-1 do for x = 0, vp24_width-1 do r,g,b = find_closest(x,y) fp24_setImageRGB(image_OUT, x, y, r, g, b) end fp24_showProgress(y, vp24_height) end