January's #AdventureInSTEM is Sierpinski Triangles! Learn how to create this fractal with a python turtle.
To learn how to cut a paper Sierpinski Triangle check out this video: • Sierpinski Triangles! an Adventure in STEM
For the whole Adventure in STEM series visit: https://lasteamlab.com/curriculum/adv...
For this experiment you will need:
a computer
access to the internet
Find us on Facebook:
FSN: / familystrengthsnetwork
STEAM Lab: / lasteamlab
Find us on YouTube:
FSN: / @familystrengthsnetwork3008
STEAM Lab: / @losalamossteamlab
Find us on the Web:
FSN: lafsn.org
STEAM Lab: https://lasteamlab.com
#AdventuresInSTEM are a collaboration between Los Alamos STEAM Lab and Family Strengths Network.
Here's the code:
import random
c="" # corner
p="" # point
def turtle_setup(canv_width, canv_height):
""" Set up the canvas and a turtle for creating dots. Return a turtle
object in hidden state with its pen up. The canvas has size canv_width
by canv_height, with a coordinate system where (0,0) is in the bottom
left corner, and automatic re-drawing of the canvas is disabled so that
turtle.update() must be called to update the drawing.
"""
t = turtle.Turtle()
screen = t.getscreen()
screen.setup(canv_width, canv_height)
screen.setworldcoordinates(0, 0, canv_width, canv_height)
t.hideturtle()
t.speed(0) #0 is the fastest speed, 10 is fast, 6 is normal, slow is 3, and 1 is slowest
t.color("black")#can change the color of the dots here
#uncomment the below line and the last line if you do NOT want to watch the triangle being drawn
#screen.tracer(0, 0)
return t
def midpoint(a, b):
""" Return the midpoint between points a = (ax, ay) and b = (bx, by) """
ax, ay = a
bx, by = b
mx = (ax + bx) / 2
my = (ay + by) / 2
return mx, my
def randopoint():
"""generates a random point given the window size specified in the system arguments"""
xcoord= random.randint(0, canv_width)
ycoord= random.randint(0, canv_height)
randopoint = (xcoord, ycoord)
p=randopoint # Let p be a random point in the window
return p
def randocorner():
"""produces the coordinates of one of the three corners
that has been determined by the window size from the system arguments"""
randocorner=random.choice(corners)
c = randocorner
return c
if _name_ == "__main__":
import sys
import turtle
"""uncomment the following 2 lines if you want to set canvas size from command line arguments"""
#canv_width = int(sys.argv[1])
#canv_height = int(sys.argv[2])
""" if using command line arguments (above) comment out the following two lines,
otherwise you can use them to set the canvas size """
canv_width = 500
canv_height = 500
t = turtle_setup(400, 400)
turtle_setup(canv_width, canv_height)
#establishes corners
middlex= (canv_width/2)
corner1= (middlex, canv_height)
corner2= (0,0)
corner3= (canv_width, 0)
corners= [corner1, corner2, corner3]
p= randopoint() #random starting point in the window
c= randocorner() #random corner of the triangle
m= midpoint(p,c) #midpoint between point p and a random corner of the triangle
t.up()
#use the below line to set the number of iterations, higher number = more dots and longer running time
for i in range (100000):
c=randocorner()
m=midpoint(p,c)
t.goto(m)
if i (greater than - replace this with the symbol that youtube won't allow) 5:
t.pendown()
t.dot(2) #can change the size of the dots here
t.penup()
p=m
#turtle.update()
Sur cette page du site, vous pouvez voir la vidéo en ligne Sierpinski Triangle Chaos Game: A Python Tutorial durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Los Alamos STEAM Lab 26 janvier 2021, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 1,982 fois et il a aimé 15 téléspectateurs. Bon visionnage!