begin process at 2008 07 05 20:28:21
1 205 339 membres
308 nouveaux aujourd'hui
14 119 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

TRACER UNE FONCTION


Information sur la source

Catégorie :Divers Classé sous : fonction, Tracer, tracer fonction Niveau : Débutant Date de création : 23/12/2007 Vu : 1 749

Note :
7 / 10 - par 1 personne
7,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

Description

Juste une appli me permettant de tracer des fonction:
Exemple: cos(x)
Nota Bene:Entrer (x)**2 au lieu de x**2

Source

  • # -*- coding: iso8859-15 -*-
  • from Tkinter import*
  • from math import*
  • import tkMessageBox
  • ref=[]
  • dic={}
  • class Application(Frame):
  • """Mise en place de l'interface"""
  • def __init__(self):
  • Frame.__init__(self)
  • self.master.title("::.Ma fonction.::-By Kouakou021988")
  • self.configure(bg="grey40",bd=0,relief=FLAT)
  • self.master.resizable(width=False, height=False)
  • self.master.protocol("WM_DELETE_WINDOW", self.quitter)
  • self.pack(side=TOP)
  • self.coul=['red','blue','yellow','green']
  • self.coord=[]
  • self.courb=['none']
  • self.build()
  • def build(self):
  • """mise en place des bouton,..."""
  • Label(self,text="f(x): ",font='arial 10 italic',background='grey40',fg='white').pack(side=LEFT)
  • self.ent_fct=Entry(self,width=25,font='arial 10 bold',relief=FLAT,fg='black')
  • self.ent_fct.pack(side=LEFT,fill=BOTH,expand=1)
  • Button(self,text="Tracer",font='arial 8 bold',background='grey40',
  • foreground='white',command=self.traceur).pack(side=RIGHT)
  • self.can=Canvas(self.master,bg='black',height=400,width=600)
  • self.can.pack(side=LEFT,fill=BOTH,expand=1)
  • pas=25
  • self.can.create_line(10,200,600,200,arrow=LAST,fill='white') #axe x
  • for x in range(1,11):
  • stx=300+x*pas
  • self.can.create_line(stx,0,stx,400,fill='grey40') #196/204
  • self.can.create_text(stx,210,text=str(x),fill='white',font='arial 6')
  • for x in range(1,11):
  • stx=300-x*pas
  • self.can.create_line(stx,0,stx,400,fill='grey40')
  • self.can.create_text(stx,210,text='-'+str(x),fill='white',font='arial 6')
  • self.can.create_line(300,395,300,5,arrow=LAST,fill='white') #axe y
  • for y in range(1,8):
  • sty=200+y*pas
  • self.can.create_line(0,sty,600,sty,fill='grey40') #296/304
  • self.can.create_text(310,sty,text='-'+str(y),fill='white',font='arial 6')
  • for y in range(1,8):
  • sty=200-y*pas
  • self.can.create_line(0,sty,600,sty,fill='grey40')
  • self.can.create_text(310,sty,text=str(y),fill='white',font='arial 6')
  • self.can.create_text(305,210,text=str(0),fill='white',font='arial 6')
  • fram=Frame(self.master)
  • fram.pack(side=RIGHT)
  • self.lstbox=Listbox(fram,height=5,font='arial 8 bold',fg='black')
  • self.lstbox.pack(side=TOP)
  • self.lstbox.insert(END,'none')
  • Button(fram,text="Reset",font='arial 8',command=self.reset).pack(side=LEFT)
  • self.re=Button(fram,text="Effacer Tracé",font='arial 8',command=self.efface)
  • self.re.pack(side=LEFT)
  • self.re.config(state=DISABLED)
  • self.lstbox.bind("<Button-1>",self.option)
  • def option(self,event=None):
  • self.master.update()
  • y=event.y
  • y=self.lstbox.nearest(y)
  • y=self.lstbox.get(y)
  • if y!='none':
  • if ref:
  • for i in ref:
  • for x in i: self.can.itemconfig(x, width =1)
  • self.re.config(state=NORMAL)
  • self.y=y #nom de la fct dans le listbox
  • self.coord=dic[self.y]
  • ref.append(self.coord)
  • for i in self.coord:
  • self.can.itemconfig(i, width =3)
  • self.can.lift(i)
  • if y=='none':
  • if self.coord:
  • for i in self.coord:
  • self.can.itemconfig(i, width =1)
  • self.re.config(state=DISABLED)
  • def efface(self):
  • try:
  • for i in self.coord:
  • self.can.delete(i)
  • s=self.courb.index(self.y)
  • self.lstbox.delete(s)
  • self.courb.remove(self.y)
  • self.initialise(self.y)
  • fin=dic.pop(self.y)
  • self.re.config(state=DISABLED)
  • except: pass
  • def initialise(self,f):
  • c=f.split('-->')[1]
  • c=c[1:len(c)-1]
  • self.coul.append(c)
  • def traceur(self):
  • fct=self.ent_fct.get()
  • if fct:
  • try: c=self.coul.pop()
  • except: tkMessageBox.showerror("Ma fonction","Limite de fonction atteinte")
  • else:
  • self.ph="f(x)="+fct+"-->("+c+")"
  • self.courbe(fct,c)
  • self.lstbox.insert(END,self.ph)
  • self.courb.append(self.ph)
  • else: pass
  • def reset(self):
  • try:
  • l=dic.keys()
  • for i in l:
  • a=dic.pop(i)
  • for x in a: self.can.delete(x)
  • self.lstbox.delete(1,END)
  • except: pass
  • self.coul=['red','blue','yellow','green']
  • def courbe(self,fonction="(x)**2.",color="red"):
  • """Courbe de la fonction:'(x)**2.' par defaut"""
  • self.func=fonction
  • curve=[]
  • x=0
  • while x<12:
  • try:
  • if "exp" in self.func:
  • self.func=fonction.replace('exp','zzz')
  • self.func=self.func.replace('x',str(x))
  • self.func=self.func.replace('zzz','exp')
  • y=eval(self.func)
  • else: y=eval(self.func.replace('x',str(x)))
  • except: pass
  • else: curve.append((x*25+300,(-y)*25+200))
  • x+=0.01
  • try: a=self.can.create_line(curve,fill=color,smooth=1)
  • except: a=""
  • x=0
  • curve=[]
  • while x>-12.0:
  • try:
  • if "exp" in self.func:
  • self.func=fonction.replace('exp','zzz')
  • self.func=self.func.replace('x',str(x))
  • self.func=self.func.replace('zzz','exp')
  • y=eval(self.func)
  • else: y=eval(self.func.replace('x',str(x)))
  • except: pass
  • else: curve.append((x*25+300,(-y)*25+200))
  • x-=0.01
  • try: b=self.can.create_line(curve,fill=color,smooth=1)
  • except: b=""
  • dic[self.ph]=(a,b)
  • def test(self,v):
  • pass
  • def quitter(self):
  • self.can.delete(ALL)
  • self.master.destroy()
  • ###--------------------------------------------------------------##############
  • if __name__=='__main__':
  • app=Application()
  • app.mainloop()
# -*- coding: iso8859-15 -*-

from Tkinter import*
from math import*
import tkMessageBox


ref=[]
dic={}
class Application(Frame):
        """Mise en place de l'interface"""
        def __init__(self):
                Frame.__init__(self)
                self.master.title("::.Ma fonction.::-By Kouakou021988")
                self.configure(bg="grey40",bd=0,relief=FLAT)
                self.master.resizable(width=False, height=False)
                self.master.protocol("WM_DELETE_WINDOW", self.quitter)
                self.pack(side=TOP)
                self.coul=['red','blue','yellow','green']
                self.coord=[]
                self.courb=['none']
                self.build()

        def build(self):
                """mise en place des bouton,..."""
                Label(self,text="f(x): ",font='arial 10 italic',background='grey40',fg='white').pack(side=LEFT)
                self.ent_fct=Entry(self,width=25,font='arial 10 bold',relief=FLAT,fg='black')
                self.ent_fct.pack(side=LEFT,fill=BOTH,expand=1)
                Button(self,text="Tracer",font='arial 8 bold',background='grey40',
                       foreground='white',command=self.traceur).pack(side=RIGHT)
                self.can=Canvas(self.master,bg='black',height=400,width=600)
                self.can.pack(side=LEFT,fill=BOTH,expand=1)
                pas=25
                self.can.create_line(10,200,600,200,arrow=LAST,fill='white')        #axe x
                for x in range(1,11):
                        stx=300+x*pas
                        self.can.create_line(stx,0,stx,400,fill='grey40')           #196/204
                        self.can.create_text(stx,210,text=str(x),fill='white',font='arial 6')
                for x in range(1,11):
                        stx=300-x*pas
                        self.can.create_line(stx,0,stx,400,fill='grey40')
                        self.can.create_text(stx,210,text='-'+str(x),fill='white',font='arial 6')
                self.can.create_line(300,395,300,5,arrow=LAST,fill='white')         #axe y
                for y in range(1,8):
                        sty=200+y*pas
                        self.can.create_line(0,sty,600,sty,fill='grey40')           #296/304
                        self.can.create_text(310,sty,text='-'+str(y),fill='white',font='arial 6')
                for y in range(1,8):
                        sty=200-y*pas
                        self.can.create_line(0,sty,600,sty,fill='grey40')
                        self.can.create_text(310,sty,text=str(y),fill='white',font='arial 6')
                self.can.create_text(305,210,text=str(0),fill='white',font='arial 6')
                fram=Frame(self.master)
                fram.pack(side=RIGHT)
                self.lstbox=Listbox(fram,height=5,font='arial 8 bold',fg='black')
                self.lstbox.pack(side=TOP)
                self.lstbox.insert(END,'none')
                Button(fram,text="Reset",font='arial 8',command=self.reset).pack(side=LEFT)
                self.re=Button(fram,text="Effacer Tracé",font='arial 8',command=self.efface)
                self.re.pack(side=LEFT)
                self.re.config(state=DISABLED)
                self.lstbox.bind("<Button-1>",self.option)

        def option(self,event=None):
                self.master.update()
                y=event.y
                y=self.lstbox.nearest(y)
                y=self.lstbox.get(y)
                if y!='none':
                        if ref:
                                for i in ref:
                                        for x in i: self.can.itemconfig(x, width =1)   
                        self.re.config(state=NORMAL)
                        self.y=y                                                            #nom de la fct dans le listbox
                        self.coord=dic[self.y]
                        ref.append(self.coord)
                        for i in self.coord:
                                self.can.itemconfig(i, width =3)
                                self.can.lift(i)
                if y=='none':
                        if self.coord:
                                for i in self.coord:
                                        self.can.itemconfig(i, width =1)
                                        self.re.config(state=DISABLED)
                                
        def efface(self):
                try:
                        for i in self.coord:
                                self.can.delete(i)
                        s=self.courb.index(self.y)
                        self.lstbox.delete(s)
                        self.courb.remove(self.y)
                        self.initialise(self.y)
                        fin=dic.pop(self.y)
                        self.re.config(state=DISABLED)
                except: pass

        def initialise(self,f):
                c=f.split('-->')[1]
                c=c[1:len(c)-1]
                self.coul.append(c)
                
        def traceur(self):
                fct=self.ent_fct.get()
                if fct:
                        try:    c=self.coul.pop()
                        except: tkMessageBox.showerror("Ma fonction","Limite de fonction atteinte")
                        else:
                                self.ph="f(x)="+fct+"-->("+c+")"
                                self.courbe(fct,c)
                                self.lstbox.insert(END,self.ph)
                                self.courb.append(self.ph)
                else:   pass
                
        def reset(self):
                try:
                        l=dic.keys()
                        for i in l:
                                a=dic.pop(i)
                                for x in a: self.can.delete(x)
                        self.lstbox.delete(1,END)
                except: pass
                self.coul=['red','blue','yellow','green']
                
        def courbe(self,fonction="(x)**2.",color="red"):
                """Courbe de la fonction:'(x)**2.' par defaut"""
                self.func=fonction
                curve=[]
                x=0
                while x<12:
                        try:
                                if "exp" in self.func:
                                        self.func=fonction.replace('exp','zzz')
                                        self.func=self.func.replace('x',str(x))
                                        self.func=self.func.replace('zzz','exp')
                                        y=eval(self.func)
                                else:   y=eval(self.func.replace('x',str(x)))
                        except: pass
                        else:   curve.append((x*25+300,(-y)*25+200))
                        x+=0.01
                try:    a=self.can.create_line(curve,fill=color,smooth=1)
                except: a=""
                x=0
                curve=[]
                while x>-12.0:
                        try:
                                if "exp" in self.func:
                                        self.func=fonction.replace('exp','zzz')
                                        self.func=self.func.replace('x',str(x))
                                        self.func=self.func.replace('zzz','exp')
                                        y=eval(self.func)
                                else:   y=eval(self.func.replace('x',str(x)))
                        except: pass
                        else:   curve.append((x*25+300,(-y)*25+200))
                        x-=0.01
                try:    b=self.can.create_line(curve,fill=color,smooth=1)
                except: b=""
                dic[self.ph]=(a,b)

        def test(self,v):
                pass
            
        def quitter(self):
                self.can.delete(ALL)
                self.master.destroy()



###--------------------------------------------------------------##############
if __name__=='__main__':
        app=Application()
        app.mainloop()

Conclusion

Merci.
  • signaler à un administrateur
    Commentaire de aera group le 04/01/2008 10:42:06 7/10

    Ta source n'est pas mal, mais .... il y en existe une autre bien plus "puissante" :
    http://www.pythonfrance.com/codes/TRACEUR-FONCTION-MATHEMATIQUE_29942.aspx
    Voila, je met quand même 7/10, parce que c'est tout de même pas mal !
    Bon courrage pour la suite

Ajouter un commentaire

Pub



Appels d'offres

Plugin Dialer outlook
Budget : 2 000€
Travail graphique- ill...
Budget : 1 000€
creation de marque et ...
Budget : 1 000€

CalendriCode

Juillet 2008
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Téléchargements

Logiciels à télécharger sur le même thème :

Boutique

Boutique de goodies CodeS-SourceS