Skip to content

Instantly share code, notes, and snippets.

@erdoganbavas
Created May 4, 2017 10:38
Show Gist options
  • Save erdoganbavas/0ccab54e7daeef89a9cf161b712bf5fa to your computer and use it in GitHub Desktop.
Save erdoganbavas/0ccab54e7daeef89a9cf161b712bf5fa to your computer and use it in GitHub Desktop.
import math
import turtle
from random import randint
def draw_list(turtle, coords, color):
for coord in coords:
draw_circle(turtle, color, 3, coord[0], coord[1])
def draw_circle(turtle, color, size, x, y):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.begin_fill()
turtle.pendown()
turtle.circle(size)
turtle.penup()
turtle.end_fill()
turtle.pendown()
# random starting point in an Isosceles Triangle
def get_st_point(t):
while True:
p = [randint(t[2][0], t[1][0]), randint(t[1][1],t[0][1])]
yn = t[0][1] - p[1]
h = t[0][1] - t[1][1]
if (abs(p[0]) / abs(yn)) < (abs(t[1][0]) / h):
return p
tommy = turtle.Turtle()
tommy.speed('fastest')
i = 0
iteration = 1000
t = [[0, 150], [150,-100], [-150,-100]] # triangle
# black point to the corners
draw_list(tommy, t, "black")
# find a random starting point
st = get_st_point(t)
draw_circle(tommy, "red", 0.5, st[0], st[1])
lp = st # last point
while True:
c = randint(0, 2) # random corner
np = [(t[c][0] + lp[0])/2, (t[c][1] + lp[1])/2]
draw_circle(tommy, "green", 0.5, np[0], np[1])
lp = np
if i > iteration:
break
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment