from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np  
from random import random

#Q_15 et Q_16 voir fichier MvtBrownien.py

#Q_17 et Q_18  voir UrnesEhrenfest.py

# Q_19 #########################################☺
def nuage(n):
    plt.clf()
    x=n*[0]
    y=n*[0]
    for i in range(n):
        r=random()
        t=random()*2*np.pi
        x[i]=r*np.cos(t)
        y[i]=r*np.sin(t)
    #MONTE-CARLO
    plt.scatter(x,y,s=5)  #s donne la grosseur des points
    plt.title('Nuage de points avec Matplotlib')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.axis('equal') # pour avoir des axes orthonormés
    plt.show()
    
def nuagebis(n):
    plt.clf()
    x=n*[0]
    y=n*[0]
    for i in range(n):
        r=np.sqrt(random()) # pour une répartition unfiforme
        t=random()*2*np.pi
        x[i] = r*np.cos(t)
        y[i] = r*np.sin(t)
    plt.scatter(x,y,s=10)  
    plt.title('Nuage de points répartis avec Matplotlib')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.axis('equal') 
    plt.show()
  

# Q_20 et Q_21 #########################################☺
def traceCardioide(n): 
    plt.clf()
    T=np.linspace(0,2*np.pi,101)
    xt=np.cos(T)*(1+np.cos(T))
    yt=np.sin(T)*(1+np.cos(T))
    cerclext=np.cos(T)*(2.5)
    cercleyt=np.sin(T)*(2.5)

    x=n*[0]
    y=n*[0]
    
    nb=0
    
    for i in range(n):
        r=random()*2.5**2
        t=random()*2*np.pi
        x[i]=np.sqrt(r)*np.cos(t)
        y[i]=np.sqrt(r)*np.sin(t)
        if np.sqrt(r)<1+np.cos(t):
            nb+=1
    plt.scatter(x,y,s=1) #s donne la grosseur des points
    plt.title('cardioide')    
    plt.xlabel('x')
    plt.ylabel('y')
    plt.axis('equal') 
    plt.plot(xt,yt,linewidth=4,color='red')
    plt.plot(cerclext,cercleyt,linewidth=1,color='g')

    plt.show()
    print('Valeur approchée (MonteCarlo) avec ',n,'points : ',
    nb/n*np.pi*2.5**2)
    print('Valeur exacte (avec les intégrales doubles) : ',3*np.pi/2)





#close() sinon pas de dessin

# Q_22#########################################☺

def trace(): #trace l'hyperboloide à une nappe
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    u = np.linspace(0, 2 * np.pi, 100)
    v = np.linspace(-1,1, 100)
    x = np.outer(np.cos(u), np.cosh(v)) 
    y = np.outer(np.sin(u), np.cosh(v))
    z = np.outer(np.ones(np.size(u)), np.sinh(v))
    ax.plot_surface(x, y, z,  rstride=4, cstride=4, color='g')
    plt.show()

cube=4*4*2;

# Q_23 #########################################☺
def al(b): # genere un nb alea entre -b et b
    return -b+2*b*random()

# Q_24 #########################################☺
def montecarlo(n):
    nbdedans=0;nbdehors=0;
    for i in range(n):
        x=al(2);y=al(2);z=al(1);
        if (x**2+y**2-z**2)<=1 and z**2<=1:
            nbdedans=nbdedans+1;
        else:
            nbdehors=nbdehors+1;
    return(cube*nbdedans/n,nbdedans,nbdehors)
 
# Q_25 #########################################☺
def vol(nb,max):
    s=0
    for i in range(nb):
        s=s+montecarlo(max)[0]
    return s/nb

# Valeur exacte avec les intégrales triples : 8*Pi/3
