Created
August 24, 2014 20:42
-
-
Save Sorseg/33ffb4b1a776ef137947 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tkinter as tk | |
import random | |
from collections import namedtuple | |
WIDTH, HEIGHT = 300, 300 | |
PROG = WIDTH//10 | |
root = tk.Tk() | |
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg='white') | |
img = tk.PhotoImage(width=WIDTH, height=HEIGHT) | |
canvas.create_image((WIDTH/2, HEIGHT/2), image=img, state="normal") | |
canvas.pack() | |
COL_RES = 255 | |
def saw(ph): | |
ph = float(ph) % float(6) | |
if ph < 1: | |
return ph | |
if ph < 3: | |
return 1 | |
if ph < 4: | |
return 4 - ph | |
if ph < 6: | |
return 0 | |
def col_from_hue(h): | |
phi = h*6 | |
return col_to_str( | |
int(saw(phi+2)*255), | |
int(saw(phi)*255), | |
int(saw(phi+4)*255) | |
) | |
def col_to_str(r, g, b): | |
return '#{:02x}{:02x}{:02x}'.format(r, g, b) | |
assert col_from_hue(0) == '#ff0000', col_from_hue(0) | |
assert col_from_hue(1) == '#ff0000', col_from_hue(1) | |
def draw(x, y, col): | |
img.put(str(col), (x, y)) | |
class FractalViewer: | |
def __init__(self): | |
self.center = -1 | |
self.radius = 2 | |
self.dx = 0 | |
self.dy = 0 | |
def zoomout(self, evt): | |
self.redraw(evt, True) | |
def redraw(self, evt = None, out=False): | |
self.start = self.center - self.radius + self.radius*1j | |
self.end = self.center + self.radius - self.radius*1j | |
if evt: | |
self.center = self.start + self.dx*evt.x + self.dy*evt.y*1j | |
if out: | |
self.radius *= 1.5 | |
else: | |
self.radius *= 0.2 | |
print(self.center, self.radius) | |
return self.redraw() | |
pic = '' | |
self.dx = (self.end.real - self.start.real)/WIDTH | |
self.dy = (self.end.imag - self.start.imag)/HEIGHT | |
for y in range(HEIGHT): | |
line = '{' | |
for x in range(WIDTH): | |
xc = self.start.real + self.dx*x | |
yc = self.start.imag + self.dy*y | |
z = 0j | |
for i in range(COL_RES): | |
z = pow(z, 2) + xc + yc*1j | |
if abs(z) > 10: | |
col = col_from_hue(i/COL_RES) | |
break | |
else: | |
col = col_to_str(0, 0, 0) | |
line += col+' ' | |
pic += line +'} ' | |
if y and not y % PROG: | |
img.put(pic, (0, y - PROG)) | |
print(y) | |
pic = '' | |
img.put(pic, (0, y - PROG)) | |
f = FractalViewer() | |
canvas.bind('<Button-1>', f.redraw) | |
canvas.bind('<Button-2>', f.zoomout) | |
f.redraw() | |
tk.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment