lehrkraefte:snr:informatik:rgbwuerfel-und-sierpinski-farbig

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lehrkraefte:snr:informatik:rgbwuerfel-und-sierpinski-farbig [2022/03/07 20:16]
Olaf Schnürer [Vorlagen für die Lektion]
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:
 {{:lehrkraefte:snr:informatik:sierpinski-farbdreieck-ausgefuellt-fuer-dokuwiki.png?800|}} {{:lehrkraefte:snr:informatik:sierpinski-farbdreieck-ausgefuellt-fuer-dokuwiki.png?800|}}
  
-===== 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 44: Line 46:
 ausgabe = open("C:\\Users\\kleingeschreibenerVorname.kleingeschriebenerNachname\\RGB-farbwuerfel.ppm", "w") ausgabe = open("C:\\Users\\kleingeschreibenerVorname.kleingeschriebenerNachname\\RGB-farbwuerfel.ppm", "w")
  
-# unter Linux funktioniert z.B. +# unter Linux (und vermutlich auch macOS) funktioniert z.B. 
 # ausgabe = open("RGB-color-cube.ppm", "w") # ausgabe = open("RGB-color-cube.ppm", "w")
  
Line 59: Line 61:
 </code> </code>
  
-=== Code 2: Sierpinski-Dreieck zum Rekursion lernen ===+==== Code 2: Vorlage: Auf dem Weg zum rekursiv programmierten Sierpinski-Dreieck (Einführung in Rekursion) ====
  
 <code python> <code python>
 from gpanel import * from gpanel import *
  
-# Bitte erhöhenaber nicht viel grösser als 12 gehen. +def dreieck_4(x1y1, x2, y2, x3, y3): 
-MAX_TIEFE = 2 +    fillTriangle(x1, y1, x2, y2, x3, y3)
-pausenZeit = 100+
  
-def sierpinskiDreieck(x1, y1, x2, y2, x3, y3, tiefe): +def dreieck_3(x1, y1, x2, y2, x3, y3): 
-    if tiefe == MAX_TIEFE:     +   
-        fillTriangle(x1, y1, x2, y2, x3, y3) +    print("Code zu ergänzen!"  
-         +    # Code zu ergänzen 
-        # Damit auch etwas gezeichnet wird, wenn die Dreiecke  + 
-        # "zu kleinsind. +def dreieck_2(x1, y1, x2, y2, x3, y3): 
-        if MAX_TIEFE > 7: + 
-            point(int(x1), int(y1)) +    print("Code zu ergänzen!"  
-             +    # Code zu ergänzen 
-        delay(pausenZeit+ 
-    else: +def dreieck_1(x1, y1, x2, y2, x3y3): 
-        a12 = (x1 + x2) / 2 + 
-        b12 = (y1 + y2) / 2 +    print("Code zu ergänzen!"  
-        a13 = (x1 + x3) / 2 +    # Code zu ergänzen
-        b13 = (y1 + y3) / 2 +
-        a23 = (x2 + x3) / 2 +
-        b23 = (y2 + y3) / 2 +
-     +
-        sierpinskiDreieck(x1, y1, a12, b12, a13, b13, tiefe + 1) +
-        sierpinskiDreieck(a12, b12, x2, y2, a23b23, tiefe + 1+
-        sierpinskiDreieck(a13, b13, a23, b23, x3, y3, tiefe + 1)+
  
 makeGPanel(Size(255, 222)) makeGPanel(Size(255, 222))
Line 97: Line 91:
 Rx, Ry = 128, 222 Rx, Ry = 128, 222
  
-sierpinskiDreieck(Px, Py, Qx, Qy, Rx, Ry, 0)+dreieck_4(Px, Py, Qx, Qy, Rx, Ry)
  
 delay(2000) delay(2000)
Line 103: Line 97:
 </code> </code>
  
-=== Code 3: Vorlage für RGB-Farbdreieck bzw. zuerst RGB-Farbrechteck ===+ 
 +==== Code 3: Vorlage für RGB-Farbdreieck bzw. zuerst RGB-Farbrechteck ====
  
 <code python> <code python>
Line 133: Line 128:
 </code> </code>
  
-=== Einfache SVG-Beispieldatei ===+==== Einfache SVG-Beispieldatei ====
  
 <code html> <code html>
Line 145: 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 165: 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 = "0" + s 
-    return s 
                          
 def sierpinskiDreieck(x1, y1, x2, y2, x3, y3, tiefe): def sierpinskiDreieck(x1, y1, x2, y2, x3, y3, tiefe):
Line 202: Line 191:
 Rx, Ry = 128, 222 Rx, Ry = 128, 222
  
-# auf Schulcomputer funktioniert:+# auf Schulcomputern mit Windows funktioniert:
 ausgabe = open("C:\\Users\\kleingeschreibenerVorname.kleingeschriebenerNachname\\sierpinski.svg", "w") ausgabe = open("C:\\Users\\kleingeschreibenerVorname.kleingeschriebenerNachname\\sierpinski.svg", "w")
  
 +# Unter Linux (und wohl auch macOS):
 # ausgabe = open("sierpinski.svg", "w") # ausgabe = open("sierpinski.svg", "w")
-# Wenn man auf Dateien in einem anderen Verzeichnis zugreifen will, 
-# muss man unter Windows den Pfad sinngemaess wie folgt angeben. 
-# ausgabe = open("C:\\Users\\Vorname.Nachname\\Downloads\\ausgabe.txt", "w") 
  
 ausgabe.write("<svg height='222' width='255'>\n") ausgabe.write("<svg height='222' width='255'>\n")
Line 224: Line 211:
 </code> </code>
  
-===== Einige Lösungsvorschläge =====+====== Einige Lösungsvorschläge ======
 <hidden Farbwürfel, Ausgabe auch als ppm-Datei> <hidden Farbwürfel, Ausgabe auch als ppm-Datei>
 <code python> <code python>
Line 249: Line 236:
 print("Schreibe ppm-Datei.") print("Schreibe ppm-Datei.")
  
-ausgabe = open("RGB-color-cube.ppm", "w")+ausgabe = open("C:\\Users\\kleingeschreibenerVorname.kleingeschriebenerNachname\\RGB-farbwuerfel.ppm", "w"
 +ausgabe = open("RGB-color-cube.ppm", "w")
 ausgabe.write("P3\n") ausgabe.write("P3\n")
 ausgabe.write("383 383\n") ausgabe.write("383 383\n")
Line 262: Line 250:
 print("Fertig!") print("Fertig!")
 ausgabe.close() ausgabe.close()
 +
 +delay(2000)
 +dispose()
 +</code>
 +</hidden>
 +
 +<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, y1, x2, y2, x3, y3, tiefe):
 +    if tiefe == MAX_TIEFE:    
 +        fillTriangle(x1, y1, x2, y2, x3, y3)
 +        
 +        # Damit auch etwas gezeichnet wird, wenn die Dreiecke 
 +        # "zu klein" sind.
 +        if MAX_TIEFE > 7:
 +            point(int(x1), int(y1))
 +            
 +        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, y1, a12, b12, a13, b13, tiefe + 1)
 +        sierpinskiDreieck(a12, b12, x2, y2, a23, b23, tiefe + 1)
 +        sierpinskiDreieck(a13, b13, a23, b23, x3, y3, tiefe + 1)
 +
 +makeGPanel(Size(255, 222))
 +window(0, 255, 0, 222)
 +
 +Px, Py = 0, 0
 +Qx, Qy = 255, 0
 +Rx, Ry = 128, 222
 +
 +sierpinskiDreieck(Px, Py, Qx, Qy, Rx, Ry, 0)
  
 delay(2000) delay(2000)
Line 311: Line 343:
 </hidden> </hidden>
  
-<hidden Farbiges Sierpinski-Dreieck, Ausgabe auch als SVG-Datei>+<hidden Farbiges Sierpinski-Dreieck, Ausgabe auch als SVG-Datei; ACHTUNG, DAS GEHT EINFACHER MIT FORMATIERTER AUSGABE: %02x für hexadezimal mit führenden Nullen und mindestens zweistellig; STATT FUNKTION hexadezimalZweistellig>
 <code python> <code python>
 from gpanel import * from gpanel import *
Line 349: Line 381:
             point(int(x1), int(y1))             point(int(x1), int(y1))
                  
-        ausgabe.write("<polyline points='"\+        ausgabe.write("<polygon points='"\
             + 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) + " "
-            + str(x1) + ", " + str(222-y1) + " ' style='fill:#"\+            + " ' style='fill:#"\
             + hexadezimalZweistellig(r)             + hexadezimalZweistellig(r)
             + hexadezimalZweistellig(g)             + hexadezimalZweistellig(g)
Line 379: Line 411:
 Rx, Ry = 128, 222 Rx, Ry = 128, 222
  
-ausgabe = open("sierpinski.svg", "w")+ausgabe = open("C:\\Users\\kleingeschreibenerVorname.kleingeschriebenerNachname\\sierpinski.svg", "w"
 + 
 +ausgabe = open("sierpinski.svg", "w")
 # Wenn man auf Dateien in einem anderen Verzeichnis zugreifen will, # Wenn man auf Dateien in einem anderen Verzeichnis zugreifen will,
 # muss man unter Windows den Pfad sinngemaess wie folgt angeben. # muss man unter Windows den Pfad sinngemaess wie folgt angeben.
  • lehrkraefte/snr/informatik/rgbwuerfel-und-sierpinski-farbig.1646680613.txt.gz
  • Last modified: 2022/03/07 20:16
  • by Olaf Schnürer