# -*- coding: utf-8 -*-
"""
Created on Mon Nov 16 11:15:56 2020

@author: dconduche
"""

import numpy as np
import matplotlib.pyplot as plt


def cantor(n, x):
    """Keyword arguments:
    n -- entier positif
    x -- float dans l'intervalle [0, 1]
    retourne la valeur en x de la fonction f_n
    """
    if n == 0:
        return x
    if x <= 1/3:
        return cantor(n-1, 3*x)/2
    elif x < 2/3:
        return 1/2
    else:
        return (1 + cantor(n-1, 3*x-2))/2


"""Initialise l'affichage dans un mode 'cours de maths'"""
ax = plt.gca()
 #Axes horizontaux.
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
 #Axes verticaux.
ax.spines['right'].set_color('none')
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

a, b = 0, 1
x = np.linspace(a, b, 1000)
ntot = 9
for n in range(ntot+1):
    y = [cantor(n, t) for t in x]
    ax.plot(x, y, label='$f_{}$'.format(n))
    # c=(n/ntot, ((n+1)%ntot)/ntot, ((n+2)%ntot)/ntot),
ax.legend(loc='best')
plt.savefig('DS2_2020_trace{}.pdf'.format(ntot))
