From fe84c73459990202303ef2200d98b766d49594dc Mon Sep 17 00:00:00 2001 From: Connor Date: Sat, 19 Jan 2019 23:21:58 -0800 Subject: [PATCH 1/2] --- lsca.uni.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 lsca.uni.py diff --git a/lsca.uni.py b/lsca.uni.py new file mode 100644 index 0000000..e2ab60f --- /dev/null +++ b/lsca.uni.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 + +import sys +import numpy as np + + +def lament(*args, **kwargs): + return print(*args, file=sys.stderr, **kwargs) + + +def colorize(x): + return x * 17 % 256 + + +def render(row): + grays = colorize(row) + if scale > 1: + grays = np.repeat(grays, scale) + line = " ".join(str(x) for x in grays) + for y in range(scale): + print(line) + + +def limit(row): # in-place + row[row < 0] = 0 + row[row > 15] = 15 + return row + + +def initialize(): + row = np.zeros(width, int) + prev, here = 0, 0 + for i in range(width): + if np.random.randint(0, 8) == 0: + here = np.random.randint(16) + row[i] = here + prev = here + limit(row) + return row + + +name, args = sys.argv[0], sys.argv[1:] + +if len(args) == 0: + rule_lut = None +elif len(args) == 16: + rule_lut = [int(x.strip(",")) for x in args] +else: + lament(f"usage: {name}") + lament(f"usage: {name} {{rules 1 through 16}}") + sys.exit(1) + +scale = 3 +width, height = 960 // scale, 960 // scale + +if rule_lut is None: + rule_lut = np.random.randint(-2, 2 + 1, size=(4, 4)) + lament(" ".join(f"{x:+2}" for x in rule_lut.flat)) +else: + rule_lut = np.array(rule_lut).reshape(4, 4) + +print("P2") # magic code for an ascii Portable GrayMap (PGM) file +print(width * scale, height * scale) +print(255) # maximum color value + +row = initialize() + +for i in range(height): + # left, center, right: + L = np.roll(row, 1) + C = row.copy() + R = np.roll(row, -1) + + diffusion = (L + C + C + R + 2) // 4 + + # v = [0,1,2,3,1,0,3,2,2,3,0,1,3,2,1,0][V] + y = (L ^ (L >> 2)) % 4 + x = (R ^ (R >> 2)) % 4 + + delta = rule_lut[y, x] + row = diffusion + delta + + limit(row) + render(row) From 1d216288dbabf1936c003c6412e42664b25530e5 Mon Sep 17 00:00:00 2001 From: Connor Date: Sat, 19 Jan 2019 23:41:58 -0800 Subject: [PATCH 2/2] --- lsca.uni.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lsca.uni.py b/lsca.uni.py index e2ab60f..326eb86 100644 --- a/lsca.uni.py +++ b/lsca.uni.py @@ -12,7 +12,7 @@ def colorize(x): return x * 17 % 256 -def render(row): +def render(row, scale): grays = colorize(row) if scale > 1: grays = np.repeat(grays, scale) @@ -27,14 +27,13 @@ def limit(row): # in-place return row -def initialize(): +def initialize(width): row = np.zeros(width, int) - prev, here = 0, 0 + value = 0 for i in range(width): - if np.random.randint(0, 8) == 0: - here = np.random.randint(16) - row[i] = here - prev = here + if np.random.randint(8) == 0: # only change values occasionally + value = np.random.randint(16) + row[i] = value limit(row) return row @@ -63,7 +62,7 @@ print("P2") # magic code for an ascii Portable GrayMap (PGM) file print(width * scale, height * scale) print(255) # maximum color value -row = initialize() +row = initialize(width) for i in range(height): # left, center, right: @@ -81,4 +80,4 @@ for i in range(height): row = diffusion + delta limit(row) - render(row) + render(row, scale)