No Description

Beleg_Mandelbrotmenge.py 7.0KB

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