#!/usr/bin/python # kohaku0.py - Don Yang (uguu.org) # Julia set renderer # # 11/02/05 import string import random # Constants iteration_count = 26 x_steps = 79 y_steps = 24 aspect_ratio = 1.5 def render_point(z, c): """Render a point by exponentiating it repeatedly. Returns number of iterations it takes for the number to diverge, clamped by iteration_count.""" for i in range(iteration_count + 1): if abs(z) > 2: return i z = z * z + c return iteration_count + 1 def iter_char(i): """Convert iteration value to character.""" if i >= 26 or i < 0: return ' ' return chr(ord('A') + i) def interesting(x0, x1, y0, y1, c): """Check if a particular set of coordinates is interesting by checking for high variety of values in sampled points.""" series = map(lambda p : render_point(p, c), [complex(x0, y0), complex(x0, y1), complex(x1, y0), complex(x1, y1), complex((x0 + x1) / 2, (y0 + y1) / 2)]) average = sum(series) / 5 stddev = sum(map(lambda x : (x - average) * (x - average), series)) / 5 if stddev > iteration_count * 4.6: return 1 return 0 def render_image(x0, x1, xsteps, y0, y1, ysteps, c): """Render image.""" for i in range(ysteps): y = (y0 - y1) * float(i) / (ysteps - 1) + y1 print string.join( map(lambda j : iter_char(render_point( complex((x1 - x0) * float(j) / (xsteps - 1) + x0, y), c)), range(xsteps)), "") def render_random_image(): """Generate coordinates repeatedly until an interesting one is found, then render image.""" i = 0 while not i: x0 = random.uniform(-1, 1) y0 = random.uniform(-1, 1) s = random.uniform(0.1, 1) c = complex(random.uniform(-1, 1), random.uniform(-1, 1)) i = interesting(x0, x0 + s * aspect_ratio, y0, y0 + s, c) render_image(x0, x0 + s * aspect_ratio, x_steps, y0, y0 + s, y_steps, c) random.seed() render_random_image()