# Projet d'algorithme de génération de courbes par morceaux
from math import* ; import numpy as np ;

#Fonction transformation str en coordonées 
def convert(x):
    ind=x.find(',')
    c1=float(x[0:ind])
    x=x[ind+1:]
    ind=x.find(',')
    c2=float(x[0:ind])
    x=x[ind+1:]
    c3=float(x)
    res=(c1,c2,c3)
    return res

#Demande des coordonnées 
L=[] #Liste de points
n=int(input("Nombre de points contraintes :"))
for i in range(n): # Saisie des points
    point=input("Saisir point sous la forme :  x, y, pente :")
    vect=convert(point)
    L=L+[vect]

print("\n")

# Calcul de la fonction polynômiale par morceaux
for i in range(0,len(L)-1) :
    #Affecations aux variables ponctuelles
    xA=L[i][0]*1.0 ; yA=L[i][1]*1.0 ; xB=L[i+1][0]*1.0 ; yB=L[i+1][1]*1.0   ; a=L[i][2]; b=L[i+1][2];
    #Constitution de la matrice puis inversion
    M=np.array(([xA**3, xA**2, xA, 1.0],[xB**3,xB**2,xB,1],[3*(xA**2), 2*xA,1,0],[3*(xB**2),2*xB,1,0]))
    M2=np.linalg.inv(M)
    V=np.array(([yA],[yB],[a],[b])) #Vecteurs de valeurs contraintes
    res=M2.dot(V) #Produit matriciel
    # Constitution de la chaîne de caractères 
    # Pour Latex PGF / TikZ
    sfonc='('+str(round(res[0][0],5))+'*(x)^3+'+str(round(res[1][0],5))+'*(x)^2+'+str(round(res[2][0],5))+'*(x)+'+str(round(res[3][0],5))+')/1.0});'
    s='\draw [samples=50,rotate around={0.:(0.,0.)},xshift=0.cm,yshift=0.cm,line width=1.2pt,domain='+str(xA)+':'+str(xB)+')] plot (x,{'+sfonc+');'
    print(s)
    

