cellular.py: impleement a individual color mode. Default setting

is pretty vivid and good to go.
This commit is contained in:
Sanky 2012-10-30 20:03:16 +01:00
parent 6165044591
commit 62602ef940

View file

@ -1,6 +1,6 @@
#!/usr/bin/python #!/usr/bin/python
""" """
An elementary 2D celluar automata implementation for the ledbar in brmlab. An elementary 2D cellular automata implementation for the ledbar in brmlab.
For some fun rules, try: For some fun rules, try:
30: near-random behavior 30: near-random behavior
22: gives a symmetric triangle pattern. It just looks like splitting cells and gets 22: gives a symmetric triangle pattern. It just looks like splitting cells and gets
@ -10,15 +10,16 @@ empty quickly, though.
51: likes to blink. 51: likes to blink.
The color mode encodes individual bits into, well, colors. Not too exciting, The color mode encodes individual bits into, well, colors. Not too exciting,
but sure more colorful. but sure more colorful. The individual color mode generates a separate
iteration plane for each color, which works the best with a random starting
state.
You can choose between a single pixel or a random starting row. You can choose between a single pixel or a random starting row.
By setting TOTALISTIC to True and adding proper rules, you get continuous By setting TOTALISTIC to True and adding proper rules, you get continuous
totalistic 1D celluar automata. The basic rule mostly just fades out and totalistic 1D cellular automata. The basic rule mostly just fades out and
in again: it looks like triangles on a plane. But tell me if you find some in again: it looks like triangles on a plane. But tell me if you find some
more interesting rule! more interesting rule!
The RULE format for totalistic automta is a dictionaries of functions which get The RULE format for totalistic automta is a dictionary of functions which get
passed the sum of the above three pixels. The keys are conditions, if one passed the sum of the above three pixels. The keys are conditions, if one
returns true, the value is executed. (Thus they shouldn't overlap.) returns true, the value is executed. (Thus they shouldn't overlap.)
@ -32,12 +33,13 @@ import random
from ledbar import Ledbar from ledbar import Ledbar
PIXELS = 20 PIXELS = 20
PIXEL_MODE = ('bw', 'color')[0] PIXEL_MODE = ('bw', 'color', 'individual_color')[2]
START = ('single', 'random')[1] START = ('single', 'random')[1]
TOTALISTIC = True TOTALISTIC = True
#RULE = 30 #RULE = 30
RULE = {(lambda t: True): (lambda t: (t+0.9) % 1)} RULE = {(lambda t: True): (lambda t: (t+0.98) % 1)}
SLEEP = 25 #RULE = {(lambda t: t > 5): (lambda t: (t+-0.6) % 1), (lambda t: t <= 5): (lambda t: (t+0.9) % 1)}
SLEEP = 10
WIDTH = PIXELS WIDTH = PIXELS
if PIXEL_MODE == 'color': WIDTH *= 3 if PIXEL_MODE == 'color': WIDTH *= 3
@ -50,13 +52,22 @@ if not TOTALISTIC:
rules = dict(zip(((1,1,1), (1,1,0), (1,0,1), (1,0,0), (0,1,1), (0,1,0), (0,0,1), (0,0,0)), bits(RULE))) rules = dict(zip(((1,1,1), (1,1,0), (1,0,1), (1,0,0), (0,1,1), (0,1,0), (0,0,1), (0,0,0)), bits(RULE)))
iteration = [0]*WIDTH iterations = []
if START == 'single': iterations.append([0]*WIDTH)
iteration[WIDTH//2] = 1 if PIXEL_MODE == 'individual_color':
elif START == 'random': iterations.append([0]*WIDTH)
iteration = list((random.randint(0, 1) if not TOTALISTIC else random.random()) for i in iteration) iterations.append([0]*WIDTH)
def iterate(iteration): if START == 'single':
for it in iterations:
it[WIDTH//2] = 1
elif START == 'random':
for j, it in enumerate(iterations):
iterations[j] = list((random.randint(0, 1) if not TOTALISTIC else random.random()) for i in it)
def iterate(iterations):
for j, iteration in enumerate(iterations):
new = [] new = []
iteration.insert(0, 0) iteration.insert(0, 0)
iteration.append(0) iteration.append(0)
@ -71,14 +82,19 @@ def iterate(iteration):
new.append(func(sum(top)/3)) new.append(func(sum(top)/3))
else: else:
new.append(0) new.append(0)
return new iterations[j] = new
return iterations
def update(i): def update(i):
visible = iteration[(len(iteration)//2)-(WIDTH//2):(len(iteration)//2)+(WIDTH//2)] visibles = []
for iteration in iterations:
visibles.append(iteration[(len(iteration)//2)-(WIDTH//2):(len(iteration)//2)+(WIDTH//2)])
if PIXEL_MODE == 'bw': if PIXEL_MODE == 'bw':
return (visible[i], visible[i], visible[i]) return (visibles[0][i], visibles[0][i], visibles[0][i])
elif PIXEL_MODE == 'color': elif PIXEL_MODE == 'color':
return (visible[3*i], visible[3*i+1], visible[3*i+2]) return (visibles[0][3*i], visibles[0][3*i+1], visibles[0][3*i+2])
elif PIXEL_MODE == 'individual_color':
return (visibles[0][i], visibles[1][i], visibles[2][i])
l = Ledbar(PIXELS) l = Ledbar(PIXELS)
work = True work = True
@ -90,4 +106,4 @@ while work:
work = l.update() work = l.update()
t += 1 t += 1
if not (t % SLEEP): if not (t % SLEEP):
iteration = iterate(iteration) iterations = iterate(iterations)