-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractal_restriction_3.py
103 lines (70 loc) · 2.13 KB
/
fractal_restriction_3.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Use the ``bokeh serve`` command to run the example by executing:
bokeh serve --show fractal_restriction_3.py
at your command prompt.
This will open a new window (or tab) in your browser at
http://localhost:5006/fractal_restriction_3
It will keep running until ctrl + c.
"""
from random import choice
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models.widgets import TextInput
from bokeh.plotting import figure
# Just for now, these points. Will generalize later:
points = {
'ax': 0,
'ay': 0,
'bx': 0,
'by': 10,
'cx': 10,
'cy': 10,
'dx': 10,
'dy': 0
}
last_target = choice(['a', 'b', 'c', 'd'])
last_x = points[last_target + 'x']
last_y = points[last_target + 'y']
def choose_next():
next_target = choice(['a', 'b', 'c', 'd'])
return next_target
def draw():
global last_x, last_y, last_target
flag = 0
while flag == 0:
next_target = choose_next()
if abs(ord(next_target) - ord(last_target)) == 2:
continue
else:
flag = 1
last_target = next_target
next_x = points[next_target + 'x']
next_y = points[next_target + 'y']
new_x = (next_x + last_x) / 2
new_y = (next_y + last_y) / 2
plot.circle(x=new_x, y=new_y, size=1, color='black')
last_x = new_x
last_y = new_y
def create_plot():
# Set up plot
plot = figure(plot_height=500, plot_width=500, title="Fractals",
tools="crosshair,pan,reset,save,wheel_zoom",
x_range=[0, 10], y_range=[0, 10])
# plot.border_fill_color = 'black'
# plot.background_fill_color = 'black'
plot.outline_line_color = None
plot.grid.grid_line_color = None
curdoc().add_periodic_callback(draw, 0.001)
return plot
plot = create_plot()
# Set up widgets
text = TextInput(title="title", value='Fractals')
# Set up callbacks
def update_title(attrname, old, new):
plot.title.text = text.value
text.on_change('value', update_title)
# Set up layouts and add to document
inputs = widgetbox(text)
layout = row(inputs, plot)
curdoc().add_root(layout)
curdoc().title = "Fractals"