Skip to content

Instantly share code, notes, and snippets.

@seantibor
Last active February 1, 2021 15:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save seantibor/e533b1a1baaab28bff566108acdf89d6 to your computer and use it in GitHub Desktop.
# Write your code here :-)
# idea from this YouTube video: https://www.youtube.com/watch?v=kbKtFN71Lfs
import random
import turtle as t
def midpoint(point1, point2):
# given two coordinates, find the point halfway between them
x1, y1 = point1
x2, y2 = point2
x = (x1 + x2) / 2
y = (y1 + y2) / 2
return (x, y)
def plot_point(x, y, label=None, radius=2):
# plots a single point in turtle
t.penup()
t.goto(x, y)
t.pendown()
t.circle(radius)
if label:
t.penup()
t.goto(x+10, y)
t.pendown()
t.write(label, font=('Arial', 16, 'normal'))
#make three points of a triangle
triangle = {"a": (0, 200), "b": (-200, -100), "c": (200, -100)}
# make it go fast
t.speed(0)
# plot the initial triangle
for label, point in triangle.items():
plot_point(*point, label=label)
# start at a point halfway between a and b
current_point = midpoint(triangle['a'], triangle['b'])
plot_point(*current_point)
# make a list of the vertices for later
vertices = list(triangle.values())
for i in range(1000):
#pick one of the vertices at random
destination = random.choice(vertices)
# find a point halfway between the current point and the random vertex
# make that the new current point
current_point = midpoint(current_point, destination)
plot_point(*current_point)
input("Type Enter to end")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment