Skip to content

Commit f90ea3c

Browse files
committed
Add docs for polygon
Signed-off-by: martinRenou <[email protected]>
1 parent 0cc98b4 commit f90ea3c

File tree

4 files changed

+158
-1
lines changed

4 files changed

+158
-1
lines changed

docs/source/drawing_shapes.rst

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,62 @@ You can also clear a certain canvas rectangle area:
6868
6969
.. image:: images/rects.png
7070

71+
Drawing polygons
72+
----------------
73+
74+
You can draw a polygon by providing a list of points, either a Python list, or a NumPy array.
75+
It's the fastest way to draw a polygon with ipycanvas.
76+
77+
- ``fill_polygon(points)``: Fill a polygon from a list of points ``[(x1, y1), (x2, y2), ..., (xn, yn)]``.
78+
- ``stroke_polygon(points)``: Draw polygon outline from a list of points ``[(x1, y1), (x2, y2), ..., (xn, yn)]``.
79+
80+
.. code:: Python
81+
82+
from ipycanvas import Canvas
83+
84+
canvas = Canvas(width=200, height=200)
85+
86+
canvas.fill_style = '#63934e'
87+
canvas.stroke_style = '#4e6393'
88+
canvas.line_width = 5
89+
canvas.fill_polygon([(20, 20), (180, 20), (100, 150)])
90+
canvas.stroke_polygon([(20, 20), (180, 20), (100, 150)])
91+
92+
canvas
93+
94+
.. image:: images/polygon.png
95+
96+
.. code:: Python
97+
98+
from math import pi
99+
import numpy as np
100+
from ipycanvas import Canvas
101+
102+
def polygon(canvas, x, y, radius, n_points):
103+
angles = (2 * pi / n_points) * np.arange(n_points)
104+
105+
v_x = x + np.cos(angles) * radius
106+
v_y = y + np.sin(angles) * radius
107+
108+
points = np.stack((v_x, v_y), axis=1)
109+
110+
canvas.fill_polygon(points)
111+
112+
background_color = '#89c64f'
113+
polygon_color = '#c6574f'
114+
115+
canvas = Canvas(width=200, height=200)
116+
117+
canvas.fill_style = background_color
118+
canvas.fill_rect(0, 0, canvas.width, canvas.height)
119+
120+
canvas.fill_style = polygon_color
121+
polygon(canvas, 100, 100, 70, 6)
122+
123+
canvas
124+
125+
.. image:: images/polygon_numpy.png
126+
71127
Drawing arcs and circles
72128
------------------------
73129

@@ -168,4 +224,4 @@ instead of running:
168224
169225
canvas.fill_rect(position, position, size)
170226
171-
canvas
227+
canvas

docs/source/images/polygon.png

3.18 KB
Loading
3.2 KB
Loading

examples/polygon.ipynb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from ipycanvas import Canvas\n",
10+
"\n",
11+
"canvas = Canvas(width=200, height=200)\n",
12+
"\n",
13+
"canvas.fill_style = '#63934e'\n",
14+
"canvas.stroke_style = '#4e6393'\n",
15+
"canvas.line_width = 5\n",
16+
"canvas.fill_polygon([(20, 20), (180, 20), (100, 150)])\n",
17+
"canvas.stroke_polygon([(20, 20), (180, 20), (100, 150)])\n",
18+
"\n",
19+
"canvas"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"from math import pi\n",
29+
"\n",
30+
"import numpy as np\n",
31+
"\n",
32+
"from ipycanvas import Canvas"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"def polygon(canvas, x, y, radius, n_points):\n",
42+
" angles = (2 * pi / n_points) * np.arange(n_points)\n",
43+
"\n",
44+
" v_x = x + np.cos(angles) * radius\n",
45+
" v_y = y + np.sin(angles) * radius\n",
46+
"\n",
47+
" points = np.stack((v_x, v_y), axis=1)\n",
48+
"\n",
49+
" canvas.fill_polygon(points)"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"background_color = '#89c64f'\n",
59+
"polygon_color = '#c6574f'"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"canvas = Canvas(width=500, height=500)\n",
69+
"\n",
70+
"canvas.fill_style = background_color\n",
71+
"canvas.fill_rect(0, 0, canvas.width, canvas.height)\n",
72+
"\n",
73+
"canvas.fill_style = polygon_color\n",
74+
"polygon(canvas, 250, 250, 100, 6)\n",
75+
"\n",
76+
"canvas"
77+
]
78+
}
79+
],
80+
"metadata": {
81+
"kernelspec": {
82+
"display_name": "Python 3",
83+
"language": "python",
84+
"name": "python3"
85+
},
86+
"language_info": {
87+
"codemirror_mode": {
88+
"name": "ipython",
89+
"version": 3
90+
},
91+
"file_extension": ".py",
92+
"mimetype": "text/x-python",
93+
"name": "python",
94+
"nbconvert_exporter": "python",
95+
"pygments_lexer": "ipython3",
96+
"version": "3.9.0"
97+
}
98+
},
99+
"nbformat": 4,
100+
"nbformat_minor": 4
101+
}

0 commit comments

Comments
 (0)