-
Notifications
You must be signed in to change notification settings - Fork 0
/
elcellomata.py
executable file
·226 lines (184 loc) · 8.05 KB
/
elcellomata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
"""
Visualize elementary cellular automata.
Copyright (c) 2020 Waqar Hameed
SPDX-License-Identifier: MIT
"""
__version__ = "0.1"
__url__ = "https://www.github.com/whame/elcellomata"
import argparse
import random
import math
import cairo
def transition(config, rules):
"""Get next state of cell for a rule (Wolfram code).
:param config: Neighbor configuration.
:param rules: Rule for state transistion.
:returns: Next state.
:raises ValueError: If config is invalid.
"""
assert len(rules) == 8
if config == [1, 1, 1]:
return rules[0]
elif config == [1, 1, 0]:
return rules[1]
elif config == [1, 0, 1]:
return rules[2]
elif config == [1, 0, 0]:
return rules[3]
elif config == [0, 1, 1]:
return rules[4]
elif config == [0, 1, 0]:
return rules[5]
elif config == [0, 0, 1]:
return rules[6]
elif config == [0, 0, 0]:
return rules[7]
raise ValueError(f"Invalid config argument: {config}")
def draw_circle(cairo_context, x, y, radius, fill):
"""Draw a circle.
:param cairo_context: Cairo context to use when drawing.
:param x: X center coordinate for the circle.
:param y: Y center coordinate for the circle.
:param radius: Radius for the circle.
:param fill: True if circle should be filled, otherwise false.
"""
cairo_context.arc(x, y, radius, 0, 2 * math.pi)
if fill:
cairo_context.fill()
cairo_context.stroke()
def draw_line(cairo_context, from_x, from_y, to_x, to_y):
"""Draw a line.
:param cairo_context: Cairo context to use when drawing.
:param from_x: Start drawing the line from this X coordinate.
:param from_y: Start drawing the line from this Y coordinate.
:param to_x: End drawing the line at this X coordinate.
:param to_y: End drawing the line at this Y coordinate.
"""
cairo_context.move_to(from_x, from_y)
cairo_context.line_to(to_x, to_y)
cairo_context.stroke()
def print_grid(grid):
"""Print a grid.
"""
for i in range(len(grid)):
for j in range(len(grid[i])):
print("¤" if grid[i][j] else ".", end="")
print()
# Argument parsing.
arg_parser = argparse.ArgumentParser(
description="Visualize elementary cellular automata.",
epilog=f"Report bugs to {__url__}.")
arg_parser.add_argument(
"rule", metavar="RULE", type=int, choices=range(0, 256),
help="Rule for the cellular automaton.")
arg_parser.add_argument("-o", "--output", metavar="FILE", dest="output_file",
help="Output SVG image to FILE. Default is "
"\"ruleN.svg\", where N is the rule number RULE.")
arg_parser.add_argument("-p", "--print", action="store_true",
help="Print the visualization to stdout.")
arg_parser.add_argument("-v", "--version", action="version",
version="%(prog)s" + " " + __version__)
args = arg_parser.parse_args()
# Rule value for the configurations [111, 110, 101, 100, 011, 010, 001, 000].
RULES = [int(c) for c in bin(args.rule)[2:].zfill(8)]
# The cells is represented by a grid of nodes.
GRID_WIDTH = 100
GRID_HEIGHT = 120
GRID_LEFT_RIGHT_PAD = 10
GRID_TOP_BOTTOM_PAD = 20
DRAW_WIDTH_STEP = 15 # Points (pt) between each node on the same grid row.
DRAW_HEIGHT_STEP = 15 # Points (pt) between each node on the same grid column.
DRAW_WIDTH = (GRID_WIDTH - 1 + GRID_LEFT_RIGHT_PAD * 2) * DRAW_WIDTH_STEP
DRAW_HEIGHT = (GRID_HEIGHT - 1 + GRID_TOP_BOTTOM_PAD * 2) * DRAW_HEIGHT_STEP
if not args.output_file:
args.output_file = "rule" + str(args.rule) + ".svg"
# Initialize Cairo.
crsfc = cairo.SVGSurface(args.output_file, DRAW_WIDTH, DRAW_HEIGHT)
crctx = cairo.Context(crsfc)
crctx.set_source_rgb(1, 1, 1)
crctx.rectangle(0, 0, DRAW_WIDTH, DRAW_HEIGHT)
crctx.fill()
crctx.set_source_rgb(0, 0, 0)
crctx.set_line_cap(cairo.LINE_CAP_ROUND)
# Calculate and draw nodes.
grid = [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
grid[0][:] = [random.randint(0, 1) for i in range(GRID_WIDTH)]
for i in range(len(grid) - 1):
for j in range(len(grid[i])):
if j == 0:
# Wrap left edge with right ("infinite plane").
grid[i + 1][j] = transition([grid[i][-1], *grid[i][0: j + 2]],
RULES)
elif j == len(grid[i]) - 1:
# Wrap right edge with left ("infinite plane").
grid[i + 1][j] = transition([*grid[i][j - 1:], grid[i][0]],
RULES)
else:
grid[i + 1][j] = transition(grid[i][j - 1:j + 2], RULES)
if not grid[i + 1][j]:
continue
# Draw lines from previous row neighbors.
if j and grid[i][j - 1]:
draw_line(crctx, (j - 1 + GRID_LEFT_RIGHT_PAD) * DRAW_WIDTH_STEP,
(i + GRID_TOP_BOTTOM_PAD) * DRAW_HEIGHT_STEP,
(j + GRID_LEFT_RIGHT_PAD) * DRAW_WIDTH_STEP,
(i + 1 + GRID_TOP_BOTTOM_PAD) * DRAW_HEIGHT_STEP)
if grid[i][j]:
draw_line(crctx, (j + GRID_LEFT_RIGHT_PAD) * DRAW_WIDTH_STEP,
(i + GRID_TOP_BOTTOM_PAD) * DRAW_HEIGHT_STEP,
(j + GRID_LEFT_RIGHT_PAD) * DRAW_WIDTH_STEP,
(i + 1 + GRID_TOP_BOTTOM_PAD) * DRAW_HEIGHT_STEP)
if j < len(grid[i]) - 1 and grid[i][j + 1]:
draw_line(crctx, (j + 1 + GRID_LEFT_RIGHT_PAD) * DRAW_WIDTH_STEP,
(i + GRID_TOP_BOTTOM_PAD) * DRAW_HEIGHT_STEP,
(j + GRID_LEFT_RIGHT_PAD) * DRAW_WIDTH_STEP,
(i + 1 + GRID_TOP_BOTTOM_PAD) * DRAW_HEIGHT_STEP)
# Mark nodes with only one neighbor.
for i in range(len(grid)):
for j in range(len(grid[i])):
if not grid[i][j]:
continue
start_slice = max(0, j - 1)
stop_slice = min(j + 2, len(grid[i]))
if i == 0:
if sum(grid[i + 1][start_slice:stop_slice]) == 1:
draw_circle(crctx, (j + GRID_LEFT_RIGHT_PAD) * DRAW_WIDTH_STEP,
(i + GRID_TOP_BOTTOM_PAD) * DRAW_HEIGHT_STEP, 4,
True)
elif i == len(grid) - 1:
if sum(grid[i - 1][start_slice:stop_slice]) == 1:
draw_circle(crctx, (j + GRID_LEFT_RIGHT_PAD) * DRAW_WIDTH_STEP,
(i + GRID_TOP_BOTTOM_PAD) * DRAW_HEIGHT_STEP, 4,
True)
elif sum(grid[i - 1][start_slice:stop_slice]) + \
sum(grid[i + 1][start_slice:stop_slice]) == 1:
draw_circle(crctx, (j + GRID_LEFT_RIGHT_PAD) * DRAW_WIDTH_STEP,
(i + GRID_TOP_BOTTOM_PAD) * DRAW_HEIGHT_STEP, 4, True)
# Draw rule pattern.
RULE_PATTERN_SCALE = 0.85
RULE_PATTERN_RADIUS = RULE_PATTERN_SCALE * DRAW_WIDTH_STEP
RULE_PATTERN_SPACE = 10 * RULE_PATTERN_RADIUS
RULE_PATTERN_CIRCLE_PAD = 2.5 * RULE_PATTERN_RADIUS
RULE_PATTERN_WIDTH = (7 * RULE_PATTERN_SPACE + 2 * RULE_PATTERN_CIRCLE_PAD +
RULE_PATTERN_RADIUS) / DRAW_WIDTH_STEP
RULE_PATTERN_WIDTH = round(RULE_PATTERN_WIDTH)
RULE_PATTERN_X_POS = ((GRID_WIDTH + 2 * GRID_LEFT_RIGHT_PAD -
RULE_PATTERN_WIDTH) / 2) * DRAW_WIDTH_STEP
RULE_PATTERN_Y_POS = (DRAW_HEIGHT - GRID_TOP_BOTTOM_PAD * 0.7 *
DRAW_HEIGHT_STEP)
for j, rule in enumerate(RULES):
draw_circle(crctx, RULE_PATTERN_X_POS + j * RULE_PATTERN_SPACE,
RULE_PATTERN_Y_POS, RULE_PATTERN_RADIUS, (7 - j) & (1 << 2))
draw_circle(crctx, RULE_PATTERN_X_POS +
j * RULE_PATTERN_SPACE + RULE_PATTERN_CIRCLE_PAD,
RULE_PATTERN_Y_POS, RULE_PATTERN_RADIUS, (7 - j) & (1 << 1))
draw_circle(crctx, RULE_PATTERN_X_POS +
j * RULE_PATTERN_SPACE + 2 * RULE_PATTERN_CIRCLE_PAD,
RULE_PATTERN_Y_POS, RULE_PATTERN_RADIUS, (7 - j) & (1 << 0))
draw_circle(crctx, RULE_PATTERN_X_POS +
j * RULE_PATTERN_SPACE + RULE_PATTERN_CIRCLE_PAD,
RULE_PATTERN_Y_POS + RULE_PATTERN_CIRCLE_PAD,
RULE_PATTERN_RADIUS, rule)
if args.print:
print_grid(grid)