|
@@ -0,0 +1,91 @@
|
|
1
|
+# Programmierung 2 Beleg
|
|
2
|
+# Aufgabe 6
|
|
3
|
+# Mandelbrotmenge
|
|
4
|
+# Yuan Chen MAB14 4060920
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+#--------------------------------------------------------------------------
|
|
8
|
+# Import module
|
|
9
|
+
|
|
10
|
+from tkinter import * # All modules of TKinter have been imported
|
|
11
|
+
|
|
12
|
+#--------------------------------------------------------------------------
|
|
13
|
+# Create a funktion that draw the rectangles with selected color of the
|
|
14
|
+# every single pixels in the canvas
|
|
15
|
+
|
|
16
|
+def Mandelbrot(Xmin, Xmax, Ymin, Ymax):
|
|
17
|
+
|
|
18
|
+ xscale = float(canvas["width"]) / (Xmax - Xmin) # Calculate the x axis zoom scale of canvas width and range of real part of c
|
|
19
|
+ yscale = float(canvas["height"]) / (Ymax - Ymin) # Calculate the y axis zoom scale of canvas height and range of imaginary part of c
|
|
20
|
+ xstep = (Xmax - Xmin) / (float(canvas["width"])) # Calculate the changing step im x axis
|
|
21
|
+ ystep = (Ymax - Ymin) / (float(canvas["height"])) # Calculate the changing step im y axis
|
|
22
|
+
|
|
23
|
+ x = Xmin # x change from the inputed minimum value Xmin to the inputed maximum value Xmax
|
|
24
|
+ while x < Xmax:
|
|
25
|
+ y = Ymin # y change from the inputed minimum value Ymin to the inputed maximum value Ymax
|
|
26
|
+ while y < Ymax:
|
|
27
|
+ c = c_count(complex(x, y)) # Read c for the current x and y with help of the c_count funktion
|
|
28
|
+ if c == i_limit: # Judge if c is equal to the limit which in this case 20
|
|
29
|
+ color = "red" # If ture give it the color red
|
|
30
|
+ else:
|
|
31
|
+ color = colorlist[c] # If not give it the color with help of the getColor funktion
|
|
32
|
+
|
|
33
|
+ canvas.create_rectangle((x - Xmin)*xscale, (y - Ymin)*yscale, # Draw the squares of every single pixels with side length 1 and selected color
|
|
34
|
+ (x - Xmin)*xscale, (y - Ymin)*yscale, # The positions have been also multiplied by the zoom scale so it can fit to the
|
|
35
|
+ fill=color, outline = color) # canvas when it has been changed
|
|
36
|
+ y += ystep
|
|
37
|
+ x += xstep
|
|
38
|
+
|
|
39
|
+#--------------------------------------------------------------------------
|
|
40
|
+# Create a funktion that calculate the time of iterations with gived c and
|
|
41
|
+# the gived maximum absolute value of z_n+1 which is 2 in this case
|
|
42
|
+
|
|
43
|
+def c_count(c):
|
|
44
|
+
|
|
45
|
+ z = complex(0,0) # Defined a complex number z with intial value (0,0)
|
|
46
|
+ for ite in range(i_limit): # Time of iterations changes from 0 to the count limit which is 20 in this case
|
|
47
|
+ z = z*z + c # The calculation fomula
|
|
48
|
+ if abs(z) > 2: # Judge if the absolute value of z has exceeded 2
|
|
49
|
+ return ite # If it is exceeded, return the time of iterations
|
|
50
|
+ return i_limit # If not then return the limit
|
|
51
|
+
|
|
52
|
+#--------------------------------------------------------------------------
|
|
53
|
+# Create a funktion that add different colors to the setcolorlist for the
|
|
54
|
+# different section of time of iterations
|
|
55
|
+
|
|
56
|
+def getColor(iter):
|
|
57
|
+
|
|
58
|
+ for iter in range(i_limit): # The time of iterations changes from 0 to the count limit.
|
|
59
|
+ if iter < 3 : # If it's 0 - 2(time of interations: 1 - 3), set the color as 'magenta'.
|
|
60
|
+ colorlist.append('magenta')
|
|
61
|
+ elif iter < 7 : # If it's 3 - 6(time of interations: 4 - 7), set the color as 'blue' .
|
|
62
|
+ colorlist.append('blue')
|
|
63
|
+ elif iter < 11 : # If it's 7 - 10(time of interations: 8 - 11), set the color as 'green'.
|
|
64
|
+ colorlist.append('#00FF00') # I tried just 'green' but it showed dark green instead, so I used rgb color here so the right color can be showed
|
|
65
|
+ elif iter < 15 : # If it's 11 - 14(time of interations: 12 - 15), set the color as 'yellow'.
|
|
66
|
+ colorlist.append('yellow')
|
|
67
|
+ else : # If it's 15 or greater(time of interations: 16 or greater), set the color as 'red'.
|
|
68
|
+ colorlist.append('red')
|
|
69
|
+
|
|
70
|
+#------------------------------------------------------------------------------
|
|
71
|
+# Main programm
|
|
72
|
+
|
|
73
|
+i_limit = 20 # The time of iterations will be limited within 20
|
|
74
|
+Xmin = -2.0 # The minimum of real part set -2
|
|
75
|
+Xmax = 0.5 # The maxmimum of real part set 0.5
|
|
76
|
+Ymin = -1 # The minimum of imaginary part set -1
|
|
77
|
+Ymax = 1 # The maximum of imaginary part set 1
|
|
78
|
+
|
|
79
|
+iter = 0 # Iterations has been initialized
|
|
80
|
+colorlist = [] # Colorlist has been initialized
|
|
81
|
+getColor(iter) # Colors has been added to the Colorlist with help of getColor funktion
|
|
82
|
+
|
|
83
|
+root = Tk() # The TK class has been initialized
|
|
84
|
+root.title("Mandelbrotmenge - By Yuan Chen") # The title has been set
|
|
85
|
+
|
|
86
|
+canvas = Canvas(root, width=400, height=400, bg="white") # The size and background of the window has been set
|
|
87
|
+canvas.pack() # The window will be showed
|
|
88
|
+
|
|
89
|
+Mandelbrot(Xmin, Xmax, Ymin, Ymax) # The color of every pixel in the canvas has been calculated with help of the Mandelbrot funktion
|
|
90
|
+
|
|
91
|
+root.mainloop()
|