Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Last active May 17, 2018 00:38
Show Gist options
  • Save jorendorff/0426b3b5a78adc14e3139fabde781012 to your computer and use it in GitHub Desktop.
Save jorendorff/0426b3b5a78adc14e3139fabde781012 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys, os, time
import math, random
import functools
PALETTE = ' .+?#$%'
DEFAULT_WIDTH = 160
def get_width():
if sys.stdin.isatty() and os.name == 'posix':
with os.popen('stty size', 'r') as f:
_rows, _space, columns = f.read().strip().partition(' ')
if columns is not None and columns.isdigit():
n = int(columns)
if 10 <= n < 100000:
return n
return DEFAULT_WIDTH
def render_glyph(pal, z):
n = len(pal)
z = int(n * z)
if z < 0:
return pal[0]
elif z >= len(pal):
return pal[-1]
else:
return pal[z]
ENTROPY = time.time() # lol
def drng(seed):
seed += ENTROPY
seq = 0.0
def drand():
nonlocal seq
seq += 1.0
return (1.0e7 * math.sin(seed * math.sqrt(3) + seq)) % 1.0
return drand
def random_noise_function():
ASPECT_RATIO = 0.7
@functools.lru_cache(maxsize=3)
def get_waves_at(patch_id):
rng = drng(patch_id)
n_octaves = 4 + int(rng() * 16)
freq_base = 1 + rng()
TYPICAL_FREQ = 20.0
center_w = (2 * math.pi / TYPICAL_FREQ) * 2 ** (2 * rng() - 1)
return [
(1/16 * (1 + rng() + (1 - octave / n_octaves)),
0.9 * (2 * rng() - 1),
center_w * freq_base ** (octave - n_octaves / 2))
for octave in range(n_octaves)
for _ in (0, 1)
]
def eval_waves(waves, x, y):
t = 0.0
for a, b, w in waves:
t += a * math.sin(w * (math.sin(b) * x + math.cos(b) * y))
return t
PATCH_SIZE = 500
def mix(x1, x2, pct):
return (1.0 - pct) * x1 + pct * x2
def f(x, y):
x *= ASPECT_RATIO
y_patch, y_offset = divmod(y, PATCH_SIZE)
waves0 = get_waves_at(y_patch)
waves1 = get_waves_at(y_patch + 1)
return mix(eval_waves(waves0, x, y),
eval_waves(waves1, x, y),
y_offset / PATCH_SIZE)
return f
def flyover_forever(width, f):
y = 0.0
while True:
print(''.join(render_glyph(PALETTE, f(x, y)) for x in range(width)))
time.sleep(1/120)
y += 1.0
flyover_forever(get_width(), random_noise_function())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment