Here’s an updated class version. I allow circles or ellipses for the timers.
viewer.mode=FULLSCREEN
function setup()
tab={}
table.insert(tab,timer(200,300,100,100,10))
table.insert(tab,timer(300,500,150,50,20))
table.insert(tab,timer(600,500,80,150,30))
end
function draw()
background(0)
for a,b in pairs(tab) do
b:draw()
if b.done then
fill(255)
text("Done",b.x,b.y)
end
end
text("Tap inside each ellipse to start timer",WIDTH/2,HEIGHT-50)
end
function touched(t)
if t.state==BEGAN then
for a,b in pairs(tab) do
b:touched(t)
end
end
end
timer=class()
function timer:init(x,y,a,b,t)
self.x=x
self.y=y
self.a=a
self.b=b
self.e=0
self.t=t
self.run=false
self.done=false
self.m=mesh()
end
function timer:draw()
self:addAngle()
self:calcMesh()
self:drawMesh()
self:meshOutline()
self:countdown()
self:checkAngle()
end
function timer:touched(t)
if t.state==BEGAN then
if (t.x-self.x)^2/self.a^2+(t.y-self.y)^2/self.b^2 <= 1 then
self.run=true
end
end
end
function timer:drawMesh()
self.m.vertices=self.mtab
if self.done then
self.m:setColors(255,0,0)
else
self.m:setColors(0,200,255)
end
self.m.draw(self.m)
end
function timer:meshOutline()
stroke(0,200,255)
strokeWidth(2)
noFill()
ellipse(self.x,self.y,self.a*2+2,self.b*2+2)
end
function timer:countdown()
fill(255)
if not self.done then
text(string.format("%.1f",self.t-(self.t/(360/self.e))),self.x,self.y)
end
end
function timer:addAngle()
if self.run then
self.e=self.e+6/self.t
end
end
function timer:checkAngle()
if self.e>360 then
self.run=false
self.done=true
end
end
function timer:calcMesh()
if self.run then
self.mtab={}
self.ctab={}
table.insert(self.ctab,vec2(self.x,self.y))
for z=0,self.e do
x1=self.a*math.cos(math.rad(90-z))+self.x
y1=self.b*math.sin(math.rad(90-z))+self.y
table.insert(self.ctab,vec2(x1,y1))
end
for z=2,#self.ctab do
table.insert(self.mtab,(vec2(self.ctab[1].x,self.ctab[1].y)))
table.insert(self.mtab,(vec2(self.ctab[z-1].x,self.ctab[z-1].y)))
table.insert(self.mtab,(vec2(self.ctab[z].x,self.ctab[z].y)))
end
end
end