""" Just for Fun: Simple Fractal (ASCII art)
(cc) Kirby Urner, MIT license 4dsolutions.net """ class M: """ Plant an M-Seed and call it, in each cell of the Garden """ depth = 7 def __init__(self, x, y): self.c = complex(x,y) def __call__(self): z = 0+0j for _ in range(M.depth): z = z**2 + self.c return z class Fractal(dict): """ This is the Garden, a complex plane planted with M-Seeds Inherit from dict to store complex numbers in key (x,y) """ def __init__(self, left_x, top_y, right_x, bottom_y): """Top Left, Top, Far Right, Bottom""" super().__init__() self.left_x = left_x y = self.top_y = top_y self.right_x, self.bottom_y = right_x, bottom_y while y >= bottom_y: x = left_x while x < right_x: self[(x,y)] = M(x,y)() # plant seed and call it x += 0.02 y -= 0.05 def __str__(self): """ Reap the harvest """ rows = "" y = self.top_y while y >= self.bottom_y: row="" x = self.left_x while x <= self.right_x: v = abs(self[(x,y)]) # calibrate by magnitude of stored value if v >= 2: row += "." elif 2 > v >= 1.5: row += "^" elif 1.5 > v >= 1.3: row += "+" elif 1.3 > y >= 1.0: row += "@" else: row += "@" x += 0.02 rows += row + "\n" y -= 0.05 return rows if __name__ == "__main__": # run me and open the file in a text editor to see 'fractal' f = Fractal(-2.2, 1.4, 0.8, -1.4) with open("mandelbrot.txt","w") as mandelbrot: print(f, file = mandelbrot)
_______________________________________________ Edu-sig mailing list Edu-sig@python.org https://mail.python.org/mailman/listinfo/edu-sig