# -*- coding: utf-8 -*-
"""
Created on Sat Sep 27 17:00:08 2014

@author: dconduche
"""

import numpy as np
import matplotlib.pyplot as plt
plt.set_cmap('gray')


angle = np.pi/6
    #angle entre les branches
hauteur1 = .40
    #depart de branche gauche (en proportion de la hauteur totale)
hauteur2 = .60
    #idem droite
dimin = .65
    #diminution à chaque itération


def rotation(point, centre, theta):
    x, y = point - centre
    return (x*np.cos(theta)-y*np.sin(theta),
            x*np.sin(theta)+y*np.cos(theta))+centre


def tracer_segment2(point_debut, point_fin):
    # alternative à écrire : utiliser plot de matplotlib.pyplot
    return None


def tracer_segment(point_debut, point_fin):
    lgtx, lgty = point_fin - point_debut
    lgtx, lgty = int(lgtx), int(lgty)
    x, y = point_debut
    if abs(lgtx) >= abs(lgty):
        if lgtx < 0:
            signe = -1
        else:
            signe = 1
        for i in range(1, abs(lgtx)+1):
            image[x+i*signe, y+i*lgty/lgtx] = 1
    else:
        if lgty < 0:
            signe = -1
        else:
            signe = 1
        for i in range(1, abs(lgty)+1):
            image[x+i*lgtx/lgty, y+i*signe] = 1
    return None


#def etoile(point, d, n):  # pour tester
#    theta = 2*np.pi/n
#    for k in range(n):
#        point_fin = rotation(point + np.array([0, d]), point, k*theta)
#        tracer_segment(point, point_fin)


def branche_rec(poin, d, theta):
    point = poin.copy()
    point_fin = rotation(point + np.array([0, d]), point, theta)
    tracer_segment(point, point_fin)
    if d > 10:
        branchement1 = rotation(point+np.array([0, d*hauteur1]), point, theta)
        branchement2 = rotation(point+np.array([0, d*hauteur2]), point, theta)
        branche_rec(branchement1, d*dimin, theta-angle)
        branche_rec(branchement2, d*dimin, theta+angle)

N = 700
xpixels, ypixels = 2*N, N
image = np.zeros((xpixels, ypixels))

d = 2*N//3
point = np.array([xpixels//2, 0])
branche_rec(point, d, 0)


#etoile(point,N//3,12)
plt.imshow(1-image, interpolation='none')
plt.show()

"""Rmq : aucun controle qu'on ne sort pas de l'image"""

#N = 1000
#n = 10
#perturbation = np.array([[rd.randrange(10) for k in range(N)]
#                         for kk in range(N)])
#nouv_image = np.zeros((N, N))
#for k in range(n):
#    nouv_image[int(k*N/n), :] = 255
#    nouv_image[:, int(k*N/n)] = 255
#nouv_image = 255 - nouv_image
#nouv_image = nouv_image + perturbation
#
#plt.imshow(nouv_image, interpolation='none')
#plt.show()
#plt.imshow(np.linalg.inv(nouv_image), interpolation='none')
#plt.show()
