I took one of my older programs and modified it to show how a virus could spread. When the program starts, there are 350 white balls roaming around the screen. Once you tap the screen, you infect one of the balls (color yellow). That ball will interact with the other balls but nothing will happen. After it’s been infected for 6 days (1 day = 3 seconds), that ball becomes contagious (color red). Any white ball that comes in contact with the red ball becomes infected (yellow). That ball then goes thru the 6 day stretch before it becomes contagious (red). Each red ball will infect other balls for 15 days after which time it will be cured (color green). It takes about 50 some days for all balls to go from white to green. Of course, if we had millions of balls and a larger area, it would take a lot longer for this to run. I have counts in the middle of the screen to show the status of the balls.
Remember, tap the screen to start the infection.
displayMode(FULLSCREEN)
function setup()
colTab={color(255),color(255,255,0),color(255,0,0),color(0,255,0)}
dti=5
dtc=20
physics.continuous=true
speed=50
line1 = physics.body(EDGE,vec2(5,5),vec2(10,HEIGHT-5))
line2 = physics.body(EDGE,vec2(WIDTH-5,5),vec2(WIDTH-5,HEIGHT-5))
line3 = physics.body(EDGE,vec2(5,5),vec2(WIDTH-5,5))
line4 = physics.body(EDGE,vec2(5,HEIGHT-5),vec2(WIDTH-5,HEIGHT-5))
val=0
days=0
cnt=0
limit=180
nbr=350
tab={} -- table for balls
for x=1,nbr do -- start with 20 balls
create()
end
end
function draw()
background(50, 50, 50)
normal,infected,contagious,cured=0,0,0,0
strokeWidth(0)
for a,b in pairs(tab) do
if b.inf>0 then
if days-b.inf>=dti and b.col==2 then
b.col=3
elseif days-b.inf>=dtc and b.col==3 then
b.col=4
end
end
fill(colTab[b.col])
ellipse(b.x,b.y,12)
if b.col==1 then
normal=normal+1
elseif b.col==2 then
infected=infected+1
elseif b.col==3 then
contagious=contagious+1
elseif b.col==4 then
cured=cured+1
end
end
stroke(255)
strokeWidth(5)
line(5,5,5,HEIGHT-5)
line(WIDTH-5,5,WIDTH-5,HEIGHT-5)
line(5,5,WIDTH-5,5)
line(5,HEIGHT-5,WIDTH-5,HEIGHT-5)
cnt=cnt+val
if cnt>limit then
cnt=0
days=days+1
end
fill(255, 0, 217)
text("day "..days,WIDTH/2,HEIGHT/2+200)
fill(255)
text("normal "..normal,WIDTH/2,HEIGHT/2+150)
fill(255,255,0)
text("infected "..infected,WIDTH/2,HEIGHT/2+100)
fill(255,0,0)
text("contagious "..contagious,WIDTH/2,HEIGHT/2+50)
fill(0,255,0)
text("cured "..cured,WIDTH/2,HEIGHT/2)
end
function touched(t)
if t.state==BEGAN and infected==0 then
tab[1].col=2
tab[1].inf=1
val=1
end
end
function collide(contact)
if contact.state==BEGAN then
if contact.bodyA.col==3 then
if contact.bodyB.col==1 then
contact.bodyB.inf=days
contact.bodyB.col=2
infected=infected+1
normal=normal-1
end
elseif contact.bodyB.col==3 then
if contact.bodyA.col==1 then
contact.bodyA.inf=days
contact.bodyA.col=2
infected=infected+1
normal=normal-1
end
end
end
end
function create()
local a=#tab+1
tab[a]=physics.body(CIRCLE,6)
tab[a].mass=2
tab[a].friction=0
tab[a].x=math.random(30,WIDTH-30)
tab[a].y=math.random(30,HEIGHT-30)
tab[a].gravityScale=0
tab[a].restitution=1
tab[a].sleepingAllowed=false
a1=math.random(-1,1)*speed
a2=math.random(-1,1)*speed
tab[a].linearVelocity=vec2(a1,a2)
tab[a].inf=0
tab[a].col=1
end