Skip to content

Commit b3944b7

Browse files
authored
Merge pull request #220 from martinRenou/fractals_forest
Add Fractals forest example
2 parents b91b25e + 61838d1 commit b3944b7

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

examples/fractals_forest.ipynb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "364e2f77",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from math import pi\n",
11+
"from random import choice, uniform, randint\n",
12+
"\n",
13+
"from ipycanvas import Canvas, hold_canvas\n",
14+
"from ipywidgets import Button\n",
15+
"\n",
16+
"\n",
17+
"def draw_tree(canvas, start_x, start_y, length, angle, branch_width):\n",
18+
" canvas.begin_path()\n",
19+
" canvas.save()\n",
20+
" canvas.line_width = branch_width\n",
21+
" canvas.translate(start_x, start_y)\n",
22+
" canvas.rotate(angle * pi / 180)\n",
23+
" canvas.move_to(0, 0)\n",
24+
" canvas.line_to(0, -length)\n",
25+
"\n",
26+
" if length > 25:\n",
27+
" canvas.stroke_style = '#c68a47'\n",
28+
" else:\n",
29+
" canvas.stroke_style = choice(['#559b37', '#379b4b', '#879b37'])\n",
30+
"\n",
31+
" canvas.stroke()\n",
32+
"\n",
33+
" if length < 10:\n",
34+
" canvas.restore()\n",
35+
" return\n",
36+
"\n",
37+
" draw_tree(canvas, 0, -length, length * uniform(0.8, 0.9), angle + uniform(-1, 10), branch_width * 0.7)\n",
38+
" draw_tree(canvas, 0, -length, length * uniform(0.8, 0.9), angle + uniform(-10, 1), branch_width * 0.7)\n",
39+
" canvas.restore()\n",
40+
"\n",
41+
"canvas = Canvas()\n",
42+
"\n",
43+
"canvas.shadow_color = 'black'\n",
44+
"canvas.shadow_offset_x = 1\n",
45+
"canvas.shadow_offset_y = 0\n",
46+
"canvas.shadow_blur = 3\n",
47+
"canvas.line_cap = 'round'\n",
48+
"\n",
49+
"\n",
50+
"def draw_random_forest(*args):\n",
51+
" global canvas\n",
52+
"\n",
53+
" with hold_canvas(canvas):\n",
54+
" canvas.clear()\n",
55+
" for tree in range(randint(6, 10)):\n",
56+
" draw_tree(canvas, uniform(0, canvas.width), canvas.height, uniform(40, 70), uniform(-10, 10), uniform(8, 15))\n",
57+
" canvas.fill_style = '#559b37'\n",
58+
" canvas.fill_rect(0, canvas.height - 5, canvas.width, canvas.height)\n",
59+
"\n",
60+
"draw_random_forest()\n",
61+
"\n",
62+
"button = Button(description='Generate new forest')\n",
63+
"button.on_click(draw_random_forest)\n",
64+
"\n",
65+
"display(canvas, button)"
66+
]
67+
}
68+
],
69+
"metadata": {
70+
"kernelspec": {
71+
"display_name": "Python 3 (ipykernel)",
72+
"language": "python",
73+
"name": "python3"
74+
},
75+
"language_info": {
76+
"codemirror_mode": {
77+
"name": "ipython",
78+
"version": 3
79+
},
80+
"file_extension": ".py",
81+
"mimetype": "text/x-python",
82+
"name": "python",
83+
"nbconvert_exporter": "python",
84+
"pygments_lexer": "ipython3",
85+
"version": "3.9.7"
86+
}
87+
},
88+
"nbformat": 4,
89+
"nbformat_minor": 5
90+
}

0 commit comments

Comments
 (0)