"""
Principe de récurrence
Auteur: Stéphane Pasquet
https://mathweb.fr
Date: 2021-09-06

Dans un terminal : manim recurrence.py principe ou manim -ql -p recurrence.py principe
"""

from manim import *

class principe(Scene):    
    def construct(self):
        self.camera.background_color = "#ffffff"
        
        # Titre & courspasquet.fr
        
        titre = Text("Démonstration par récurrence (Terminale)" , font_size=45 , color=GREEN).shift([0,3.5,0])
        self.play(Write(titre), run_time = 0.1)
        
        mathweb = Text("courspasquet.fr" , font_size=16 , color=BLUE).shift([6,-3.75,0])
        self.play(Write(mathweb), run_time = 0.5)
        
        # Rappel : points coins sup gauche : Dot([-7.1,3.95,0])        
        
        P1 = Text("On considère une propriété " , color=BLACK , font_size=25)
        P2 = Tex("$\\mathcal{P}_n$" , color=PURPLE , font_size=25)
        P3 = Text(" dépendant d'un entier naturel " , color=BLACK , font_size=25)
        P4 = Tex("$n$." , color=BLACK , font_size=25)
        
        P = VGroup(P1,P2,P3,P4).arrange(RIGHT).shift([0,2.5,0])
        self.play(Write(P), run_time = 0.5)
        
        self.wait(3)
        
        P5 = Text("On peut schématiser" , color=BLACK , font_size=20)
        P6 = Tex("$\\mathcal{P}_n$" , color=PURPLE , font_size=24)
        P7 = Text("par un escalier infini." , color=BLACK , font_size=20)
        
        PP = VGroup(P5,P6,P7).arrange(RIGHT).shift([3,1.5,0])
        
        self.play(Write(PP), run_time = 0.5)
        
        # escalier
        
        x = -5
        y = -2
        
        for i in range(7):
            A = Dot([x,y,0])
            B = Dot([x,y+0.5,0])
            C = Dot([x+0.5,y+0.5,0])
            AB = Line(A.get_center(),B.get_center()).set_color(PURPLE)
            self.play(Create(AB), run_time = 0.2)
            BC = Line(B.get_center(),C.get_center()).set_color(PURPLE)
            self.play(Create(BC), run_time = 0.2)
            x += 0.5
            y += 0.5
        
        M = Dot(point = [(-1.25,1.75,0)] , color=PURPLE , radius=0.01)
        N = Dot(point = [(-1.125,1.875,0)] , color=PURPLE,radius=0.01)
        Q = Dot(point = [(-1,2,0)] , color=PURPLE,radius=0.01)
        
        PPP = VGroup(M , N , Q)
        self.play(Create(PPP))
        
        # première marche
        
        P8 = Text("Si la première marche n'est pas solide," , color=RED , font_size=20)
        P9 = Text("on ne peut pas monter." , color=RED , font_size=20)
        
        PP2 = VGroup(P8,P9).arrange(DOWN).shift([3.5,0,0])
        self.play(Create(PP2))
        
        fleche = CurvedArrow([3.5,-1,0],[-4.75,-1.75,0],radius=-15).set_color(RED)
        self.play(Write(fleche), run_time = 0.5)
        
        A = Dot([-5,-2,0])
        B = Dot([-5,-1.5,0])
        C = Dot([-4.5,-1.5,0])
        AB = Line(A.get_center(),B.get_center()).set_color(RED)
        BC = Line(B.get_center(),C.get_center()).set_color(RED)
        self.play(Create(BC), Create(AB), run_time = 0.2)
        
        self.wait(5)
        
        P10 = Text("Il faut donc s'assurer que l'on peut y monter." , color=GREEN , font_size=20)
        P11 = Text("Cela revient à s'assurer que", color=GREEN , font_size=20)
        P12 = Tex("$P_0$", color=GREEN , font_size=24)
        P13 = Text("est vraie.", color=GREEN , font_size=20)
        
        PP3 = VGroup( P10 , VGroup(P11,P12,P13).arrange(RIGHT) ).arrange(DOWN).shift([3.5,-1,0])
        
        self.play(FadeOut(fleche),run_time=0.1)
        self.play(Create(PP3),run_time=0.5)
        
        self.wait(5)
        
        self.play(FadeOut(AB), FadeOut(BC) ,run_time=0.1)
        
        A = Dot([-5,-2,0])
        B = Dot([-5,-1.5,0])
        C = Dot([-4.5,-1.5,0])
        AB = Line(A.get_center(),B.get_center()).set_color(GREEN)
        BC = Line(B.get_center(),C.get_center()).set_color(GREEN)
        
        self.play(Create(BC), Create(AB), run_time = 0.2)
        
        P14 = Text("C'est l'initialisation." , color='#1e3368' , font_size = 24).shift([3.5,-2.5,0])
        self.play(Write(P14))
        
        fleche = CurvedArrow([3.5,-2.7,0],[-4.75,-1.75,0],radius=-15).set_color(RED)
        self.play(Write(fleche), run_time = 0.5)
        
        P15 = Tex("$\\mathcal{P}_0$" , color=GREEN , font_size=20)
        P16 = Text("vraie" , color=GREEN , font_size=20)
        
        PP4 = VGroup(P15,P16).arrange(RIGHT).shift([-6,-1.75,0])
        self.play(Write(PP4))
        
        self.wait(5)
        
        self.play(FadeOut(fleche), FadeOut(P14), FadeOut(PP3), FadeOut(PP2), FadeOut(PP), FadeOut(P), run_time=0.1)
        
        # hérédité
        
        P17 = Text("Supposons maintenant que l'on soit sur la k-ième marche, k étant arbitraire," , font_size = 16, color=ORANGE)
        P18 = Text("et que l'on puisse monter sur la marche suivante: la (k+1)-ième." , font_size = 16 , color=ORANGE)
        
        PP5 = VGroup(P17,P18).arrange(DOWN).shift([2.5,0,0])
        self.play(Write(PP5))
        
        P19 = Text("On appelle cela l'" , font_size = 20, color=ORANGE)
        P20 = Text("hypothèse de récurrence." , font_size = 20, color=ORANGE , slant=ITALIC)
        PP6 = VGroup(P19,P20).arrange(RIGHT).shift([2.5,-2,0])
        
        self.play(Write(PP6))
        
        fleche = CurvedArrow([2.5,-0.5,0],[-3.75,-0.75,0],radius=-15).set_color(RED)
        self.play(Write(fleche), run_time = 0.5)
        A = Dot([-4,-1,0])
        B = Dot([-4,-0.5,0])
        C = Dot([-3.5,-0.5,0])
        AB = Line(A.get_center(),B.get_center()).set_color(ORANGE)
        BC = Line(B.get_center(),C.get_center()).set_color(ORANGE)
        self.play(Create(BC), Create(AB), run_time = 0.2)
        
        fleche2 = CurvedArrow([-3.75,-0.22,0],[-3.25,0.25,0] , radius = -0.5).set_color(ORANGE)
        self.play(Write(fleche2), run_time = 0.5)
        
        self.wait(10)
        
        self.play(FadeOut(PP6), FadeOut(PP5), FadeOut(fleche), run_time=0.1)
        
        P21 = Text("Alors, si l'on est sur la (k+1)-ième marche," , font_size = 16, color=GREEN)
        P22 = Text("on pourra aller sur la suivante, la (k+2)-ième," , font_size = 16, color=GREEN)
        P23 = Text("car on sait monter d'une marche," , font_size = 16, color=GREEN)
        P24 = Text("d'après l'hypothèse de récurrence." , font_size = 16, color=GREEN)
        
        PP7 = VGroup(P21,P22,P23,P24).arrange(DOWN).shift([2.5,0,0])
        self.play(Write(PP7))
        
        self.wait(8)
        
        fleche3 = CurvedArrow([-3.25,0.25,0],[-2.75,0.75,0],radius=-0.5).set_color(GREEN)
        fleche4 = CurvedArrow([2.5,-1,0],[-2.75,0.25,0],radius=-7).set_color(GREEN)
        self.play(Write(fleche4), run_time = 0.5)
        self.play(Write(fleche3), run_time = 0.5)
        
        self.wait(5)
        
        P25 = Text("C'est l'" , font_size = 16, color=GREEN)
        P26 = Text("hérédité." , font_size = 20, color=GREEN , slant=ITALIC)
        PP8 = VGroup(P25,P26).arrange(RIGHT).shift([2.5,-2,0])
        self.play(Write(PP8))
        
        self.wait(5)
        
        P27 = Tex("$\\mathcal{P}_k$" , font_size = 20, color=ORANGE)
        P28 = Text("vraie", font_size = 16, color=ORANGE)
        P29 = Tex("$\\Rightarrow\\ \\mathcal{P}_{k+1}$" , font_size = 20, color=GREEN)
        P30 = Text("vraie", font_size = 16, color=GREEN)
        
        PP9 = VGroup(P27,P28,P29,P30).arrange(RIGHT).rotate(TAU / 8).shift([-3.75,0.75,0])
        self.play(Write(PP9))
        
        self.wait(5)
        
        self.play(FadeOut(PP9),FadeOut(PP8),FadeOut(fleche3),FadeOut(fleche4),FadeOut(fleche2),FadeOut(PP7),FadeOut(BC),FadeOut(AB))
        
        P31 = Text("Ces deux étapes constituent la" , font_size = 20, color=BLACK)
        P32 = Text("démonstration par récurrence" , font_size = 20, color=RED , slant=ITALIC)
        P33 = Text("qui permet de démontrer que" , font_size = 20, color=BLACK)
        P34 = Tex("$\\mathcal{P}_n$" , font_size = 24, color=BLACK)
        P35 = Text("est vraie quel que soit l'entier naturel" , font_size = 20, color=BLACK)
        P36 = Tex("$n$." , font_size = 24, color=BLACK)
        
        PP10 = VGroup(P31,P32,VGroup(P33,P34).arrange(RIGHT),VGroup(P35,P36).arrange(RIGHT)).arrange(DOWN).shift([3.5,0,0])
        
        self.play(Write(PP10))
        self.wait(5)
        
        # en résumé
        
        self.play(FadeOut(PP10))
        
        P37 = Text("En résumé..." , font_size=25,color=BLACK).shift([3.5,2.5,0])
        self.play(Write(P37))
        
        P38 = Text("Si" , font_size=25 , color=BLACK)
        P39 = Tex("$\\mathcal{P}_0$" , font_size=30,color=BLACK)
        P40 = Text("est vraie et si" , font_size=25 , color=BLACK)
        P41 = Tex("$\\mathcal{P}_k \\Rightarrow \\mathcal{P}_{k+1}$" , font_size=30,color=BLACK)
        P42 = Text("pour un" , font_size=25 , color=BLACK)
        P43 = Tex("$k$" , font_size=30,color=BLACK)
        P44 = Text("arbitraire, alors" , font_size=25 , color=BLACK)
        P45 = Tex("$\\mathcal{P}_n$" , font_size=30,color=BLACK)
        P46 = Text("est vraie pour tout entier naturel" , font_size=25 , color=BLACK)
        P47 = Tex("$n$." , font_size=30,color=BLACK)
        
        PPA = VGroup(P38,P39,P40).arrange(RIGHT)
        PPB = VGroup(P42,P43,P44).arrange(RIGHT)
        PPC = VGroup(P45,P46,P47).arrange(RIGHT)
        
        PR = VGroup(PPA,P41,PPB,PPC).arrange(DOWN).shift([3.5,0,0])
        self.play(Write(PR))
        
        A = Dot([-5,-2,0])
        B = Dot([-5,-1.5,0])
        C = Dot([-4.5,-1.5,0])
        AB = Line(A.get_center(),B.get_center()).set_color(GREEN)
        BC = Line(B.get_center(),C.get_center()).set_color(GREEN)
        fleche2 = CurvedArrow([-3.75,-0.22,0],[-3.25,0.25,0] , radius = -0.5).set_color(ORANGE)
        fleche3 = CurvedArrow([-3.25,0.25,0],[-2.75,0.75,0],radius=-0.5).set_color(GREEN)
        P15 = Tex("$\\mathcal{P}_0$" , color=GREEN , font_size=20)
        P16 = Text("vraie" , color=GREEN , font_size=20)
        PP4 = VGroup(P15,P16).arrange(RIGHT).shift([-6,-1.75,0])
        P27 = Tex("$\\mathcal{P}_k$" , font_size = 20, color=ORANGE)
        P28 = Text("vraie", font_size = 16, color=ORANGE)
        P29 = Tex("$\\Rightarrow\\ \\mathcal{P}_{k+1}$" , font_size = 20, color=GREEN)
        P30 = Text("vraie", font_size = 16, color=GREEN)
        PP9 = VGroup(P27,P28,P29,P30).arrange(RIGHT).rotate(TAU / 8).shift([-3.75,0.75,0])
        self.play(Create(BC),Create(fleche2),Create(fleche3),Create(AB),Write(PP4),Write(PP9), run_time = 0.2)
        
        self.wait(10)
        