Monday, November 7, 2022

Python Turtle Graphics Drawing - Sierpiński Triangle

 Python Turtle Graphics Drawing - Sierpiński Triangle


Youtube





Source code:
import turtle as t
import colorsys as cs
from math import sqrt

t.setup(640, 640)
t.bgcolor('black')
t.tracer(10)
t.pensize(2)
R = 500

def tri(r,l):
    if l == 0:
        p=t.pos()
        d = sqrt(p[0]**2+p[1]**2)
        t.color(cs.hls_to_rgb(d/300,0.5,1))
        t.down()
        for i in range(3):
            t.fd(r)
            t.lt(120)
        t.up()
    else:
        t.fd(r/2)
        tri(r/2,l-1)
        t.lt(120)
        tri(r/2,l-1)
        t.fd(r/2)
        t.rt(120)
        tri(r/2,l-1)
        t.rt(120)
        t.fd(r/2)
        t.lt(120)

t.hideturtle()
y = sqrt(R**2-(R/2)**2)
t.up()
t.goto(-R/2,-y/2)
tri(R,7)

t.done()

No comments:

Post a Comment

Python Turtle Graphics Drawing - Gigabyte

 Python Turtle Graphics Drawing - Gigabyte Youtube Source code: from turtle import * setup ( 600 , 600 ) color ( '#2D68AE' ) up (...