Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lehrkraefte:snr:informatik:rgbwuerfel-und-sierpinski-farbig [2022/03/09 10:42] Olaf Schnürer |
lehrkraefte:snr:informatik:rgbwuerfel-und-sierpinski-farbig [2022/03/27 20:44] (current) Olaf Schnürer [Einige Lösungsvorschläge] |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~NOTOC~~ | ||
+ | |||
====== Mit Python Bild-Dateien erzeugen: RGB-Würfel (als ppm) und farbiges Sierpinski-Dreieck (als SVG) ====== | ====== Mit Python Bild-Dateien erzeugen: RGB-Würfel (als ppm) und farbiges Sierpinski-Dreieck (als SVG) ====== | ||
Line 29: | Line 31: | ||
{{: | {{: | ||
- | ===== Vorlagen für die Lektion ===== | + | ====== Vorlagen für die Lektion |
- | === Code 1: Vorlage für RGB-Farbwürfel === | + | ==== Code 1: Vorlage für RGB-Farbwürfel |
<code python> | <code python> | ||
Line 59: | Line 61: | ||
</ | </ | ||
- | === Code 2: Auf dem Weg zum rekursiv programmierten Sierpinski-Dreieck (Einführung in Rekursion) === | + | ==== Code 2: Vorlage: Auf dem Weg zum rekursiv programmierten Sierpinski-Dreieck (Einführung in Rekursion) |
<code python> | <code python> | ||
Line 95: | Line 97: | ||
</ | </ | ||
- | === Code 2: Sierpinski-Dreieck zum Rekursion lernen === | ||
- | <code python> | + | ==== Code 3: Vorlage für RGB-Farbdreieck bzw. zuerst RGB-Farbrechteck |
- | from gpanel import * | + | |
- | + | ||
- | # Bitte erhöhen, aber nicht viel grösser als 12 gehen. | + | |
- | MAX_TIEFE | + | |
- | pausenZeit = 100 | + | |
- | + | ||
- | def sierpinskiDreieck(x1, | + | |
- | if tiefe == MAX_TIEFE: | + | |
- | fillTriangle(x1, | + | |
- | + | ||
- | # Damit auch etwas gezeichnet wird, wenn die Dreiecke | + | |
- | # "zu klein" sind. | + | |
- | if MAX_TIEFE > 7: | + | |
- | point(int(x1), | + | |
- | + | ||
- | delay(pausenZeit) | + | |
- | else: | + | |
- | a12 = (x1 + x2) / 2 | + | |
- | b12 = (y1 + y2) / 2 | + | |
- | a13 = (x1 + x3) / 2 | + | |
- | b13 = (y1 + y3) / 2 | + | |
- | a23 = (x2 + x3) / 2 | + | |
- | b23 = (y2 + y3) / 2 | + | |
- | + | ||
- | sierpinskiDreieck(x1, | + | |
- | sierpinskiDreieck(a12, | + | |
- | sierpinskiDreieck(a13, | + | |
- | + | ||
- | makeGPanel(Size(255, | + | |
- | window(0, 255, 0, 222) | + | |
- | + | ||
- | Px, Py = 0, 0 | + | |
- | Qx, Qy = 255, 0 | + | |
- | Rx, Ry = 128, 222 | + | |
- | + | ||
- | sierpinskiDreieck(Px, | + | |
- | + | ||
- | delay(2000) | + | |
- | dispose() | + | |
- | </ | + | |
- | + | ||
- | === Code 3: Vorlage für RGB-Farbdreieck bzw. zuerst RGB-Farbrechteck === | + | |
<code python> | <code python> | ||
Line 169: | Line 128: | ||
</ | </ | ||
- | === Einfache SVG-Beispieldatei === | + | ==== Einfache SVG-Beispieldatei |
<code html> | <code html> | ||
Line 181: | Line 140: | ||
- | === Code 4: Vorlage für farbiges Sierpinski-Dreieck samt SVG-Ausgabe === | + | ==== Code 4: Vorlage für farbiges Sierpinski-Dreieck samt SVG-Ausgabe |
<code python> | <code python> | ||
from gpanel import * | from gpanel import * | ||
- | MAX_TIEFE = 6 | + | MAX_TIEFE = 3 |
# Tiefe 12 schafft Inkscape noch mit Mühe, ab 13 weigert es sich | # Tiefe 12 schafft Inkscape noch mit Mühe, ab 13 weigert es sich | ||
# Tiefe 8 für dokuwiki svg | # Tiefe 8 für dokuwiki svg | ||
Line 201: | Line 160: | ||
| | ||
return int(rot), int(blau), int(gruen) | return int(rot), int(blau), int(gruen) | ||
- | |||
- | def hexadezimalZweistellig(zahl): | ||
- | s = hex(zahl)[2: | ||
- | if len(s) < 2: | ||
- | s = " | ||
- | return s | ||
| | ||
def sierpinskiDreieck(x1, | def sierpinskiDreieck(x1, | ||
Line 238: | Line 191: | ||
Rx, Ry = 128, 222 | Rx, Ry = 128, 222 | ||
- | # auf Schulcomputer | + | # auf Schulcomputern mit Windows |
ausgabe = open(" | ausgabe = open(" | ||
# Unter Linux (und wohl auch macOS): | # Unter Linux (und wohl auch macOS): | ||
# ausgabe = open(" | # ausgabe = open(" | ||
- | # Wenn man auf Dateien in einem anderen Verzeichnis zugreifen will, | ||
- | # muss man unter Windows den Pfad sinngemaess wie folgt angeben. | ||
- | # ausgabe = open(" | ||
ausgabe.write("< | ausgabe.write("< | ||
Line 261: | Line 211: | ||
</ | </ | ||
- | ===== Einige Lösungsvorschläge ===== | + | ====== Einige Lösungsvorschläge |
<hidden Farbwürfel, | <hidden Farbwürfel, | ||
<code python> | <code python> | ||
Line 300: | Line 250: | ||
print(" | print(" | ||
ausgabe.close() | ausgabe.close() | ||
+ | |||
+ | delay(2000) | ||
+ | dispose() | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <hidden Sierpinski-Dreieck rekursiv> | ||
+ | <code python> | ||
+ | from gpanel import * | ||
+ | |||
+ | # Bitte erhöhen, aber nicht viel grösser als 12 gehen. | ||
+ | MAX_TIEFE = 2 | ||
+ | pausenZeit = 100 | ||
+ | |||
+ | def sierpinskiDreieck(x1, | ||
+ | if tiefe == MAX_TIEFE: | ||
+ | fillTriangle(x1, | ||
+ | | ||
+ | # Damit auch etwas gezeichnet wird, wenn die Dreiecke | ||
+ | # "zu klein" sind. | ||
+ | if MAX_TIEFE > 7: | ||
+ | point(int(x1), | ||
+ | | ||
+ | delay(pausenZeit) | ||
+ | else: | ||
+ | a12 = (x1 + x2) / 2 | ||
+ | b12 = (y1 + y2) / 2 | ||
+ | a13 = (x1 + x3) / 2 | ||
+ | b13 = (y1 + y3) / 2 | ||
+ | a23 = (x2 + x3) / 2 | ||
+ | b23 = (y2 + y3) / 2 | ||
+ | | ||
+ | sierpinskiDreieck(x1, | ||
+ | sierpinskiDreieck(a12, | ||
+ | sierpinskiDreieck(a13, | ||
+ | |||
+ | makeGPanel(Size(255, | ||
+ | window(0, 255, 0, 222) | ||
+ | |||
+ | Px, Py = 0, 0 | ||
+ | Qx, Qy = 255, 0 | ||
+ | Rx, Ry = 128, 222 | ||
+ | |||
+ | sierpinskiDreieck(Px, | ||
delay(2000) | delay(2000) | ||
Line 349: | Line 343: | ||
</ | </ | ||
- | <hidden Farbiges Sierpinski-Dreieck, | + | <hidden Farbiges Sierpinski-Dreieck, |
<code python> | <code python> | ||
from gpanel import * | from gpanel import * | ||
Line 387: | Line 381: | ||
point(int(x1), | point(int(x1), | ||
| | ||
- | ausgabe.write("< | + | ausgabe.write("< |
+ str(x1) + ", " + str(222-y1) + " " | + str(x1) + ", " + str(222-y1) + " " | ||
+ str(x2) + ", " + str(222-y2) + " " | + str(x2) + ", " + str(222-y2) + " " | ||
+ str(x3) + ", " + str(222-y3) + " " | + str(x3) + ", " + str(222-y3) + " " | ||
- | | + | + " ' style=' |
+ hexadezimalZweistellig(r) | + hexadezimalZweistellig(r) | ||
+ hexadezimalZweistellig(g) | + hexadezimalZweistellig(g) |