Skip to content

Commit 6f2f870

Browse files
authored
Add files via upload
1 parent 64358aa commit 6f2f870

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

ex11_3ZoomingInAndOut.pyde

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

0 commit comments

Comments
 (0)