-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_walk.py
79 lines (67 loc) · 2.36 KB
/
random_walk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import streamlit as st
import random
from patterns import Pattern, Turtle, bbox_contains
class RandomWalk(Pattern):
def options():
return {
"label": "Random Walk",
"inputs": {
"steps": {
"function": st.slider,
"args": {
"label": "Steps",
"min_value": 10,
"max_value": 2000,
"step": 1,
"value": 1000,
},
},
"heading_step": {
"function": st.slider,
"args": {
"label": "Heading Step",
"min_value": 1,
"max_value": 180,
"step": 1,
"value": 1,
},
},
"step_size": {
"function": st.slider,
"args": {
"label": "Step Size",
"min_value": 20,
"max_value": 100,
"step": 1,
"value": (20, 40),
},
},
"random_seed": {
"function": st.number_input,
"args": {
"label": "Random Seed",
"value": lambda: random.randint(0, 999_999_999),
"step": 1,
},
},
},
}
def pattern(self, steps, heading_step, step_size, random_seed):
min_step, max_step = step_size
random.seed(random_seed)
turtle = Turtle()
turtle.goto(*self.canvas.centroid)
headings = range(0, 360, heading_step)
for i in range(0, steps):
turtle.setheading(random.choice(headings))
turtle.forward(random.uniform(min_step, max_step))
pos = turtle.pos()
bbox = [pos[0], pos[1], pos[0] + 1, pos[1] + 1]
if not bbox_contains(self.canvas.bbox, bbox):
turtle.undo()
continue
self.canvas.pattern += pos
if i % 50 == 0:
yield self.canvas.pattern
self.canvas.pattern.center(*self.canvas.centroid)
yield self.canvas.pattern