Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
lehrkraefte:blc:informatik:glf22:python:graphics [2022/11/16 11:42] Ivo Blöchliger created |
lehrkraefte:blc:informatik:glf22:python:graphics [2022/11/25 08:09] (current) Ivo Blöchliger [Sinus/Cosinus] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Simple Grafik mit graphics.py ====== | ====== Simple Grafik mit graphics.py ====== | ||
- | Installieren Sie zuerst das nötige Paket, siehe unten. | + | Installieren Sie zuerst das nötige Paket, siehe [[#Setup|unten]]. |
===== Dokumentation ===== | ===== Dokumentation ===== | ||
Line 6: | Line 6: | ||
+ | ====== Aufgaben ====== | ||
+ | ===== Wiederholungen ===== | ||
+ | Testen und Verstehen Sie folgenden Code: | ||
+ | <code python> | ||
+ | from graphics import * | ||
+ | |||
+ | win = GraphWin(" | ||
+ | |||
+ | for i in range(100, | ||
+ | r = Rectangle(Point(i, | ||
+ | r.setFill(" | ||
+ | r.draw(win) | ||
+ | |||
+ | |||
+ | win.getMouse() # Pause to view result | ||
+ | win.close() | ||
+ | </ | ||
+ | |||
+ | <WRAP todo> | ||
+ | Verändern Sie das Programm so, dass folgendes Bild entsteht: {{: | ||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from graphics import * | ||
+ | |||
+ | win = GraphWin(" | ||
+ | |||
+ | for i in range(100, | ||
+ | r = Rectangle(Point(200, | ||
+ | r.setFill(" | ||
+ | r.draw(win) | ||
+ | |||
+ | |||
+ | win.getMouse() # Pause to view result | ||
+ | win.close() | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP todo> | ||
+ | Erweitern Sie das Programm so, dass folgendes Bild entsteht: {{: | ||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from graphics import * | ||
+ | |||
+ | win = GraphWin(" | ||
+ | |||
+ | for i in range(100, | ||
+ | for j in range(100, | ||
+ | r = Rectangle(Point(j, | ||
+ | r.setFill(" | ||
+ | r.draw(win) | ||
+ | |||
+ | |||
+ | win.getMouse() # Pause to view result | ||
+ | win.close() | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <hidden Zusatzaufgabe> | ||
+ | Erzeugen Sie folgendes Bild: {{: | ||
+ | |||
+ | Hinweis: Testen Sie, ob die Summe der Koordinaten durch eine bestimme Zahl (hier im Beispiel 42) teilbar ist: | ||
+ | <code python> | ||
+ | if (i+j)%42==0: | ||
+ | # Tu was | ||
+ | else: | ||
+ | # Tu was anderes | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Cosinus/ | ||
+ | |||
+ | Testen und Verstehen Sie folgendes Programm: | ||
+ | |||
+ | <code python> | ||
+ | from graphics import * | ||
+ | from math import cos,sin,pi | ||
+ | |||
+ | win = GraphWin(" | ||
+ | win.setCoords(-2, | ||
+ | |||
+ | |||
+ | for i in range(5): | ||
+ | grad = 360/5*i | ||
+ | bogenmass = grad/180*pi | ||
+ | x = cos(bogenmass) | ||
+ | y = sin(bogenmass) | ||
+ | c = Circle(Point(x, | ||
+ | c.setFill(" | ||
+ | c.draw(win) | ||
+ | |||
+ | |||
+ | Circle(Point(0, | ||
+ | |||
+ | win.getMouse() # Pause to view result | ||
+ | win.close() | ||
+ | </ | ||
+ | |||
+ | <WRAP todo> | ||
+ | Ändern Sie das Programm so ab, dass 10 Punkte auf dem Einheitskreis eingezeichnet werden. | ||
+ | </ | ||
+ | |||
+ | ==== Funktion für $P_\alpha$ ==== | ||
+ | Damit wir Punkte auf dem Einheitskreis mit Linien verbinden können, ist es praktisch Punkte auf dem Einheitskreis einfach berechnen zu können. Dazu definieren wir eine Funktion '' | ||
+ | |||
+ | <code python> | ||
+ | def palpha(grad): | ||
+ | bogenmass = grad/180*pi | ||
+ | x = cos(bogenmass) | ||
+ | y = sin(bogenmass) | ||
+ | return Point(x, | ||
+ | </ | ||
+ | |||
+ | <WRAP todo> | ||
+ | Bauen Sie die Funktion '' | ||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from graphics import * | ||
+ | from math import cos,sin,pi | ||
+ | |||
+ | win = GraphWin(" | ||
+ | win.setCoords(-2, | ||
+ | |||
+ | def palpha(grad): | ||
+ | bogenmass = grad/180*pi | ||
+ | x = cos(bogenmass) | ||
+ | y = sin(bogenmass) | ||
+ | return Point(x, | ||
+ | |||
+ | |||
+ | for i in range(5): | ||
+ | c = Circle(palpha(360/ | ||
+ | c.setFill(" | ||
+ | c.draw(win) | ||
+ | |||
+ | |||
+ | Circle(Point(0, | ||
+ | |||
+ | win.getMouse() # Pause to view result | ||
+ | win.close() | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ==== Linien ==== | ||
+ | Testen und verstehen Sie folgendes Programm: | ||
+ | <code python> | ||
+ | from graphics import * | ||
+ | from math import cos,sin,pi | ||
+ | |||
+ | win = GraphWin(" | ||
+ | win.setCoords(-2, | ||
+ | |||
+ | def palpha(grad): | ||
+ | bogenmass = grad/180*pi | ||
+ | x = cos(bogenmass) | ||
+ | y = sin(bogenmass) | ||
+ | return Point(x, | ||
+ | |||
+ | Line(palpha(0), | ||
+ | Line(palpha(120), | ||
+ | Line(palpha(240), | ||
+ | |||
+ | |||
+ | win.getMouse() # Pause to view result | ||
+ | win.close() | ||
+ | </ | ||
+ | |||
+ | <WRAP todo> | ||
+ | Ändern Sie das Programm so, dass der Befehl '' | ||
+ | <hidden Lösungsvorschlag> | ||
+ | Hier sind nur die geänderten Zeilen, der Rest vom Programm bleibt sich gleich. | ||
+ | <code python> | ||
+ | for i in range(0, | ||
+ | Line(palpha(i), | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP todo> | ||
+ | Zeichnen Sie ein Fünfeck. | ||
+ | </ | ||
+ | |||
+ | <WRAP todo> | ||
+ | Definieren Sie am Anfang des Programms eine Variable '' | ||
+ | Testen Sie Ihr Programm, indem Sie für '' | ||
+ | <hidden Lösungsvorschlag> | ||
+ | Nur die relevanten Zeilen sind hier aufgeführt: | ||
+ | <code python> | ||
+ | n = 7 | ||
+ | schritt = 360/n | ||
+ | for i in range(n): | ||
+ | Line(palpha(i*schritt), | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP todo> | ||
+ | Zeichnen Sie einen 5-Zack Stern: {{: | ||
+ | </ | ||
+ | |||
+ | <WRAP todo> | ||
+ | Verallgemeinern Sie Ihr Programm so, dass zwei Variablen '' | ||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from graphics import * | ||
+ | from math import cos,sin,pi | ||
+ | |||
+ | win = GraphWin(" | ||
+ | win.setCoords(-2, | ||
+ | |||
+ | def palpha(grad): | ||
+ | bogenmass = grad/180*pi | ||
+ | x = cos(bogenmass) | ||
+ | y = sin(bogenmass) | ||
+ | return Point(x, | ||
+ | |||
+ | n = 11 | ||
+ | m = 4 | ||
+ | schritt = 360/n | ||
+ | for i in range(n): | ||
+ | Line(palpha(i*schritt), | ||
+ | |||
+ | |||
+ | win.getMouse() # Pause to view result | ||
+ | win.close() | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Weitere Aufgaben ===== | ||
+ | Programmieren Sie ein Programm, das folgende Bilder erzeugt: | ||
+ | {{: | ||
+ | {{: | ||
+ | |||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from graphics import * | ||
+ | from math import cos,sin,pi | ||
+ | |||
+ | win = GraphWin(" | ||
+ | |||
+ | |||
+ | def huellkurve(): | ||
+ | win.setCoords(-0.1, | ||
+ | n=50 | ||
+ | for i in range(n+1): | ||
+ | x = i/n | ||
+ | Line(Point(x, | ||
+ | |||
+ | def spirale(): | ||
+ | win.setCoords(-1.1, | ||
+ | umdrehungen = 5 | ||
+ | schritte = 400; | ||
+ | for i in range(schritte): | ||
+ | winkel = i/ | ||
+ | winkel2 = (i+1)/ | ||
+ | radius = i/schritte; | ||
+ | Line(Point(radius*cos(winkel), | ||
+ | |||
+ | |||
+ | |||
+ | def clickAndWait(titel=" | ||
+ | global win # Damit kann die Variable win auch verändert werden | ||
+ | win.getMouse() # Pause to view result | ||
+ | win.close() | ||
+ | win = GraphWin(titel, | ||
+ | |||
+ | |||
+ | huellkurve() | ||
+ | clickAndWait(" | ||
+ | spirale() | ||
+ | |||
+ | win.getMouse() # Pause to view result | ||
+ | |||
+ | win.close() | ||
+ | </ | ||
+ | </ | ||
====== Setup ====== | ====== Setup ====== | ||
- | Auf der Komandozeile | + | Es gibt einen Screencast: [[https:// |
+ | |||
+ | ===== Windows ===== | ||
+ | |||
+ | Auf der Kommandozeile | ||
<code bash> | <code bash> | ||
pip install graphics.py | pip install graphics.py | ||
+ | </ | ||
+ | ===== Linux/Mac ===== | ||
+ | <code bash> | ||
+ | sudo pip3 install tk graphics.py | ||
+ | </ | ||
+ | |||
+ | ===== Alle Systeme ===== | ||
+ | |||
+ | |||
+ | Gehen Sie in einen geeigneten Ordner (wo Ihre eigenen Dateien fürs Programmieren liegen) und öffnen Sie dort den Ordner mit VisualCode: | ||
+ | <code bash> | ||
+ | code . | ||
+ | </ | ||
+ | wobei der Punkt für das aktuelle Verzeichnis steht. Alternativ kann natürlich Code auch über das Startmenu gestartet werden und via File-> | ||
+ | |||
+ | Wird ein ganzer Ordner geöffnet, können mehrere Dateien bearbeitet werden und die Programme komfortabel gestartet werden. | ||
+ | |||
+ | Beispiel-Programm zum Testen: | ||
+ | <code python> | ||
+ | from graphics import * | ||
+ | |||
+ | win = GraphWin(" | ||
+ | text = Text(Point(100, | ||
+ | text.setSize(36) | ||
+ | text.draw(win) | ||
+ | win.getMouse() # Pause to view result | ||
+ | win.close() | ||
</ | </ | ||