Skip to content

Instantly share code, notes, and snippets.

@loopspace
Created January 25, 2015 17:09
Show Gist options
  • Save loopspace/5e3827b5d9094dc5b6b3 to your computer and use it in GitHub Desktop.
Save loopspace/5e3827b5d9094dc5b6b3 to your computer and use it in GitHub Desktop.
Radioactive Decay Release v1.0 -A simulation of radioactive deacy.
Radioactive Decay Tab Order Version: 1.0
------------------------------
This file should not be included in the Codea project.
#Main
-- Radioactive Decay
VERSION = "1.0"
-- Use this function to perform your initial setup
supportedOrientations(LANDSCAPE_ANY)
function setup()
if AutoGist then
autoGist = AutoGist("Radioactive Decay", "A simulation of radioactive deacy.", VERSION)
autoGist:backup(true)
end
parameter.integer("particles",1,2000,1000)
parameter.number("decay_rate",0,1,.1)
parameter.action("Reset",reset)
parameter.boolean("Pause",true)
__sound = sound
parameter.boolean("Geiger_Counter",false, function(b) if b then sound = __sound else sound = function() end end end)
parameter.watch("Number_of_Particles")
imght = HEIGHT/2
graph = image(WIDTH,imght)
reset()
colours = {
color(255, 3, 0, 255),
color(77, 188, 60, 255)
}
end
function draw()
background(40, 40, 50)
local p,n,w
n = 0
for k,v in ipairs(pt) do
if not Pause then
if v[2] == 1 then
if math.random() < decay_rate*DeltaTime then
v[2] = 2
w = 120*vec2(1,0):rotate(math.random(360))
table.insert(betas,{v[1],w})
-- p = true
sound(SOUND_EXPLODE, 14137)
else
n = n + 1
end
end
end
fill(colours[v[2]])
ellipse(v[1].x,v[1].y,wd)
end
local dels = {}
for k,v in ipairs(betas) do
v[1] = v[1] + DeltaTime * v[2]
fill(255, 235, 0, 255)
ellipse(v[1].x,v[1].y,6)
if v[1].x < 0 or v[1].x > WIDTH or v[1].y > HEIGHT or v[1].y < HEIGHT - imght then
table.insert(dels,1,k)
end
end
for k,v in ipairs(dels) do
table.remove(betas,v)
end
pushStyle()
if not Pause then
Number_of_Particles = n
if gx < WIDTH then
setContext(graph)
rectMode(CORNER)
lineCapMode(PROJECT)
noSmooth()
fill(0, 221, 255, 123)
rect(gx,0,20*DeltaTime,n/np*imght)
if n/np < nxt then
local sw = 8
smooth()
stroke(255, 0, 217, 255)
strokeWidth(sw)
line(gx-sw/2,0,gx-sw/2,nxt*imght)
line(gx-sw/2,nxt*imght,0,nxt*imght)
stroke(0, 0, 0, 255)
strokeWidth(2)
line(gx-sw/2,0,gx-sw/2,nxt*imght)
line(gx-sw/2,nxt*imght,0,nxt*imght)
nxt = nxt / 2
end
gx = gx + DeltaTime*20
setContext()
end
end
spriteMode(CORNER)
sprite(graph,0,0)
if p then
sound(SOUND_EXPLODE, 14137)
end
popStyle()
end
function reset()
gx = 0
setContext(graph)
background(0, 0, 0, 0)
setContext()
nxt = .5
betas = {}
local s = math.floor(math.sqrt(WIDTH*imght/particles))
local x = math.floor(WIDTH/s)
local xs = WIDTH/x
local y = math.floor(imght/s)
local ys = imght/y
local v = {}
for k=1,x do
for l=1,y do
table.insert(v,{vec2((k-.5 + .5*math.random())*xs,(l-.5 + .5*math.random())*ys+HEIGHT-imght),1})
end
end
pt = v
wd = s
np = x * y
Number_of_Particles = np
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment