[Edu-sig] Brainstorming about January
Gregor Lingl
glingl at aon.at
Mon Dec 13 00:13:02 CET 2004
Kirby Urner schrieb:
>OK, ...
>
>===
>Math & Programming: From Chaos to Python
>
>Explore topics in mathematics by writing and modifying programs in a
>contemporary computer language.
>
Attention: this is a somewhat technical question/contribution (in
contrast to the more philosophical ones concerning the design an
didactics of cs courses)
Hi Kirby and all the others!
In one of my classes I'm just doing similar things. Starting with the
very first program of John Zelle's mervelous book I arrived at a
program drawing a Feigenbaum diagram. With this - and I think with
all those chaos/fractal-programs which use pixel graphics - there
occurs the problem, that pixel graphics with Tkinter is *very* slow.
Although I have used a few tricks to make the program fast it still
needs approximately 50 seconds to draw the diagram (on my machine).
Moreover it show's that the slow operation is indeed the put-method of
PhotoImage (and the update method). My students of course searching the
net find Feigenbaum applets performing much faster. Being asked about
that my (somewhat poor) answer is, yes it's slow in Python but the
advantage is: *you* can write it in Python, whereas you can't write it
in Java. Not really satisfying.
As this is a problem concerning a large class of applications (at least
in the context of programming in highschool) I dream of having at hand
some easily usable class which defines a window for doing fast pixel
graphics.
As far as I know there are a *lot* of graphics-Toolkits for Python out
there. Which of them could be used for this task? What are your
experiences? Do you have significantly faster solutions?
Here is the code of my solution using Tkinter:
# Autor: Gregor Lingl
# Datum: 21. 10. 2004
# feigen.py : zeichnet ein Feigenbaum-Diagramm
from Tkinter import Tk, PhotoImage, Label
WIDTH = 700
HEIGHT = 500
NUM = 2*HEIGHT//3
def f(x):
return LAMBDA * x * (1-x)
root = Tk()
root.title("Feigenbaum Diagram")
pic = PhotoImage(width=WIDTH+1, height=HEIGHT)
lbl = Label(root, image=pic, bg='white')
lbl.pack()
for i in range(8):
for j in range(0,HEIGHT):
pic.put("red", (int(round(WIDTH/7.0)*i),j))
root.update()
for frac in range(WIDTH):
LAMBDA = 3.3 + frac*0.7/WIDTH
x = 0.5 # initial value for iteration
ys={}
for i in range(150):
x = f(x)
for i in range(NUM):
x = f(x)
ys[int(HEIGHT*x)]=True
for y in ys:
pic.put("black", (frac,y))
if frac%10==0:root.update()
root.mainloop()
>A goal of this class is to help you gain proficiency as a programmer, while
>delving into number theory, group theory, chaos and fractals,
>
the above program was intended to do just this ...
Another example is from my book "Python für Kids" showing a more regular
fractal (but suffering froom the same problem ...):
### Python für Kids - Kapitel 12 ###
# Autor: Gregor Lingl
# Datum: 13. 10. 2002
# simplefractal.py : zeichnet ein Sierpinsky-Dreieck
from Tkinter import Tk, PhotoImage, Label
from random import randrange, choice
def mittelpunkt(a,b):
(x1,y1), (x2,y2) = a, b
return ( int(round((x1+x2)/2.0)),
int(round((y1+y2)/2.0)) )
def sierpinsky():
global pixel
while running:
for i in range(20):
ecke = choice(ecken)
pixel = mittelpunkt(pixel,ecke)
bild.put('black',pixel)
root.update()
def start(event):
print "START"
global running
running = 1
sierpinsky()
def stop(event):
print "STOP"
global running
running = 0
if __name__ == "__main__":
root = Tk()
bild = PhotoImage(width=300, height=300)
lbl = Label(root, image=bild, bg='white')
lbl.pack()
root.bind('<Button-1>', start)
root.bind('<Button-3>', stop)
ecken = a, b, c = (20,265),(280,265),(150,40)
pixel = randrange(300), randrange(300)
for p in (a, b, c, pixel):
bild.put("black", p)
root.update()
root.mainloop()
Any feedback, any suggestions, propositions, solutions are welcome
Regards,
Gregor
P.S.: Some conversations on the list - e. g. the one following the
posting to which this is a reply - are really difficult to follow for
me, as I'm not using English as a native language. Sometimes there seems
to prevail some strange sort of humour or animosity. If that is funny
for you, that's o.k. but nevertheless I doubt if it's useful for our
concerns. (I'm well aware of the fact that it might be equally
difficult for you to understand what I try to say in my rather poor
English.)
More information about the Edu-sig
mailing list