Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
kurse:efcomputergrafik:kw36 [2019/09/04 14:08] Marcel Metzler [Callbacks] |
kurse:efcomputergrafik:kw36 [2019/09/04 15:14] (current) Marcel Metzler [Callbacks] |
||
---|---|---|---|
Line 174: | Line 174: | ||
Bearbeite dazu das [[http:// | Bearbeite dazu das [[http:// | ||
- | Ein Callback ist somit eine Verzweigung des Programmes aufgrund eines äusseren Ereignisses. Diese äusseren Ereignisse sind von uns Usern initiiert, indem wir mit der Mause oder mit der Tastatur eine Eingabe vornehmen. | + | Ein Callback ist somit eine Verzweigung des Programmes aufgrund eines äusseren Ereignisses. Diese äusseren Ereignisse sind von uns Usern initiiert, indem wir mit der Mause oder mit der Tastatur eine Eingabe vornehmen. Aufgrund eines Ereignisses $E$ springt das Programm in ein dafür vorgesehenes Unterprogramm $UP$ führt dieses aus und kehrt wieder an die alte Stelle im Programm zurück um dort weiterzufahren. |
+ | Alle Ereignisse $E$, auf die wir reagieren wollen, müssen wir bei der Initialisierung des GPanel' | ||
+ | <code python> | ||
+ | makeGPanel(mousePressed = onMousePressed, | ||
+ | | ||
+ | </ | ||
+ | // | ||
+ | |||
+ | **Aufgabe 4** | ||
+ | - Studiere das nachfolgende Programm | ||
+ | - Was macht das Programm? Bist du dir nicht sicher, so lade es herunter und probiere es aus. | ||
+ | |||
+ | <code python Callback> | ||
+ | from gpanel import * | ||
+ | |||
+ | def onMousePressed(x, | ||
+ | clear() | ||
+ | move(x,y) | ||
+ | text(" | ||
+ | |||
+ | def onMouseReleased(x, | ||
+ | clear() | ||
+ | move(x,y) | ||
+ | text(" | ||
+ | |||
+ | def onMouseDragged(x, | ||
+ | clear() | ||
+ | move(x,y) | ||
+ | text(" | ||
+ | |||
+ | makeGPanel(mousePressed = onMousePressed, | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | **Aufgabe 5** | ||
+ | - Ergänze dein Mandelbrot Programm so, dass du mit der Mause einen Zoombereich festlegen kannst. In einer ersten Version musst du den Zoombereich nicht mit einem Rechteck oder Quadrat anzeigen. | ||
+ | - Ergänze deine erste Version Mandelbrot mit Zoom so, dass der Zoobereich mit einem farbigen Rechteck oder Quadrat angezeigt wird. Benutze dabei die Idee aus Tigerjython bei den Gummibandlinien. | ||
+ | |||
+ | Hier eine mögliche Lösung. | ||
+ | <code python Mandelbrot-Zoom> | ||
+ | from gpanel import * | ||
+ | |||
+ | def onMousePressed(x, | ||
+ | global xmin, ymin | ||
+ | storeGraphics() | ||
+ | xmin=x | ||
+ | ymin=y | ||
+ | |||
+ | def onMouseReleased(x, | ||
+ | global xmax,ymax | ||
+ | global neuesBild | ||
+ | xmax=x | ||
+ | ymax=ymin+(xmax-xmin) | ||
+ | neuesBild=0 | ||
+ | |||
+ | def onMouseDragged(x, | ||
+ | global xmin,ymin | ||
+ | delta=x-xmin | ||
+ | recallGraphics() | ||
+ | move(xmin, | ||
+ | setColor(" | ||
+ | line(xmin, ymin, xmin+delta, ymin) | ||
+ | line(xmin+delta, | ||
+ | line(xmin+delta, | ||
+ | line(xmin, ymin+delta, xmin, ymin) | ||
+ | | ||
+ | def getIterationColor(it): | ||
+ | color = makeColor((it**2) % 256, | ||
+ | (3*it) % 256, | ||
+ | (5*it) % 256) | ||
+ | return color | ||
+ | | ||
+ | def Mandelbrot(c): | ||
+ | z=0 | ||
+ | it=0 | ||
+ | while it< | ||
+ | z = z**2 + c | ||
+ | it=it+1 | ||
+ | if abs(z) > R: | ||
+ | return it | ||
+ | return maxIterations | ||
+ | |||
+ | def drawP(): | ||
+ | global xmin, xmax, xstep, xSize | ||
+ | global ymin, ymax, ystep, ySize | ||
+ | xstep=(xmax-xmin)/ | ||
+ | ystep=(ymax-ymin)/ | ||
+ | makeGPanel(Size(xSize, | ||
+ | mousePressed = onMousePressed, | ||
+ | mouseReleased = onMouseReleased, | ||
+ | mouseDragged = onMouseDragged) | ||
+ | window(xmin, | ||
+ | y = ymin | ||
+ | while y <= ymax: | ||
+ | x = xmin | ||
+ | while x <= xmax: | ||
+ | c = complex(x, | ||
+ | itCount = Mandelbrot(c) | ||
+ | if itCount == maxIterations: | ||
+ | setColor(" | ||
+ | else: | ||
+ | | ||
+ | point(c) | ||
+ | x += xstep | ||
+ | repaint() | ||
+ | y += ystep | ||
+ | # | ||
+ | # Hauptprogramm | ||
+ | # | ||
+ | R = 2 | ||
+ | maxIterations = 100 | ||
+ | xmin = -2 | ||
+ | xmax = 1 | ||
+ | ymin = -1.5 | ||
+ | ymax = 1.5 | ||
+ | xSize=600 | ||
+ | ySize=600 | ||
+ | neuesBild=0 | ||
+ | key = 0 | ||
+ | while key!=27: | ||
+ | if neuesBild==0: | ||
+ | drawP(); | ||
+ | neuesBild=1 | ||
+ | key = getKeyCode() | ||
+ | </ |