Skip to content

Commit a2276cc

Browse files
committed
Fractals tree example
Signed-off-by: martinRenou <[email protected]>
1 parent 74cb7f7 commit a2276cc

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

examples/fractals_tree.ipynb

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from math import pi\n",
10+
"from random import uniform\n",
11+
"\n",
12+
"from ipywidgets import Button\n",
13+
"\n",
14+
"from ipycanvas import Canvas, hold_canvas"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"canvas = Canvas(width=800, height=600)"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"def recursive_draw_leaf(canvas, length, r_angle, r_factor, l_angle, l_factor):\n",
33+
" canvas.stroke_line(0, 0, 0, -length)\n",
34+
" canvas.translate(0, -length)\n",
35+
"\n",
36+
" if length > 5 :\n",
37+
" canvas.save()\n",
38+
"\n",
39+
" canvas.rotate(r_angle)\n",
40+
" recursive_draw_leaf(canvas, length * r_factor, r_angle, r_factor, l_angle, l_factor)\n",
41+
"\n",
42+
" canvas.restore()\n",
43+
"\n",
44+
" canvas.save()\n",
45+
"\n",
46+
" canvas.rotate(l_angle)\n",
47+
" recursive_draw_leaf(canvas, length * l_factor, r_angle, r_factor, l_angle, l_factor)\n",
48+
"\n",
49+
" canvas.restore()"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"def draw_tree(canvas):\n",
59+
" with hold_canvas(canvas):\n",
60+
" canvas.save()\n",
61+
"\n",
62+
" canvas.clear()\n",
63+
"\n",
64+
" canvas.translate(canvas.width / 2., canvas.height)\n",
65+
"\n",
66+
" canvas.stroke_style = 'black'\n",
67+
"\n",
68+
" r_factor = uniform(0.6, 0.8)\n",
69+
" l_factor = uniform(0.6, 0.8)\n",
70+
"\n",
71+
" r_angle = uniform(pi / 10., pi / 5.)\n",
72+
" l_angle = uniform(-pi / 5., -pi / 10.)\n",
73+
"\n",
74+
" recursive_draw_leaf(canvas, 150, r_angle, r_factor, l_angle, l_factor)\n",
75+
"\n",
76+
" canvas.restore()"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"button = Button(description='Generate tree!')\n",
86+
"\n",
87+
"def click_callback(*args, **kwargs):\n",
88+
" global canvas\n",
89+
"\n",
90+
" draw_tree(canvas)\n",
91+
"\n",
92+
"button.on_click(click_callback)"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": null,
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"draw_tree(canvas)"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {},
108+
"outputs": [],
109+
"source": [
110+
"display(canvas)\n",
111+
"display(button)"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"metadata": {},
117+
"source": [
118+
"## Acknowledgment:\n",
119+
"\n",
120+
"This Notebook is adapted from a Notebook by [Eric MADEC](https://github.com/ericecmorlaix) which was itself adapted from https://medium.com/better-programming/learning-p5-js-by-making-fractals-cbdcac5c651e"
121+
]
122+
}
123+
],
124+
"metadata": {
125+
"kernelspec": {
126+
"display_name": "Python 3",
127+
"language": "python",
128+
"name": "python3"
129+
},
130+
"language_info": {
131+
"codemirror_mode": {
132+
"name": "ipython",
133+
"version": 3
134+
},
135+
"file_extension": ".py",
136+
"mimetype": "text/x-python",
137+
"name": "python",
138+
"nbconvert_exporter": "python",
139+
"pygments_lexer": "ipython3",
140+
"version": "3.9.0"
141+
},
142+
"toc": {
143+
"base_numbering": 1,
144+
"nav_menu": {},
145+
"number_sections": true,
146+
"sideBar": true,
147+
"skip_h1_title": false,
148+
"title_cell": "Table of Contents",
149+
"title_sidebar": "Contents",
150+
"toc_cell": false,
151+
"toc_position": {},
152+
"toc_section_display": true,
153+
"toc_window_display": false
154+
},
155+
"varInspector": {
156+
"cols": {
157+
"lenName": 16,
158+
"lenType": 16,
159+
"lenVar": 40
160+
},
161+
"kernels_config": {
162+
"python": {
163+
"delete_cmd_postfix": "",
164+
"delete_cmd_prefix": "del ",
165+
"library": "var_list.py",
166+
"varRefreshCmd": "print(var_dic_list())"
167+
},
168+
"r": {
169+
"delete_cmd_postfix": ") ",
170+
"delete_cmd_prefix": "rm(",
171+
"library": "var_list.r",
172+
"varRefreshCmd": "cat(var_dic_list()) "
173+
}
174+
},
175+
"types_to_exclude": [
176+
"module",
177+
"function",
178+
"builtin_function_or_method",
179+
"instance",
180+
"_Feature"
181+
],
182+
"window_display": false
183+
}
184+
},
185+
"nbformat": 4,
186+
"nbformat_minor": 4
187+
}

0 commit comments

Comments
 (0)