Both!
See attachment
Regards,
Gregor
Shi Mu schrieb:
any code to draw parabola or curve?
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor
--
Gregor Lingl
Reisnerstrasse 3/19
A-1030 Wien
Telefon: +43 1 713 33 98
Mobil: +43 664 140 35 27
Website: python4kids.net
# -*- coding: cp1252 -*-
# Minimalversion eines scalierenden Funktionsplotters --- V.4c
# Mit Realisierung Erich Neuwirth'scher Anregungen
# und einem kleinem Plus an OOP
# und einem Minus an FP
# Autor: Gregor Lingl, 9. 11. 2005
#
# Inbetriebnahme:
# Rechter Mausklick, Edit with IDLE
# Editor-Fenster aktivieren, F5 drücken
# FUNTKIONEN und KURVEN
import Tkinter, math
MARGIN = 20
## helper-function
def minmax(tuplist, index):
values = [tup[index] for tup in tuplist]
return min(values), max(values)
class Graph:
def __init__(self, rmin, rmax, steps, f, g=None):
d = 1.0*(rmax-rmin)/steps
rrange = [rmin+i*d for i in range(steps+1)]
if g is None:
self.plist = [(x, f(x)) for x in rrange]
else:
self.plist = [(f(t), g(t)) for t in rrange]
self.x_range = minmax(self.plist,0)
self.y_range = minmax(self.plist,1)
class PlotCanvas(Tkinter.Canvas):
def setCooSys(self, (xmin, xmax), (ymin, ymax)):
self.ex = (float(self["width"])-2*MARGIN)/(xmax-xmin)
self.ey = (float(self["height"])-2*MARGIN)/(ymin-ymax)
self.ox = MARGIN-xmin*self.ex
self.oy = MARGIN-ymax*self.ey
def screencoos(self,(x, y)):
return self.ox + x * self.ex, self.oy + y * self.ey
def plot(self, rmin, rmax, f, g=None, steps=50):
graph = Graph( rmin, rmax, steps, f, g)
self.setCooSys(graph.x_range, graph.y_range)
self.create_line([self.screencoos(p) for p in graph.plist])
def clear(self):
for item in self.find_all(): self.delete(item)
cv = PlotCanvas(width=240,height=240,bg="white")
cv.pack()
# Beispiel 1
cv.plot(-3.5, 3.5, math.sin)
cv.plot(-2, 2, lambda x: x**3-2*x)
raw_input("Press ENTER to continue!")
# Beispiel 2
cv.clear()
cv.plot(0, 2*math.pi, math.sin, math.cos)
raw_input("Press ENTER to continue!")
# Beispiel 3
cv.clear()
cv.plot(0, 2*math.pi, lambda t:math.sin(2*t), lambda t:math.cos(3*t), steps=100)
raw_input("Press ENTER to finish!")
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor