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:17] Marcel Metzler [Callbacks] |
kurse:efcomputergrafik:kw36 [2019/09/04 15:14] (current) Marcel Metzler [Callbacks] |
||
---|---|---|---|
Line 183: | Line 183: | ||
</ | </ | ||
// | // | ||
+ | |||
+ | **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() | ||
+ | </ |