Skip to content

Commit 9af28f0

Browse files
authored
Merge pull request #153 from martinRenou/polygon_docs
Update docs
2 parents 0cc98b4 + f6cf237 commit 9af28f0

File tree

7 files changed

+189
-2
lines changed

7 files changed

+189
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ You can try it online by clicking on this badge:
1919

2020
You can read the documentation following this link: https://ipycanvas.readthedocs.io
2121

22+
## Questions?
23+
24+
If you have any question, or if you want to share what you do with ipycanvas, [start a new discussion on Github](https://github.com/martinRenou/ipycanvas/discussions/new)!
25+
2226
## Installation
2327

2428
You can install using `pip`:

docs/source/drawing_shapes.rst

Lines changed: 79 additions & 2 deletions
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

@@ -105,9 +161,10 @@ There are methods that draw arcs/circles on the canvas:
105161
Drawing lines
106162
-------------
107163

108-
There is one command for drawing a straight line from one point to another:
164+
There are two commands for drawing a straight line from one point to another:
109165

110166
- ``stroke_line(x1, y1, x2, y2)``: Draw a line from ``(x1, y1)`` to ``(x2, y2)``.
167+
- ``stroke_lines(points)``: Draw a path of consecutive lines from a list of points ``[(x1, y1), (x2, y2), ..., (xn, yn)]``.
111168

112169
.. code:: Python
113170
@@ -128,6 +185,26 @@ There is one command for drawing a straight line from one point to another:
128185
129186
.. image:: images/lines.png
130187

188+
.. code:: Python
189+
190+
import numpy as np
191+
192+
from ipycanvas import Canvas
193+
194+
canvas = Canvas(width=200, height=200)
195+
196+
n = 50
197+
x = np.linspace(0, 200, n)
198+
y = np.random.randint(200, size=n)
199+
200+
points = np.stack((x, y), axis=1)
201+
202+
canvas.stroke_lines(points)
203+
204+
canvas
205+
206+
.. image:: images/stroke_lines.png
207+
131208

132209
Vectorized methods
133210
------------------
@@ -168,4 +245,4 @@ instead of running:
168245
169246
canvas.fill_rect(position, position, size)
170247
171-
canvas
248+
canvas

docs/source/images/polygon.png

3.18 KB
Loading
3.2 KB
Loading
18.3 KB
Loading

docs/source/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ You can try ipycanvas, without the need of installing anything on your computer,
1414
.. image:: https://img.shields.io/badge/start-drawing!-F5A252.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA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jlJKAIZSbI8SGSF3k0pA3mR5tHuwPFoa7N7reoq2bqCsAk1HqCu5uvI1n6JuRXI+S1Mco54YmYTwcn6Aeic+kssXi8XpXC4V3t7/ADuTNKaQJdScAAAAAElFTkSuQmCC
1515
:target: https://mybinder.org/v2/gh/martinRenou/ipycanvas/stable?filepath=examples
1616

17+
Questions?
18+
----------
19+
20+
If you have any question, or if you want to share what you do with ipycanvas, come `start a new discussion on Github <https://github.com/martinRenou/ipycanvas/discussions/new>`_!
21+
1722
Index
1823
-----
1924

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)