sierpinski6.py

Created by andreanx

Created on March 03, 2021

870 Bytes

rotsierp(n=6, n0=0, s=1, h=222, m=3) Draws adjacent Sierpiński triangles starting with iterations and increasing by each time and with pixels of margin for a global height of pixels.


import turtle
from math import sqrt

def sierp(n, l):
  a, r = 60, 1
  if n <= 0:
    for i in range (3):
      turtle.fd(l)
      turtle.left(a * 2)
  else:
    l /= 2
    n -= 1
    sierp(n, l)
    turtle.fd(l)
    sierp(n, l)
    turtle.bk(l)
    turtle.left(a)
    turtle.fd(l)
    turtle.right(a)
    r += 3 * sierp(n, l)
    turtle.left(a)
    turtle.bk(l)
    turtle.right(a)
  return r

def rotsierp(n=6, n0=0, s=1, h=222, m=3):
  r = 0
  l = (h - m - m*sqrt(3)) / sqrt(3)
  turtle.penup()
  turtle.left(90)
  turtle.fd(m / 2)
  turtle.right(90)
  for k in range(n0, n0 + n):
    turtle.pendown()
    t = sierp(k,l)
    turtle.penup()
    turtle.right(90)
    turtle.fd(m)
    turtle.left(30)
    r += t
    print("+ " + str(t) + " triangle" + ((t > 1) and "s" or ""))
  return r

t = rotsierp(6, 0, 1, 222)
print("= " + str(t) + " triangles")