#!/usr/bin/python
# -*- coding: utf-8 -*-   ## Baustelle: MUSS 1. oder 2. Zeile sein
# name mb_thread          # MRies, 09/2015
  
__author__ = 'ries'

import numpy as np 
import matplotlib.pyplot as plt
import threading

def mb_iteration(x, y):
	c = complex(x,y)      # x+yj
	z = 0+0j
	for iteration in range(255):
		z = z*z + c
		if abs( z ) >= 2:
			return  iteration 
	return 255
	
def mb_matrix_t(x_min, x_max, y_min, y_max, bild,threads,i):
	zeilen_zahl  = bild.shape[0]
	spalten_zahl = bild.shape[1]
	delta_x = (x_max - x_min) / spalten_zahl
	delta_y = (y_max - y_min) / zeilen_zahl
	spalten_start = i * spalten_zahl/threads 
	spalten_ende  = (i+1) * spalten_zahl/threads
	for spalte_j in range(spalten_start, spalten_ende):
		x_pix = x_min + spalte_j * delta_x
		for zeile_i in range(zeilen_zahl):
			y_pix = y_min + zeile_i * delta_y
			bild[(zeilen_zahl-1)-zeile_i, spalte_j] =  \
						 mb_iteration(x_pix, y_pix)
	return bild

bild = np.zeros((400, 400), dtype=np.uint8)

threads = 8
jobs = []
for i in range(0, threads):
		thread = threading.Thread(target=mb_matrix_t, \
			args = (-0.088, -0.085, 0.855, 0.858, bild, \
			threads,i) )
		thread.start()

for i in jobs:
		i.start()

for i in jobs:
		i.join()
		
		
plt.imshow(bild)                                 
plt.jet() # hsv() # hot() # gray()                     
plt.show()