So I was still wondering if there’s a better way around this annoying lack of efficient flood fill algorithms for the iPad i decided to make my own little idea come in to action, what it does is it takes the touch point then fires 200 tracers in a circle around the touch point and it works its way out til they all set, it then fills the area with a mesh consisting of all the 200 points, this is aimed at full screen images but works for others… Touch to fill and watch the progress
Of course this only really works well with regular polygons
Anyway try it out:
--# Main
-- TestProj
-- Use this function to perform your initial setup
function setup()
screen = image(WIDTH,HEIGHT)
setContext(screen)
pushStyle()
background(40,40,50,255)
fill(255,255)
ellipse(WIDTH/2,HEIGHT/2,400)
popStyle()
setContext()
fil = nil
end
function touched(t)
if t.state == BEGAN then
local r,g,b,a = screen:get(t.x,t.y)
fil = Fill(screen,vec2(t.x,t.y),color(0,255,255),color(r,g,b))
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
sprite(screen,WIDTH/2,HEIGHT/2)
if fil ~= nil then
fil:draw()
end
-- Do your drawing here
end
--# Fill
Fill = class()
function Fill:init(img,pos,fillColor,intColor)
self.img = img
self.pos = pos
self.fillcol = fillColor
self.intcol = intColor
if self.intcol == self.fillcol then return end
self.num = 200
self.int = (math.pi*2)/self.num
self.tracker = {}
for i=1,self.num do
local tbl = {}
tbl.dir = vec2(0,1):rotate(self.int*i)
tbl.pos = self.pos
tbl.set = false
table.insert(self.tracker,tbl)
end
self.tick = 0
self.m = mesh()
self.m:setColors(self.fillcol.r,self.fillcol.g,self.fillcol.b,255)
end
function Fill:draw()
local setall = true
for k,v in pairs(self.tracker) do
pushStyle()
fill(255,0,0)
ellipse(v.pos.x,v.pos.y,10)
popStyle()
local s = v.pos+v.dir
local sx,sy = s.x,s.y
local r,g,b,a = self.img:get(sx,sy)
if color(r,g,b) == self.intcol and v.set == false then
v.pos = v.pos + v.dir
else
v.set = true
end
if v.set == false then
setall = false
end
end
if setall then
if self.tick == 0 then
local tbl = {}
for k,v in pairs(self.tracker) do
table.insert(tbl,v.pos)
end
self.m.vertices = triangulate(tbl)
self.m:setColors(self.fillcol.r,self.fillcol.g,self.fillcol.b,255)
self.tick = 1
end
end
self.m:draw()
end
function Fill:touched(touch)
-- Codea does not automatically call this method
end