Debian Codegraph @

rpython/tool/ansi_mandelbrot.py

raw symbols SCIP
1 from __future__ import print_function
2 import sys
3
4 from py.io import ansi_print, get_terminal_width
5
6 """
7 Black 0;30 Dark Gray 1;30
8 Blue 0;34 Light Blue 1;34
9 Green 0;32 Light Green 1;32
10 Cyan 0;36 Light Cyan 1;36
11 Red 0;31 Light Red 1;31
12 Purple 0;35 Light Purple 1;35
13 Brown 0;33 Yellow 1;33
14 Light Gray 0;37 White 1;37
15 """
16
17
18 import os
19 if os.environ.get('TERM', 'dumb').find('256') > 0:
20 try:
21 from ansiramp import ansi_ramp80
22 palette = map(lambda x: "38;5;%d" % x, ansi_ramp80)
23 except ImportError:
24 import warnings
25 warnings.warn("ansiramp not installed; falling back to 8-color palette (pip install ansiramp for 256-color support)")
26 palette = [39, 34, 35, 36, 31, 33, 32, 37]
27 else:
28 palette = [39, 34, 35, 36, 31, 33, 32, 37]
29
30 # used for debugging/finding new coordinates
31 # How to:
32 # 1. Set DEBUG to True
33 # 2. Add a new coordinate to coordinates with a high distance and high max colour (e.g. 300)
34 # 3. Run, pick an interesting coordinate from the shown list and replace the newly added
35 # coordinate by it.
36 # 4. Rerun to see the max colour, insert this max colour where you put the high max colour.
37 # 5. Set DEBUG to False
38 DEBUG = False
39
40
41 class Mandelbrot:
42 def __init__ (self, width=100, height=28, x_pos=-0.5, y_pos=0, distance=6.75):
43 self.xpos = x_pos
44 self.ypos = y_pos
45 aspect_ratio = 1/3.
46 factor = float(distance) / width # lowering the distance will zoom in
47 self.xscale = factor * aspect_ratio
48 self.yscale = factor
49 self.iterations = 170
50 self.x = width
51 self.y = height
52 self.z0 = complex(0, 0)
53
54 def init(self):
55 self.reset_lines = False
56 xmin = self.xpos - self.xscale * self.x / 2
57 ymin = self.ypos - self.yscale * self.y / 2
58 self.x_range = [xmin + self.xscale * ix for ix in range(self.x)]
59 self.y_range = [ymin + self.yscale * iy for iy in range(self.y)]
60
61 def reset(self, cnt):
62 self.reset_lines = cnt
63
64 def generate(self):
65 self.reset_lines = False
66 iy = 0
67 while iy < self.y:
68 ix = 0
69 while ix < self.x:
70 c = complex(self.x_range[ix], self.y_range[iy])
71 z = self.z0
72 colour = 0
73 mind = 2
74
75 for i in range(self.iterations):
76 z = z * z + c
77 d = abs(z)
78 if d >= 2:
79 colour = min(int(mind / 0.007), 254) + 1
80 break
81 else:
82 mind = min(d, mind)
83
84 yield ix, iy, colour
85 if self.reset_lines is not False: # jump to the beginning of the line
86 iy += self.reset_lines
87 do_break = bool(self.reset_lines)
88 self.reset_lines = False
89 if do_break:
90 break
91 ix = 0
92 else:
93 ix += 1
94 iy += 1
95
96
97 class Driver(object):
98 zoom_locations = [
99 # x, y, "distance", max color range
100 (0.37865401, 0.669227668, 0.04, 111),
101 (-1.2693, -0.4145, 0.2, 105),
102 (-1.2693, -0.4145, 0.05, 97),
103 (-1.2642, -0.4185, 0.01, 95),
104 (-1.15, -0.28, 0.9, 94),
105 (-1.15, -0.28, 0.3, 58),
106 (-1.15, -0.28, 0.05, 26),
107 ]
108 def __init__(self, **kwargs):
109 self.kwargs = kwargs
110 self.zoom_location = -1
111 self.max_colour = 256
112 self.colour_range = None
113 self.invert = True
114 self.interesting_coordinates = []
115 self.init()
116
117 def init(self):
118 self.width = get_terminal_width() or 80 # in some envs, the py lib doesnt default the width correctly
119 self.mandelbrot = Mandelbrot(width=(self.width or 1), **self.kwargs)
120 self.mandelbrot.init()
121 self.gen = self.mandelbrot.generate()
122
123 def reset(self, cnt=0):
124 """ Resets to the beginning of the line and drops cnt lines internally. """
125 self.mandelbrot.reset(cnt)
126
127 def restart(self):
128 """ Restarts the current generator. """
129 print('', file=sys.stderr)
130 self.init()
131
132 def dot(self):
133 """ Emits a colourful character. """
134 x = c = 0
135 try:
136 x, y, c = self.gen.next()
137 if x == 0:
138 width = get_terminal_width()
139 if width != self.width:
140 self.init()
141 except StopIteration:
142 if DEBUG and self.interesting_coordinates:
143 print("Interesting coordinates:", self.interesting_coordinates, file=sys.stderr)
144 self.interesting_coordinates = []
145 kwargs = self.kwargs
146 self.zoom_location += 1
147 self.zoom_location %= len(self.zoom_locations)
148 loc = self.zoom_locations[self.zoom_location]
149 kwargs.update({"x_pos": loc[0], "y_pos": loc[1], "distance": loc[2]})
150 self.max_colour = loc[3]
151 if DEBUG:
152 # Only used for debugging new locations:
153 print("Colour range", self.colour_range)
154 self.colour_range = None
155 self.restart()
156 return
157 if self.print_pixel(c, self.invert):
158 self.interesting_coordinates.append(dict(x=(x, self.mandelbrot.x_range[x]),
159 y=(y, self.mandelbrot.y_range[y])))
160 if x == self.width - 1:
161 print('', file=sys.stderr)
162
163 def print_pixel(self, colour, invert=1):
164 chars = [".", ".", "+", "*", "%", "#"]
165 idx = lambda chars: (colour+1) * (len(chars) - 1) / self.max_colour
166 if invert:
167 idx = lambda chars, idx=idx:len(chars) - 1 - idx(chars)
168 char = chars[idx(chars)]
169 ansi_colour = palette[idx(palette)]
170 ansi_print(char, ansi_colour, newline=False, flush=True)
171 if DEBUG:
172 if self.colour_range is None:
173 self.colour_range = [colour, colour]
174 else:
175 old_colour_range = self.colour_range
176 self.colour_range = [min(self.colour_range[0], colour), max(self.colour_range[1], colour)]
177 if old_colour_range[0] - colour > 3 or colour - old_colour_range[1] > 3:
178 return True
179
180
181 if __name__ == '__main__':
182 import random
183 from time import sleep
184
185 d = Driver()
186 while True:
187 sleep(random.random() / 800)
188 d.dot()
189 if 0 and random.random() < 0.01:
190 string = "WARNING! " * 3
191 d.jump(len(string))
192 print(string, end='')