Draw Math Curve : Curve : 2D PYTHON examples


PYTHON examples » 2D » Curve »

 

Draw Math Curve


Draw Math Curve


from Tkinter import *
import math

root = Tk()

fram = Frame(root)
Label(fram,text='f(x):').pack(side=LEFT)
func = Entry(fram)
func.pack(side=LEFT, fill=BOTH, expand=1)
butt = Button(fram, text='Plot')
butt.pack(side=RIGHT)
fram.pack(side=TOP)

fram = Frame(root)
bounds = []
for label in 'minX', 'maxX', 'minY', 'maxY':
    Label(fram,text=label+':').pack(side=LEFT)
    edit = Entry(fram, width=6)
    edit.pack(side=LEFT)
    bounds.append(edit)
fram.pack(side=TOP)

c = Canvas(root)
c.pack(side=TOP, fill=BOTH, expand=1)

def minimax(values=[0.01.00.01.0]):
    for i in range(4):
        edit = bounds[i]
        try: values[ifloat(edit.get())
        except: pass
        edit.delete(0END)
        edit.insert(END, '%.2f'%values[i])
    return values

def plot():
    minx, maxx, miny, maxy = minimax()

    f = func.get()
    f = compile(f, f, 'eval')

    CX = c.winfo_width()
    CY = c.winfo_height()

    coords = []
    for i in range(0,CX,5):
        coords.append(i)
        x = minx + ((maxx-minx)*i)/CX
        y = eval(f, vars(math){'x':x})
        j = CY*(y-miny)/(maxy-miny)
        coords.append(j)

    c.delete(ALL)
    c.create_line(*coords)

butt.config(command=plot)

f = 'sin(x+ cos(x)'
func.insert(END, f)
minimax([0.010.0, -2.02.0])

root.mainloop()

           
       



    Related Scripts with Example Source Code in same category :

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON examples

 Navioo 2D
» Curve