from gturtle import *
import random
colors = ["white","red","green","blue"]
WIDTH = 1200
HEIGHT = 800
Options.setPlaygroundSize(WIDTH,HEIGHT)
t = Turtle()
t.setFillColor("black")
t.fill(0,0)
t.setPenColor("white")
t.setPenWidth(1)
t.hideTurtle()
### dimensions
scale = 0.5
d = int(50 * scale)
d1 = int(15 * scale)
ang1 = 45
d2 = int(20 * scale)
d3 = int(15 * scale)
ang3 = 55
def schneeflocke(x=0,y=0,color="white"):
t.setPenColor(color)
for i in range(6):
t.home()
t.setPos(x,y)
t.left(60*i+30)
t.forward(d)
t.back(d1)
t.left(ang1)
t.forward(d1)
t.back(d1)
t.right(2*ang1)
t.forward(d1)
t.back(d1)
t.left(ang1)
t.back(d2)
t.left(ang3)
t.forward(d3)
t.back(d3)
t.right(2*ang3)
t.forward(d3)
t.back(d3)
t.left(ang3)
def get_nonoverlapping_positions(n=10,d_min=2,width=800,height=600):
positions = []
for i in range(n):
while True:
x = random.randint(-width//2,width//2)
y = random.randint(-height//2,height//2)
# list empty
if len(positions) == 0:
positions.append((x,y))
break
# check if point is far enough from other points
too_close = False
for (px,py) in positions:
d = ((px-x)**2 + (py-y)**2)**0.5
if d < d_min:
too_close = True
break
if not too_close:
positions.append((x,y))
break
return positions
positions = get_nonoverlapping_positions(n=120,d_min=70,width=WIDTH,height=HEIGHT)
for x,y in positions:
schneeflocke(x,y,random.choice(colors))
#t.savePlayground("/Users/anschae/schneeflocken.png","png")