-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX12.15.py
78 lines (56 loc) · 2.24 KB
/
EX12.15.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 12.15 (Tkinter: Julia set) The preceding exercise describes Mandelbrot sets. The Mandelbrot
# set consists of the complex c value such that the sequence zn+1 = z2n + c
# is bounded with Z_0 fixed and c varying. If we fix c and vary z0 (= x + yi), the
# point (x, y) is said to be in a Julia set for a fixed complex value c if the function
# zn+1 = z2n + c stays bounded. Write a program that draws a Julia set as shown
# in Figure 12.26b. Note that you only need to revise the count method in Exercise
# 12.14 by using a fixed c value (-0.3 + 0.6i).
from tkinter import * # Import tkinter
# Convert a decimal to a hex as a string
def toHex(decimalValue):
hex = ""
while decimalValue != 0:
hexValue = decimalValue % 16
hex = toHexChar(hexValue) + hex
decimalValue = decimalValue // 16
if len(hex) < 2:
hex = "0" + hex
return hex
# Convert an integer to a single hex digit in a character
def toHexChar(hexValue):
if hexValue <= 9 and hexValue >= 0:
return chr(hexValue + ord('0'))
else: # hexValue <= 15 && hexValue >= 10
return chr(hexValue - 10 + ord('A'))
COUNT_LIMIT = 60
# Paint a Mandelbrot image in the canvas
def paint():
x = -2.0
while x < 2.0:
y = -2.0
while y < 2.0:
z = count(complex(x, y))
if z == COUNT_LIMIT:
color = "red"
else:
color = "#" + toHex(z * 37 % 256) + toHex(
z * 58 % 256) + toHex(z * 159 % 256)
canvas.create_rectangle(x * 100 + 200, y * 100 + 200,
x * 100 + 200 + 5, y * 100 + 200 + 5, fill=color)
y += 0.05
x += 0.05
# Returns the iteration count
def count(z):
c = complex(-0.3, 0.6)
for i in range(COUNT_LIMIT):
z = z * z + c # Get z1, z2, ...
if abs(z) > 2: return i # The sequence is unbounded
return COUNT_LIMIT # Indicate a bounded sequence
window = Tk() # Create a window
window.title("Julia set") # Set a title
width = 400 # Width of the canvas
height = 400 # Height of the canvas
canvas = Canvas(window, width=width, height=height)
canvas.pack()
Button(window, text="Display", command=paint).pack()
window.mainloop() # Create an event loop