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/02 12:22]
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:
-====== RGB-Würfel (als ppm) und farbiges Sierpinski-Dreieck (als SVG) ======+~~NOTOC~~ 
 + 
 +====== Mit Python Bild-Dateien erzeugen: RGB-Würfel (als ppm) und farbiges Sierpinski-Dreieck (als SVG) ======
  
 Ziel heute: Die folgenden Grafiken mit Tigerjython erstellen! Ziel heute: Die folgenden Grafiken mit Tigerjython erstellen!
Line 7: Line 9:
 {{ :lehrkraefte:snr:informatik:rgb-color-cube.ppm.txt |ppm-Datei für RGB-Farbwürfel als .txt}} (Da ich hier im dokuwiki keine SVG-Dateien hochladen konnte.) {{ :lehrkraefte:snr:informatik:rgb-color-cube.ppm.txt |ppm-Datei für RGB-Farbwürfel als .txt}} (Da ich hier im dokuwiki keine SVG-Dateien hochladen konnte.)
  
-und hier als png.+und hier als png:
  
 {{:lehrkraefte:snr:informatik:rgb-color-cube.png?800|}} {{:lehrkraefte:snr:informatik:rgb-color-cube.png?800|}}
Line 17: Line 19:
 {{ :lehrkraefte:snr:informatik:sierpinski-fuer-dokuwiki.svg.txt | SVG-Datei für RGB-Farb-Sierpinski-Dreieck als .txt}} {{ :lehrkraefte:snr:informatik:sierpinski-fuer-dokuwiki.svg.txt | SVG-Datei für RGB-Farb-Sierpinski-Dreieck als .txt}}
  
-und hier als png...+und hier als png:
  
 {{:lehrkraefte:snr:informatik:sierpinski-fuer-dokuwiki.png?800|}} {{:lehrkraefte:snr:informatik:sierpinski-fuer-dokuwiki.png?800|}}
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 41: Line 43:
 # Code zu ergänzen: Male Farbwürfel Pixel für Pixel # Code zu ergänzen: Male Farbwürfel Pixel für Pixel
  
-ausgabe = open("RGB-color-cube.ppm", "w")+# auf Schulcomputer funktioniert: 
 +ausgabe = open("C:\\Users\\kleingeschreibenerVorname.kleingeschriebenerNachname\\RGB-farbwuerfel.ppm", "w"
 + 
 +# unter Linux (und vermutlich auch macOS) funktioniert z.B.  
 +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 54: Line 61:
 </code> </code>
  
-=== Code 2: Vorlage für RGB-Farbdreieck bzw. zuerst RGB-Farbrechteck ===+==== Code 2: Vorlage: Auf dem Weg zum rekursiv programmierten Sierpinski-Dreieck (Einführung in Rekursion) ==== 
 + 
 +<code python> 
 +from gpanel import * 
 + 
 +def dreieck_4(x1, y1, x2, y2, x3, y3): 
 +    fillTriangle(x1, y1, x2, y2, x3, y3) 
 + 
 +def dreieck_3(x1, y1, x2, y2, x3, y3): 
 +   
 +    print("Code zu ergänzen!"  
 +    # Code zu ergänzen 
 + 
 +def dreieck_2(x1, y1, x2, y2, x3, y3): 
 + 
 +    print("Code zu ergänzen!"  
 +    # Code zu ergänzen 
 + 
 +def dreieck_1(x1, y1, x2, y2, x3, y3): 
 + 
 +    print("Code zu ergänzen!"  
 +    # Code zu ergänzen 
 + 
 +makeGPanel(Size(255, 222)) 
 +window(0, 255, 0, 222) 
 + 
 +Px, Py = 0, 0 
 +Qx, Qy = 255, 0 
 +Rx, Ry = 128, 222 
 + 
 +dreieck_4(Px, Py, Qx, Qy, Rx, Ry) 
 + 
 +delay(2000) 
 +dispose() 
 +</code> 
 + 
 + 
 +==== Code 3: Vorlage für RGB-Farbdreieck bzw. zuerst RGB-Farbrechteck ====
  
 <code python> <code python>
Line 84: Line 128:
 </code> </code>
  
-=== Code 3: Sierpinski-Dreieck zum Rekursion lernen === +==== Einfache SVG-Beispieldatei ====
- +
-<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) +
-dispose() +
-</code> +
- +
-=== Einfache SVG-Beispieldatei ===+
  
 <code html> <code html>
Line 140: 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 160: 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 197: Line 191:
 Rx, Ry = 128, 222 Rx, Ry = 128, 222
  
-ausgabe = open("sierpinski.svg", "w"+# auf Schulcomputern mit Windows funktioniert: 
-# Wenn man auf Dateien in einem anderen Verzeichnis zugreifen will, +ausgabe = open("C:\\Users\\kleingeschreibenerVorname.kleingeschriebenerNachname\\sierpinski.svg", "w"
-muss man unter Windows den Pfad sinngemaess wie folgt angeben. + 
-# ausgabe = open("C:\\Users\\Vorname.Nachname\\Downloads\\ausgabe.txt", "w")+Unter Linux (und wohl auch macOS): 
 +# ausgabe = open("sierpinski.svg", "w")
  
 ausgabe.write("<svg height='222' width='255'>\n") ausgabe.write("<svg height='222' width='255'>\n")
Line 216: 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 241: 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 254: 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 303: 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 341: 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 371: 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.1646220150.txt.gz
  • Last modified: 2022/03/02 12:22
  • by Olaf Schnürer