"""
Les différents triangles (animation)
Auteur: Stéphane Pasquet
https://mathweb.fr
Date: 2021-08-20

Dans un terminal : manim trinome.py types ou manim -ql -p trinome.py types
"""

from manim import *

class types(Scene):    
    def construct(self):
        self.camera.background_color = "#ffffff"
        
        # Titre & courspasquet.fr
        
        titre = Tex("$P(x)=ax^2+bx+c$").set_color(GREEN).shift([-4.5,3.5,0]).scale(0.75)
        self.play(Write(titre), run_time = 0.1)
        
        mathweb = Text("courspasquet.fr").shift([6,-3.75,0]).scale(0.33).set_color(BLUE)
        self.play(Write(mathweb), run_time = 0.1)
        
        sgna = Tex('$a>0$').set_color(RED).shift([-4.5,2.5,0])
        self.play(Write(sgna))
        
        delta = Tex('$\Delta=b^2-4ac$').set_color(PURPLE).shift([-4.5,1.5,0])
        self.play(Write(delta))
        
        # Rappel : points coins sup gauche : Dot([-7.1,3.95,0])        
        
        ax = Axes( x_range=[-10, 10], y_range=[-5, 5], axis_config={"include_tip": False} ).set_color(BLACK)
        labels = ax.get_axis_labels(x_label="x", y_label="P(x)").set_color(BLACK)
        
        def f(x):
            return 0.75*(x+1)**2 - (x+1) +  1
        graphN = ax.get_graph(f, color=MAROON)
        texteN = Tex('$\Delta<0$').set_color(MAROON).shift([3.5,2,0])
        descrN = Text("Pas de racine donc pas d'intersection avec l'axe des abscisses.").scale(0.25).set_color(PINK).shift([3.5,1,0])
        signeN1 = Tex("$P(x)$").scale(0.4).set_color(ORANGE)
        signeN2 = Text("ne se factorise pas et est du signe de").scale(0.25).set_color(ORANGE)
        signeN3 = Tex("$a$").scale(0.4).set_color(ORANGE)
        signeN = VGroup(signeN1,signeN2,signeN3).arrange(RIGHT).shift([3.5,0.5,0])
        
        def g(x):
            return 2 * (x - 5) ** 2
        graph0 = ax.get_graph(g, color=MAROON)
        texte0 = Tex('$\Delta=0$').set_color(MAROON).shift([3.5,-1,0])
        descr0 = Text("Une racine donc une intersection avec l'axe des abscisses.").scale(0.25).set_color(PINK).shift([3.5,-2,0])
        signe01 = Tex("$P(x)=a(x-x_0)^2$").scale(0.6).set_color(ORANGE)
        signe02 = Text("est du signe de").scale(0.5).set_color(ORANGE)
        signe03 = Tex("$a$").scale(0.6).set_color(ORANGE)
        signe0 = VGroup(signe01,signe02,signe03).arrange(RIGHT).shift([3.5,-2.5,0])
        x0 = Tex("$x_0$" , color=RED).shift([3,-0.2,0]).scale(0.5)
        
        def h(x):
            return 2 * (x - 5) ** 2 - 3
        graphP = ax.get_graph(h, color=MAROON)
        texteP = Tex('$\Delta>0$').set_color(MAROON).shift([3.5,-2.5,0])
        descrP = Text("Deux racines donc deux intersections avec l'axe des abscisses.").scale(0.25).set_color(PINK).shift([3.5,-3,0])
        signeP = Tex("$P(x)=a(x-x_1)(x-x_2)$").scale(0.5).set_color(ORANGE).shift([3.5,-3.5,0])
        x1 = Tex('$x_1$' , color=ORANGE).shift([2.1,-0.2,0]).scale(0.5)
        x2 = Tex('$x_2$' , color=ORANGE).shift([3.9,-0.2,0]).scale(0.5)
        
        self.add(ax, labels)
        
        # aucune solution
        
        self.play(Create(graphN))
        self.play(Write(texteN),Write(descrN),Write(signeN))
        
        self.wait(10)
        
        # 1 solution
        
        self.play(Transform(graphN,graph0) , Transform(texteN,texte0), Transform(descrN,descr0), Transform(signeN,signe0),\
                  Write(x0))
        
        self.wait(10)
        
        # 2 solutions
        
        self.play(FadeOut(x0))
        
        self.play(Transform(graphN,graphP) , Transform(texteN,texteP), Transform(descrN,descrP) , Transform(signeN,signeP),\
                  Write(x1), Write(x2))
        
        self.wait(10)
        
        self.play(FadeOut(graphN,texteN,descrN,delta,x2,x1,signeN))
        
        # influence de a
        
        graphN = ax.get_graph(f, color=MAROON)
        T1 = Text('Les branches sont vers le haut').set_color(RED).shift([-4.5,1.5,0]).scale(0.25)
        T2 = Text('Les branches sont vers le bas').set_color(RED).shift([-4.5,-3,0]).scale(0.25)
        self.play(Create(graphN), Write(T1))
        
        self.wait(5)
        
        def p(x):
            return -0.75*(x+1)**2 - (x+1) +  1
        graphNN = ax.get_graph(p, color=MAROON)
        sgnaN = Tex('$a<0$').set_color(RED).shift([-4.5,-2.5,0])
        self.play(Transform(graphN,graphNN),Transform(sgna,sgnaN),Transform(T1,T2))
        
        self.wait(5)
        
        