Skip to content

Commit 64358aa

Browse files
authored
Add files via upload
1 parent 76db99d commit 64358aa

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

ex11_2ChangingtheRuleSet.pyde

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
''' Rule 30 was introduced by Stephan Wolfram in 1983.
2+
Creating Rule 90 design using Cellular Automation.
3+
Change the value of w, rows, and cols to explore the design.
4+
'''
5+
6+
w = 5 #width of each cell
7+
rows = 200
8+
cols = 200
9+
#--snip--
10+
ruleset = [0,1,0,1,1,0,1,0] #rule 90
11+
12+
def setup():
13+
global cells
14+
size(600,600)
15+
noStroke()
16+
#first row:
17+
cells = []
18+
for r in range(rows):
19+
cells.append([])
20+
for c in range(cols):
21+
cells[r].append(0)
22+
cells[0][cols//2] = 1
23+
cells = generate()
24+
25+
def draw():
26+
background(255) #white
27+
#draw the CA
28+
for i, cell in enumerate(cells): #rows
29+
for j, v in enumerate(cell): #columns
30+
if v == 1:
31+
fill(0)
32+
else:
33+
fill(255)
34+
rect(j*w-(cols*w-width)/2,w*i,w,w)
35+
36+
#connverting from binary to decimal
37+
#the indices are in reverse order, so we subtract the total from 7 to get the proper index of the ruleset.
38+
def rules(a,b,c):
39+
return ruleset[7 - (4*a + 2*b + c)]
40+
41+
def generate():
42+
for i, row in enumerate(cells): #look at first row
43+
for j in range(1,len(row)-1):
44+
left = row[j-1]
45+
me = row[j]
46+
right = row[j+1]
47+
if i < len(cells) - 1:
48+
cells[i+1][j] = rules(left,me,right)
49+
return cells

0 commit comments

Comments
 (0)