Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Newton_Raphson_Fractal.py
- from tkinter import *
- from PIL import Image, ImageTk
- from itertools import combinations
- from cmath import *
- ww = 600
- hh = 600
- # creates a z**4+1 = 0 fractal using the Newton-Raphson
- # root finding method
- delta = 0.000001 # convergence criteria
- iters = 30 # number of iterations
- # these are the solutions to the equation z**4+1 = 0 (Euler's formula)
- solutions = [cos((2*n+1)*pi/4)+1j*sin((2*n+1)*pi/5) for n in range(4)]
- colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0)]
- def draw():
- image.putdata(rgb)
- photo = ImageTk.PhotoImage(image)
- canvas.create_image(0,0,image=photo,anchor=NW)
- canvas.update()
- root = Tk()
- root.title("Tk_")
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.pack()
- image = Image.new("RGB", (ww,hh), (255,255,255))
- rgb = []
- for re in range(0, ww):
- for im in range(0, hh):
- z = (re+1j*im)/hh
- for i in range(iters):
- try:
- z -= (z**4+1)/(4*z**3)
- except ZeroDivisionError:
- continue
- if(abs(z**4+1)<delta):
- break
- # color depth is a function of the number of iterations
- color_depth = int((iters-i)*255.0/iters)
- # find to which solution this guess converged to
- err = [ abs(z-root) for root in solutions ]
- distances = zip(err, range(len(colors)))
- # select the color associated with the solution
- color = [i*color_depth for i in colors[min(distances)[1]]]
- rgb.append(tuple(color))
- draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement