Commit f1a31481 authored by Julian Müller's avatar Julian Müller
Browse files

Initial commit

parents
__pycache__/
\ No newline at end of file
from helpers import fill
from random import random
def weighted_choice(*weights):
def choice(seq):
w = fill(weights, len(seq), 1)
c = random() * sum(w)
acc = 0
for index, item in enumerate(seq):
acc += w[index]
if acc > c:
return item
return choice
def alwayscolor(color):
lambda i, d: color
def randomcolor(*colors):
return lambda i, d: random_choice(colors)
def vertexcolor(*colors):
return lambda i, d: colors[i]
def depthcolor(*colors):
return lambda i, d: colors[min(len(colors) - 1, d)]
from turtle import *
from random import choice as rchoice
from helpers import prepare, restore
from choices import weighted_choice, alwayscolor
def chooseByHand(vertices=3, **kwargs):
prepare()
vpos = []
def clickListener(x, y):
vpos.append((x,y))
goto(x, y)
dot(5, "black")
if len(vpos) == vertices:
onscreenclick(None)
draw(vpos, **kwargs)
restore()
onscreenclick(clickListener)
def equilateral(vertices=3, radius=300, **kwargs):
prepare()
vpos = []
for d in (i/vertices*360 for i in range(vertices)):
setheading(d + 90)
fd(radius)
dot(5, "black")
vpos.append(pos())
bk(radius)
draw(vpos, **kwargs)
restore()
def draw(vertex_positions, dots=10**4, dot_size=2, update_every=10**3,
walk_percentage=0.5, same_dest_multiplier=1,
choice=rchoice, color=alwayscolor("blue")):
update()
setpos(vertex_positions[0])
last_dest,depth = None,0
for i in range(1, dots + 1):
dest = choice(vertex_positions)
depth = depth + 1 if last_dest == dest else 0
last_dest = dest
setheading(towards(dest))
fd(distance(dest) * walk_percentage)
dot(dot_size * same_dest_multiplier ** depth,
color(vertex_positions.index(dest), depth))
if i % update_every == 0:
update()
print(i, "points drawn")
if __name__ == "__main__":
from choices import depthcolor, weighted_choice
ht()
equilateral(3, walk_percentage=0.4, dots=10**5,
color=depthcolor("yellow", "blue"),
choice=weighted_choice(2, 3, 3))
from turtle import *
before = True, True, True
prepared = False
def prepare():
global before
goto(0,0)
clear()
before = isvisible(), isdown(), tracer()
ht()
pu()
tracer(False)
def restore():
if before[0]: st()
if before[1]: pd()
tracer(before[2])
def fill(seq, n, filler):
return [*seq] + [filler for i in range(len(seq), n)]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment