Last active
October 29, 2017 12:33
-
-
Save nst/7f123d951355041044b7ffae9d33de3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
__author__ = "Nicolas Seriot" | |
__date__ = "2017-05-24" | |
# http://mathworld.wolfram.com/ElementaryCellularAutomaton.html | |
# https://en.wikipedia.org/wiki/Rule_110 | |
import png | |
def next_row(row, rule_bits): | |
r = row[-1:] + row + row[:1] | |
return [rule_bits[r[i]*4 + b*2 + r[i+2]] for (i, b) in enumerate(row)] | |
def save_png_data(rows, filename): | |
ll = [[255 - b * 255 for b in row] for row in rows] | |
png.from_array(ll, 'L').save(filename) | |
print filename | |
def run(): | |
rule = 110 # or 0b01101110 | |
rule_bits = [int(x) for x in '{0:08b}'.format(rule)[::-1]] | |
(WIDTH, HEIGHT) = (1000, 1000) | |
row = [0] * WIDTH | |
row[500] = 1 | |
png_data = [row] | |
for i in range(HEIGHT-1): | |
row = next_row(row, rule_bits) | |
png_data.append(row) | |
save_png_data(png_data, "rule_%d_%d_%s.png" % (rule, WIDTH, HEIGHT)) | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment