01 Jan 2024
Python und Sierpinsky - rekursiv und flott
Schnelles Sierpinsky-Beispiel in Python, Rekursiv via Turtle und Tkinter
Danke, Internet & Stackoverflow!
#!/usr/bin/python3.10
#
# Sierpinsky in Python mit Turtle und Farben
#
import turtle
Color = {"Purple":"#A46BFF","Aqua":"#56BCE8","Tree":"#5EFF7A","Summer":"#E8A83A"}
C = ["#A46BFF","#56BCE8","#5EFF7A","#E8A83A","#555555","#777777"]
def draw_sierpinski(length,depth):
if depth==0:
for i in range(0,3):
t.forward(length)
t.left(120)
else:
t.color(C[depth])
draw_sierpinski(length/2,depth-1)
t.color(C[depth])
t.forward(length/2)
draw_sierpinski(length/2,depth-1)
t.color(C[depth])
t.back(length/2)
t.left(60)
t.forward(length/2)
t.right(60)
draw_sierpinski(length/2,depth-1)
t.color(C[depth])
t.left(60)
t.back(length/2)
t.right(60)
window = turtle.Screen()
window.title("Sierpinsky")
window.setup(800, 800)
turtle.setworldcoordinates(-10, -10, 810, 810)
t = turtle.Turtle()
#draw_sierpinski(100,2)
#draw_sierpinski(200,3)
draw_sierpinski(500,5)
window.exitonclick(
Ausgabe