sierpinski6.py

Created by andreanx

Created on March 04, 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")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.