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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| #!/usr/bin/env python3
# coding: utf-8
import decimal # Decimal fixed point and floating point arithmetic
import multiprocessing # Process-based parallelism
import sys # System-specific parameters and functions
import time # Time access and conversions
import tkinter as tk # Python interface to Tcl/Tk
color_array = ("#000000", "#000102", "#000205", "#000409", "#000511",
"#010615", "#010617", "#01081c", "#010a23", "#020c28",
"#030e30", "#031037", "#04133f", "#051646", "#07194e",
"#081c55", "#0a1f5d", "#0f2267", "#132670", "#182979",
"#1c2d83", "#21318c", "#243594", "#243c97", "#23439b",
"#234a9e", "#2251a1", "#2257a5", "#215ea8", "#2166ab",
"#206eaf", "#1f76b3", "#1e7eb7", "#1e86bb", "#1d8ebe",
"#2094c0", "#269ac1", "#2ca0c1", "#32a7c2", "#2fa4c2",
'#31a7c1', '#34a7c2', '#35a9c2', '#36aac3', '#38acc3',
'#39adc4', '#39aec2', '#3aafc3', '#3bb0c2', '#3db0c3',
'#3eb3c4', '#3fb5c3', '#40b6c4', '#40b6c2', '#43b7c4',
'#45b7c2', '#47b7c3', '#49bac2', '#4ab9c2', '#4dbac1',
'#4fbac0', '#51bcc2', '#53bcc0', '#55bcc1', '#56bebf',
'#59bfc1', '#5bbfbf', '#5dbfbe', '#5ec0bf', '#61c1bf',
'#64c3bf', '#65c4be', '#68c4bf', '#6ac5bc', '#6cc6bd',
'#6fc6bd', '#70c7be', '#72c8bd', '#75c9bc', '#77c9bd',
'#78cbbb', '#7acbbc', '#7bccbb', '#7eccbc', '#80cdb9',
'#83ceba', '#84cfba', '#87d1ba', '#89d1b9', '#8bd1b9',
'#8ed2b9', '#91d3b8', '#92d4b9', '#94d5b7', '#97d6b9',
'#9ad7b8', '#9bd8b7', '#9dd8b8', '#a0d9b8', '#a2dab7',
'#a3dbb6', '#a6dcb8', '#a9ddb6', '#abddb7', '#acdfb6',
'#afdfb7', '#b1e0b6', '#b4e1b6', '#b7e2b5', '#b8e3b5',
'#bae4b4', '#bde5b3', '#c0e6b5', '#c1e7b4', '#c5e7b4',
'#c6e8b5', '#c7eab4', '#c8e9b2', '#c9eab3', '#cbe9b3',
'#ccebb2', '#ceebb3', '#cfecb2', '#d0ecb2', '#d1edb3',
'#d2eeb3', '#d5efb2', '#d6f0b3', '#d7efb3', '#d7efb1',
'#daf0b2', '#ddf1b2', '#def3b2', '#dff2b2', '#e0f3b1',
'#e4f5b1', '#e6f5b2', '#e9f6b1', '#ebf6b1', '#edf8b3',
'#eef8b3', '#eff8b5', '#f0f9b8', '#f0f9ba', '#f2fabe',
'#f5fac2', '#f3fbc2', '#f4fbc5', '#f7fbc8', '#f8fcc9',
'#f9fccd', '#fbfcd0', "#fbfdd3", "#fdfed6", "#fefed8")
nb_colors = len(color_array) - 1
def generate_line(
x_start, y_start, step, iteration_and_color_offset, loop_increment,
x_pixel, lines_number
): # {
global color_array, nb_colors
line = list()
for y_pixel in range(1, lines_number, loop_increment):
x_value = x_start + (x_pixel * step)
y_value = y_start + (y_pixel * step)
x = decimal.Decimal(0)
y = decimal.Decimal(0)
iterations = 0
pixel_color = "white"
while x * x + y * y <= 4:
if iterations > nb_colors + iteration_and_color_offset: break
x_temp = x * x - y * y + x_value
y = 2 * x * y + y_value
x = x_temp
array_index = iterations - iteration_and_color_offset
if array_index <= 1: array_index = 1
pixel_color = color_array[array_index]
iterations += 1
# while
line.append(pixel_color)
# for
return x_pixel, line
# } generate_line()
def draw_selection(x_start, y_start, step, iteration_and_color_offset, loop_increment):
global drawing_done, lines_number, rows_number, root, cnv
draw_time_m_ref = time.monotonic()
drawing_done = False
root.title("Calculs en cours...(%s)" % step)
root.update()
cnv.delete("all")
try:
nb_processes = multiprocessing.cpu_count() # Nombre de processus/CPU utilisés pour le pool de calcul des portions
if nb_processes > 1: # Conserve 1 CPU pour le main qui gère la création du canvas
nb_processes -= 1
pool_processes = multiprocessing.Pool( # Distribution des tâches automatiquement sur les processus / CPU
processes=nb_processes, # Nombre de processus/CPU
# maxtasksperchild=1, # To free memory after each processing
)
lines_processes = list()
for x_pixel in range(1, rows_number, loop_increment):
# Génération des lignes
lines_processes.append(
pool_processes.apply_async(
generate_line,
(x_start, y_start, step, iteration_and_color_offset, loop_increment, x_pixel, lines_number),
)
)
# for
while len(lines_processes):
for i_comp, line_process in enumerate(lines_processes):
if line_process.ready(): # Calcul de la ligne terminée
x_pixel, line = line_process.get(None)
for y_pixel, pixel_color in enumerate(line):
cnv.create_line(x_pixel, y_pixel, x_pixel + 1, y_pixel + 1, fill=pixel_color)
cnv.update()
lines_processes.pop(i_comp) # Retire le processus terminé
break # Pour reprendre dans l'ordre les colonnes
# if
# for
time.sleep(0.01)
# while
# try
finally:
pool_processes.close() # Libère la mémoire des pool/processus
pool_processes.join() # Attend que tous les processus se terminent
drawing_done = True
root.title("Calculs terminés (%s)" % step)
root.update()
print("Fin du calcul, temps écoulé: {:.02f} s\n".format(time.monotonic() - draw_time_m_ref))
# draw_selection()
if __name__ == '__main__':
drawing_done = False
root_horizontal_margin = 0
root_vertical_margin = 74
interface_width = 0
root = tk.Tk()
root.resizable(False, False)
root.title("")
rows_number = 1024 # root.winfo_screenwidth() - root_horizontal_margin - interface_width
lines_number = 1024 # root.winfo_screenheight() - root_vertical_margin
x_center = decimal.Decimal("-0.175390781769355")
y_center = decimal.Decimal("-1.024562467958025")
# Profondeur
xy_step = decimal.Decimal("1e-%s" % (sys.argv[1] if len(sys.argv) > 1 else "15"))
print(xy_step)
loop_step = 1
color_offset = 220
x_min = x_center - (rows_number * xy_step / 2)
y_min = y_center - (lines_number * xy_step / 2)
x_pointer_coord = int(rows_number / 2)
y_pointer_coord = int(lines_number / 2)
root.geometry(str(rows_number + interface_width) + "x" + str(lines_number) + "+0+0")
left_frame = tk.Frame(root)
left_frame.pack(side='left')
right_frame = tk.Frame(root)
right_frame.pack(side='top', fill='both', expand=True)
cnv = tk.Canvas(left_frame, width=rows_number, height=lines_number, bg='black', highlightthickness=0)
cnv.pack(side='left')
#img = tk.PhotoImage(width=rows_number, height=lines_number)
#cnv.create_image((rows_number, lines_number), image=img, state='normal')
draw_selection(x_min, y_min, xy_step, color_offset, loop_step)
root.mainloop()
# if __main__ |