polynome_2.py

Created by alex-juge84

Created on November 16, 2020

5.34 KB

Utilitaire graphique pour les polynômes du second degrés. V-1.0.2


#Import modules
from math import *
from kandinsky import *
from ion import *
from time import sleep

B = (255,255,255)
F = (220,220,220)
N = (0,0,0)


#---Fonctions utilitaires---

def col_os(): # Cf - Cent20
    try:
        get_keys()
        return (192, 53, 53 - 5)
    except:
        return (255, 183, 52)

def wait(buttons=range(53)):  # Attends qu'une des touches précisées soit pressée - Cf Arthur J.
    while True:
        for i in buttons:
            if keydown(i):
                while keydown(i): True  # Fonction anti-rebond
                return i


def texte_center(texte, x=160, y=110, col1=(255, 255, 255), col2=(255, 255, 255)):
    '''Cette fonction affiche un texte et le centre'''
    draw_string(texte, x - len(texte) * 10 // 2, y, col1, col2)


def changement(s):
    Menus = ["Poly","Facto","Poly","Var","Signe","Poly"]
    draw_string("{} >".format(Menus[s+1]),248,205,B,col_os())
    draw_string("< {}".format(Menus[s-1]),10,205,B,col_os())


def interface(titre,s=2):
    fill_rect(0, 0, 320, 222, F)
    fill_rect(0, 202, 320, 222, col_os())
    fill_rect(0, 0, 320, 20, (70,70,70))
    texte_center(titre, 160, 2, B, (70,70,70))
    changement(s)


def ligne(x1,y1,x2,y2,col=(0,0,0),pas=1):
    if x1==x2:
        for i in range(y1,y2,pas):
            set_pixel(x1,i,col)
    elif y1==y2:
        for i in range(x1,x2,pas):
            set_pixel(i,y1,col) 
    else :
        a = (y2-y1)/(x2-x1)
        for i,x in enumerate(range(x1,x2,pas)):
            set_pixel(x,round(y1+a*i),col)
            set_pixel(x+1,round(y1+a*i),col)

def rectangle(x1,y1,x2,y2):
    ligne(x1,y1,x2,y1)
    ligne(x1,y1,x1,y2)
    ligne(x2,y1,x2,y2)
    ligne(x1,y2,x2,y2)

def racines(a,b,c,delta):
    x1 = round( (-b-sqrt(delta)) / (2*a), 2)
    x2 = round( (-b+sqrt(delta)) / (2*a), 2)
    print(x1,x2)
    return x1,x2

def signe(n):
    if n < 0:
        return "-"
    else :
        return "+"



#-----------------------

#Partie texte
def play():
    print("\n"*11)
    a = float(input("\ta = "))
    b = float(input("\tb = "))
    c = float(input("\tc = "))
    delta = b**2-4*a*c
    poly(a,b,c,delta)

#Boucles graphiques
def poly(a,b,c,delta):
    poly = "{}x^2 + {}x + {}".format(a,b,c)
    interface("Polynome")
    fill_rect(30,35,250,140,B)
    texte_center("Le polynome est",160,40,col_os())
    texte_center(poly,160,70,N)
    texte_center("Delta = {}".format(delta),160,100,(42,42,42))
    if delta > 0:
        x1, x2 = racines(a,b,c,delta)
        texte_center("x1 = {}, x2 = {}".format(x1,x2), 160,130,N)
    elif delta ==0:
        texte_center("x0 = {}".format(-b/(2*a)), 160,130,N)
    else :
        texte_center("pas de racines reelles",160,130,N)


    touche = wait([0,3])
    if touche == 0:
        facto(a, b, c, delta)
    elif touche == 3:
        var(a,b,c,delta)
  
def facto(a, b, c, delta):
    interface("Factorisation", 1)
    fill_rect(20,35,280,150,B)
    if delta > 0:
        x1,x2 = racines(a,b,c,delta)
        f = "{}(x-({}))(x-({}))".format(a,x1,x2)
        texte_center("Delta > 0",160,40,col_os())
        texte_center("Forme factorisee :",160,70,N)
        texte_center(f,160,100,N)

    elif delta == 0:
        x0 = -b/(2*a)
        f = "{}(x-({}))^2".format(a,x0)
        texte_center("Delta = 0",160,40,col_os())
        texte_center("Forme factorisee :",160,70,N)
        texte_center(f,160,100,N)
    
    else :
        texte_center("Delta < 0",160,40,col_os())
        texte_center("Pas de racines reelles",160,70,N)
        texte_center("Pas de Factorisation",160,100,N)



    touche = wait([0, 3])
    if touche==3:
        poly(a, b, c, delta)
    elif touche==0:
        poly(a, b, c, delta)

def var(a, b, c, delta):
    interface("Variation", 3)
    alpha = -b/(2*a)
    beta = -delta/(4*a)

    fill_rect(20,35,280,150,B)
    rectangle(40,55,270,160)
    ligne(85,55,85,160)
    ligne(40,90,270,90)

    draw_string("x",55,62)
    draw_string("f(x)",44,120)
    draw_string("-I",87,62)
    draw_string("+I",240,62)
    texte_center(str(round(alpha,4)),172,62,N)

    if a < 0:
        texte_center(str(round(beta,4)),178,95,N)
        ligne(100,150,140,110)
        ligne(215,110,255,150)
    elif a > 0:
        texte_center(str(round(beta,4)),178,140,N)
        ligne(100,110,140,150)
        ligne(215,150,255,110)

    touche = wait([0, 3])
    if touche == 0:
        poly(a, b, c, delta)
    elif touche == 3:
        T_signe(a, b, c, delta)

def T_signe(a, b, c, delta):
    interface("Signe", 4)
    fill_rect(10,35,300,150,B)
    rectangle(30,55,290,160)
    ligne(30,100,290,100)
    ligne(80,55,80,160)
    draw_string("x",50,65)
    draw_string("f(x)",38,120)
    draw_string("-I",82,65)
    draw_string("+I",267,65)


    if delta < 0:
        draw_string(signe(a), 178, 120, N)

    elif delta == 0:
        draw_string(signe(a),100,120,N)
        draw_string(signe(a),250,120,N)
        ligne(178,100,178,160)
        x0 = str(round(-b/(2*a),2))
        draw_string(x0,172,62,N)
    
    else :
        x1, x2 = racines(a, b, c, delta)
        draw_string(signe(a),100,120,N)
        draw_string(signe(-a),178,120,N)
        draw_string(signe(a),250,120,N)

        ligne(140,100,140,160)
        ligne(230,100,230,160)

        draw_string(str(x1),126,65,N)
        draw_string(str(x2),200,65,N)



    

    touche = wait([0, 3])
    if touche == 0:
        var(a, b, c, delta)
    elif touche == 3:
        poly(a, b, c, delta)









play()