Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lehrkraefte:blc:informatik:glf22:python:graphics [2022/11/17 09:31] Ivo Blöchliger [Simple Grafik mit graphics.py] |
lehrkraefte:blc:informatik:glf22:python:graphics [2022/11/25 08:09] (current) Ivo Blöchliger [Sinus/Cosinus] |
||
---|---|---|---|
Line 64: | Line 64: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | <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 ====== | ||
Es gibt einen Screencast: [[https:// | Es gibt einen Screencast: [[https:// | ||
+ | |||
+ | ===== Windows ===== | ||
Auf der Kommandozeile (git-bash) egal in welchem Ordner: | Auf der Kommandozeile (git-bash) egal in welchem Ordner: | ||
<code bash> | <code bash> | ||
pip install graphics.py | pip install graphics.py | ||
+ | </ | ||
+ | ===== Linux/Mac ===== | ||
+ | <code bash> | ||
+ | sudo pip3 install tk graphics.py | ||
</ | </ | ||
- | Unter Linux/Mac ist '' | + | ===== Alle Systeme ===== |
Gehen Sie in einen geeigneten Ordner (wo Ihre eigenen Dateien fürs Programmieren liegen) und öffnen Sie dort den Ordner mit VisualCode: | Gehen Sie in einen geeigneten Ordner (wo Ihre eigenen Dateien fürs Programmieren liegen) und öffnen Sie dort den Ordner mit VisualCode: | ||
Line 94: | Line 324: | ||
</ | </ | ||
- | Sollte dieser Code noch nicht funktionieren (Linux/ | ||
- | <code bash> | ||
- | pip3 install tk | ||
- | </ |