beziervis 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,130 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ .pylint.d/
4
+ *.py[cod]
5
+ *$py.class
6
+ .vscode/
7
+ # C extensions
8
+ *.so
9
+
10
+ # Distribution / packaging
11
+ .Python
12
+ build/
13
+ develop-eggs/
14
+ dist/
15
+ downloads/
16
+ eggs/
17
+ .eggs/
18
+ lib/
19
+ lib64/
20
+ parts/
21
+ sdist/
22
+ var/
23
+ wheels/
24
+ pip-wheel-metadata/
25
+ share/python-wheels/
26
+ *.egg-info/
27
+ .installed.cfg
28
+ *.egg
29
+ MANIFEST
30
+
31
+ # PyInstaller
32
+ # Usually these files are written by a python script from a template
33
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
34
+ *.manifest
35
+ *.spec
36
+
37
+ # Installer logs
38
+ pip-log.txt
39
+ pip-delete-this-directory.txt
40
+
41
+ # Unit test / coverage reports
42
+ htmlcov/
43
+ .tox/
44
+ .nox/
45
+ .coverage
46
+ .coverage.*
47
+ .cache
48
+ nosetests.xml
49
+ coverage.xml
50
+ *.cover
51
+ *.py,cover
52
+ .hypothesis/
53
+ .pytest_cache/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ .python-version
87
+
88
+ # pipenv
89
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
90
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
91
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
92
+ # install all needed dependencies.
93
+ #Pipfile.lock
94
+
95
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
96
+ __pypackages__/
97
+
98
+ # Celery stuff
99
+ celerybeat-schedule
100
+ celerybeat.pid
101
+
102
+ # SageMath parsed files
103
+ *.sage.py
104
+
105
+ # Environments
106
+ .env
107
+ .venv
108
+ env/
109
+ venv/
110
+ ENV/
111
+ env.bak/
112
+ venv.bak/
113
+
114
+ # Spyder project settings
115
+ .spyderproject
116
+ .spyproject
117
+
118
+ # Rope project settings
119
+ .ropeproject
120
+
121
+ # mkdocs documentation
122
+ /site
123
+
124
+ # mypy
125
+ .mypy_cache/
126
+ .dmypy.json
127
+ dmypy.json
128
+
129
+ # Pyre type checker
130
+ .pyre/
@@ -0,0 +1,21 @@
1
+ cff-version: 1.2.0
2
+ message: "If you use this software, please cite it as below."
3
+ type: software
4
+ title: "beziervis: Bézier curve evaluation and de Casteljau animations for Python"
5
+ abstract: >-
6
+ A Python library for evaluating and manipulating Bézier curves in any
7
+ dimension, with a vectorised NumPy core, optional Numba acceleration, and
8
+ animations of de Casteljau's algorithm.
9
+ authors:
10
+ - family-names: Gopisetti
11
+ given-names: Venkateshwara
12
+ version: 1.0.0
13
+ date-released: 2026-11-24
14
+ license: MIT
15
+ repository-code: "https://github.com/venkateshwara-gopisetti/Bezier-Curves"
16
+ keywords:
17
+ - bezier curve
18
+ - de Casteljau
19
+ - computational geometry
20
+ - visualization
21
+ - numpy
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-2026 Venkateshwara Gopisetti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,209 @@
1
+ Metadata-Version: 2.5
2
+ Name: beziervis
3
+ Version: 1.0.0
4
+ Summary: Fast Bézier curve evaluation in NumPy, with animations of de Casteljau's algorithm.
5
+ Project-URL: Homepage, https://github.com/venkateshwara-gopisetti/Bezier-Curves
6
+ Project-URL: Issues, https://github.com/venkateshwara-gopisetti/Bezier-Curves/issues
7
+ Author: Venkateshwara Gopisetti
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: animation,bezier,bezier-curve,de-casteljau,geometry,numba,numpy,visualization
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Intended Audience :: Education
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Topic :: Multimedia :: Graphics
21
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
22
+ Classifier: Topic :: Scientific/Engineering :: Visualization
23
+ Requires-Python: >=3.10
24
+ Requires-Dist: numpy>=1.23
25
+ Provides-Extra: all
26
+ Requires-Dist: matplotlib>=3.6; extra == 'all'
27
+ Requires-Dist: numba>=0.59; extra == 'all'
28
+ Requires-Dist: pillow>=9; extra == 'all'
29
+ Provides-Extra: anim
30
+ Requires-Dist: matplotlib>=3.6; extra == 'anim'
31
+ Requires-Dist: pillow>=9; extra == 'anim'
32
+ Provides-Extra: dev
33
+ Requires-Dist: matplotlib>=3.6; extra == 'dev'
34
+ Requires-Dist: numba>=0.59; extra == 'dev'
35
+ Requires-Dist: pillow>=9; extra == 'dev'
36
+ Requires-Dist: pylint>=3; extra == 'dev'
37
+ Requires-Dist: pytest>=7; extra == 'dev'
38
+ Provides-Extra: fast
39
+ Requires-Dist: numba>=0.59; extra == 'fast'
40
+ Description-Content-Type: text/markdown
41
+
42
+ # beziervis
43
+
44
+ **Watch a Bézier curve build itself, and evaluate millions of curve points with a fast NumPy core.**
45
+
46
+ [![Tests](https://github.com/venkateshwara-gopisetti/Bezier-Curves/actions/workflows/tests.yml/badge.svg)](https://github.com/venkateshwara-gopisetti/Bezier-Curves/actions/workflows/tests.yml)
47
+ [![PyPI](https://img.shields.io/pypi/v/beziervis.svg)](https://pypi.org/project/beziervis/)
48
+ [![Python](https://img.shields.io/pypi/pyversions/beziervis.svg)](https://pypi.org/project/beziervis/)
49
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/LICENSE)
50
+ [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/quickstart.ipynb)
51
+
52
+ <p align="center">
53
+ <img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/hero.gif" width="480" alt="Animation of de Casteljau's algorithm tracing a degree-4 Bézier curve">
54
+ </p>
55
+
56
+ - **For learning:** animate de Casteljau's algorithm level by level, then save it as a GIF or MP4 or play it inline in Jupyter.
57
+ - **For computing:** evaluate curves of any degree, in any dimension, singly or in batches of thousands. Under the hood this is a Bernstein-matrix NumPy core, with optional Numba kernels for a further 3–40× speed-up.
58
+ - **Small:** the only required dependency is NumPy. Animation and acceleration are opt-in extras.
59
+
60
+ [Quickstart notebook](https://colab.research.google.com/github/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/quickstart.ipynb) · [Gallery](#gallery) · [API](#api) · [CLI](#command-line) · [How it works](#how-de-casteljaus-algorithm-works) · [Performance](#performance) · [Contributing](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/CONTRIBUTING.md) · [Changelog](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/CHANGELOG.md)
61
+
62
+ ## Install
63
+
64
+ ```bash
65
+ pip install beziervis # core: NumPy only
66
+ pip install "beziervis[all]" # + Numba acceleration and matplotlib animations
67
+ ```
68
+
69
+ | extra | adds | for |
70
+ |---|---|---|
71
+ | `[anim]` | matplotlib, Pillow | `animate()` and the `beziervis` command |
72
+ | `[fast]` | Numba | JIT and multi-threaded evaluation, used automatically |
73
+ | `[all]` | both | everything |
74
+
75
+ Requires Python 3.10 or newer. Tested on Linux, macOS and Windows.
76
+
77
+ ## Quickstart
78
+
79
+ ```python
80
+ import beziervis as bv
81
+
82
+ ctrl = [[0, 0], [5, 10], [9, 7], [4, 3]] # 4 control points -> a cubic curve
83
+
84
+ pts = bv.points(ctrl, n=200) # (200, 2) NumPy array, from ctrl[0] to ctrl[-1]
85
+ bv.animate(ctrl, verbose=True).save("curve.gif")
86
+ ```
87
+
88
+ <p align="center">
89
+ <img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/simple.gif" width="320" alt="A cubic Bézier curve being traced">
90
+ </p>
91
+
92
+ - **Input:** control points can be a list or a NumPy array of shape `(k, d)`, in any dimension `d`; the result has shape `(n, d)`.
93
+ - **Displaying:** `.show()` opens a window. In Jupyter, an animation that is the last expression in a cell plays inline.
94
+ - **Saving:** `.save()` writes `.gif` (Pillow) or `.mp4` (needs ffmpeg), and works on servers and in CI without a display.
95
+
96
+ ## Gallery
97
+
98
+ Ten short, self-contained scripts in [`examples/`](https://github.com/venkateshwara-gopisetti/Bezier-Curves/tree/master/examples), each a few dozen lines. Click an image for its code.
99
+
100
+ <table>
101
+ <tr><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/loop_animation.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/loop.gif" alt="A loop, every level drawn"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/loop_animation.py">A loop, every level drawn</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/generative_art.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/generative_art.png" alt="3,000 curves in one call"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/generative_art.py">3,000 curves in one call</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/calligraphy.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/calligraphy.png" alt="Broad-nib calligraphy"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/calligraphy.py">Broad-nib calligraphy</a></sub></td></tr>
102
+ <tr><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/easing.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/easing.png" alt="CSS easing curves"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/easing.py">CSS easing curves</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/curvature_comb.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/curvature_comb.png" alt="Curvature comb"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/curvature_comb.py">Curvature comb</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/helix_3d.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/helix_3d.png" alt="A 3-D spring"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/helix_3d.py">A 3-D spring</a></sub></td></tr>
103
+ <tr><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/heart.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/heart.png" alt="Two mirrored cubics"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/heart.py">Two mirrored cubics</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/arc_length.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/arc_length.png" alt="Even spacing by length"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/arc_length.py">Even spacing by length</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/subdivision.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/subdivision.png" alt="Recursive subdivision"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/subdivision.py">Recursive subdivision</a></sub></td></tr>
104
+ </table>
105
+
106
+ ## API
107
+
108
+ | function | returns |
109
+ |---|---|
110
+ | `points(ctrl, n=100, t=None)` | `(n, d)` points at `n` evenly spaced `t` in [0, 1], or at the `t` values you pass |
111
+ | `points_batch(curves, n=100)` | `(m, n, d)` points for `m` curves of equal degree, given as `(m, k, d)` |
112
+ | `casteljau_steps(ctrl, t)` | every level of the construction: `[(k, d), (k-1, d), …, (1, d)]` |
113
+ | `split(ctrl, t=0.5)` | `(left, right)`: two curves of the same degree that join at `t` |
114
+ | `derivative(ctrl)` | control points of the derivative (hodograph) curve |
115
+ | `elevate(ctrl, times=1)` | the same curve with `times` more control points |
116
+ | `arc_length(ctrl)` | the curve's length (Gauss–Legendre quadrature, error around 1e-10) |
117
+ | `animate(ctrl, frames=100, verbose=False, …)` | a `BezierAnimation` with `.save()`, `.show()` and `.to_jshtml()` |
118
+ | `random_control_points(n, seed=None)` | `n` random integer points in [-100, 100) |
119
+ | `set_backend(name)` / `get_backend()` | choose or report the backend: `"numpy"`, `"numba"` or `"auto"` |
120
+
121
+ `animate()` also takes `labels`, `fps`, `hold` (frames to pause on the finished curve), `size` (pixels), `cmap` and `title`. Every function has a docstring with the details; try `help(bv.animate)`.
122
+
123
+ ```python
124
+ import numpy as np
125
+
126
+ left, right = bv.split(ctrl, 0.5)
127
+ velocity = bv.points(bv.derivative(ctrl), n=200) # tangent vectors along the curve
128
+ length = bv.arc_length(ctrl)
129
+ many = bv.points_batch(np.random.rand(10_000, 4, 2), n=100) # (10000, 100, 2)
130
+ ```
131
+
132
+ ## Command line
133
+
134
+ ```bash
135
+ beziervis --points "0,0 5,10 9,7 4,3" --verbose --save curve.gif
136
+ beziervis -n 5 --seed 42 # 5 random control points, reproducible
137
+ python -m beziervis --help # same thing, without the script on PATH
138
+ ```
139
+
140
+ | option | meaning |
141
+ |---|---|
142
+ | `-p, --points "x,y x,y …"` | control points (otherwise random) |
143
+ | `-n N` | number of random control points (default 4) |
144
+ | `--seed S` | random seed |
145
+ | `-v, --verbose` | draw the intermediate construction lines |
146
+ | `-f, --frames F` / `--fps R` | animation steps (default 100) and playback speed (default 30) |
147
+ | `--size PX` / `--no-labels` | figure size in pixels; hide point labels |
148
+ | `-s, --save FILE` | write `.gif` or `.mp4` instead of opening a window |
149
+
150
+ ## How de Casteljau's algorithm works
151
+
152
+ A Bézier curve is defined by its control points (`A0 … A3` below). To find the point at parameter `t`:
153
+
154
+ 1. Walk a fraction `t` along each edge of the control polygon. That gives one fewer point (`B0 … B2`).
155
+ 2. Repeat on the new points (`C0, C1`), then again, until a single point is left.
156
+ 3. That point (red) is on the curve. Sweep `t` from 0 to 1 and it traces the whole curve.
157
+
158
+ ![The same cubic curve at t = 0.2, 0.4, 0.6 and 0.8](https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/how-it-works.png)
159
+
160
+ ```python
161
+ steps = bv.casteljau_steps(ctrl, t=0.4) # [A (4×2), B (3×2), C (2×2), point (1×2)]
162
+ ```
163
+
164
+ The construction also splits a curve in two exactly: `A0, B0, C0` and the red point are the control points of the left half. This is how `split()` is implemented.
165
+
166
+ ## Performance
167
+
168
+ Points are evaluated with a precomputed **Bernstein matrix** and one matrix multiply. Above degree 30, where that becomes numerically fragile, a **vectorised de Casteljau** is used instead. When Numba is installed, JIT-compiled kernels (multi-threaded for large inputs) are used automatically. Every backend agrees with the others to within 1e-12.
169
+
170
+ | case | naive Python | NumPy Bernstein | NumPy de Casteljau | Numba serial | Numba parallel |
171
+ |---|---|---|---|---|---|
172
+ | cubic, 1k points | 5.95 ms | 60 µs | 53.6 µs | 22.8 µs | 21.1 µs |
173
+ | cubic, 1M points | – | 71.5 ms | 79.2 ms | 25.9 ms | 10.6 ms |
174
+ | degree 10, 100k points | – | 21.4 ms | 52.4 ms | 7.93 ms | 2.33 ms |
175
+ | degree 50, 100k points | – | – | 1.02 s | 131 ms | 25 ms |
176
+ | 10k cubics × 100 points | – | 36.1 ms | – | – | 4.85 ms |
177
+
178
+ <sub>Best of 5 runs on Windows 11, Python 3.12, NumPy 2.5, Numba 0.68. To reproduce: `python benchmarks/bench.py`.</sub>
179
+
180
+ - **Choosing a backend:** set `BEZIERVIS_BACKEND=numpy|numba|auto` before import, or call `bv.set_backend(...)` at any time.
181
+ - **First Numba call:** it compiles the kernels, which takes about a second the first time and is then cached on disk.
182
+ - **Small inputs:** for a single curve with a few hundred points, every backend takes microseconds, so pick whichever you have installed.
183
+
184
+ ## FAQ
185
+
186
+ **Saving `.mp4` fails.** MP4 needs [ffmpeg](https://ffmpeg.org/download.html) on your `PATH`. GIF needs only Pillow.
187
+
188
+ **The animation is cluttered with many control points.** Use `verbose=False`, which draws only the polygon and the curve, or `labels=False`. Labels switch off automatically above 10 points.
189
+
190
+ **Can I animate 3-D curves?** Not yet; `animate()` draws 2-D curves. The maths functions (`points`, `split`, `derivative`, …) work in any dimension. 3-D animation is on the [roadmap](https://github.com/venkateshwara-gopisetti/Bezier-Curves/issues).
191
+
192
+ **How does this compare with the [`bezier`](https://pypi.org/project/bezier/) package?** `bezier` is a mature computational-geometry library with curve intersection, surfaces and a compiled core, and the right choice for CAD-grade geometry. beziervis is smaller and focused on teaching and visualising the construction, plus fast batch evaluation in pure NumPy or Numba.
193
+
194
+ ## Contributing
195
+
196
+ Bug reports, ideas and pull requests are welcome. See [CONTRIBUTING.md](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/CONTRIBUTING.md) for setup, tests and style. Issues labelled [good first issue](https://github.com/venkateshwara-gopisetti/Bezier-Curves/labels/good%20first%20issue) are a good place to start.
197
+
198
+ ```bash
199
+ git clone https://github.com/venkateshwara-gopisetti/Bezier-Curves && cd Bezier-Curves
200
+ pip install -e ".[dev]" && pytest
201
+ ```
202
+
203
+ ## Citation
204
+
205
+ If beziervis helps your research or teaching, please cite it. GitHub's "Cite this repository" button (from [`CITATION.cff`](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/CITATION.cff)) gives BibTeX and APA.
206
+
207
+ ## License
208
+
209
+ [MIT](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/LICENSE) © Venkateshwara Gopisetti
@@ -0,0 +1,168 @@
1
+ # beziervis
2
+
3
+ **Watch a Bézier curve build itself, and evaluate millions of curve points with a fast NumPy core.**
4
+
5
+ [![Tests](https://github.com/venkateshwara-gopisetti/Bezier-Curves/actions/workflows/tests.yml/badge.svg)](https://github.com/venkateshwara-gopisetti/Bezier-Curves/actions/workflows/tests.yml)
6
+ [![PyPI](https://img.shields.io/pypi/v/beziervis.svg)](https://pypi.org/project/beziervis/)
7
+ [![Python](https://img.shields.io/pypi/pyversions/beziervis.svg)](https://pypi.org/project/beziervis/)
8
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/LICENSE)
9
+ [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/quickstart.ipynb)
10
+
11
+ <p align="center">
12
+ <img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/hero.gif" width="480" alt="Animation of de Casteljau's algorithm tracing a degree-4 Bézier curve">
13
+ </p>
14
+
15
+ - **For learning:** animate de Casteljau's algorithm level by level, then save it as a GIF or MP4 or play it inline in Jupyter.
16
+ - **For computing:** evaluate curves of any degree, in any dimension, singly or in batches of thousands. Under the hood this is a Bernstein-matrix NumPy core, with optional Numba kernels for a further 3–40× speed-up.
17
+ - **Small:** the only required dependency is NumPy. Animation and acceleration are opt-in extras.
18
+
19
+ [Quickstart notebook](https://colab.research.google.com/github/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/quickstart.ipynb) · [Gallery](#gallery) · [API](#api) · [CLI](#command-line) · [How it works](#how-de-casteljaus-algorithm-works) · [Performance](#performance) · [Contributing](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/CONTRIBUTING.md) · [Changelog](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/CHANGELOG.md)
20
+
21
+ ## Install
22
+
23
+ ```bash
24
+ pip install beziervis # core: NumPy only
25
+ pip install "beziervis[all]" # + Numba acceleration and matplotlib animations
26
+ ```
27
+
28
+ | extra | adds | for |
29
+ |---|---|---|
30
+ | `[anim]` | matplotlib, Pillow | `animate()` and the `beziervis` command |
31
+ | `[fast]` | Numba | JIT and multi-threaded evaluation, used automatically |
32
+ | `[all]` | both | everything |
33
+
34
+ Requires Python 3.10 or newer. Tested on Linux, macOS and Windows.
35
+
36
+ ## Quickstart
37
+
38
+ ```python
39
+ import beziervis as bv
40
+
41
+ ctrl = [[0, 0], [5, 10], [9, 7], [4, 3]] # 4 control points -> a cubic curve
42
+
43
+ pts = bv.points(ctrl, n=200) # (200, 2) NumPy array, from ctrl[0] to ctrl[-1]
44
+ bv.animate(ctrl, verbose=True).save("curve.gif")
45
+ ```
46
+
47
+ <p align="center">
48
+ <img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/simple.gif" width="320" alt="A cubic Bézier curve being traced">
49
+ </p>
50
+
51
+ - **Input:** control points can be a list or a NumPy array of shape `(k, d)`, in any dimension `d`; the result has shape `(n, d)`.
52
+ - **Displaying:** `.show()` opens a window. In Jupyter, an animation that is the last expression in a cell plays inline.
53
+ - **Saving:** `.save()` writes `.gif` (Pillow) or `.mp4` (needs ffmpeg), and works on servers and in CI without a display.
54
+
55
+ ## Gallery
56
+
57
+ Ten short, self-contained scripts in [`examples/`](https://github.com/venkateshwara-gopisetti/Bezier-Curves/tree/master/examples), each a few dozen lines. Click an image for its code.
58
+
59
+ <table>
60
+ <tr><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/loop_animation.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/loop.gif" alt="A loop, every level drawn"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/loop_animation.py">A loop, every level drawn</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/generative_art.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/generative_art.png" alt="3,000 curves in one call"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/generative_art.py">3,000 curves in one call</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/calligraphy.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/calligraphy.png" alt="Broad-nib calligraphy"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/calligraphy.py">Broad-nib calligraphy</a></sub></td></tr>
61
+ <tr><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/easing.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/easing.png" alt="CSS easing curves"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/easing.py">CSS easing curves</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/curvature_comb.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/curvature_comb.png" alt="Curvature comb"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/curvature_comb.py">Curvature comb</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/helix_3d.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/helix_3d.png" alt="A 3-D spring"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/helix_3d.py">A 3-D spring</a></sub></td></tr>
62
+ <tr><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/heart.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/heart.png" alt="Two mirrored cubics"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/heart.py">Two mirrored cubics</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/arc_length.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/arc_length.png" alt="Even spacing by length"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/arc_length.py">Even spacing by length</a></sub></td><td align="center" width="33%"><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/subdivision.py"><img src="https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/gallery/subdivision.png" alt="Recursive subdivision"></a><br><sub><a href="https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/examples/subdivision.py">Recursive subdivision</a></sub></td></tr>
63
+ </table>
64
+
65
+ ## API
66
+
67
+ | function | returns |
68
+ |---|---|
69
+ | `points(ctrl, n=100, t=None)` | `(n, d)` points at `n` evenly spaced `t` in [0, 1], or at the `t` values you pass |
70
+ | `points_batch(curves, n=100)` | `(m, n, d)` points for `m` curves of equal degree, given as `(m, k, d)` |
71
+ | `casteljau_steps(ctrl, t)` | every level of the construction: `[(k, d), (k-1, d), …, (1, d)]` |
72
+ | `split(ctrl, t=0.5)` | `(left, right)`: two curves of the same degree that join at `t` |
73
+ | `derivative(ctrl)` | control points of the derivative (hodograph) curve |
74
+ | `elevate(ctrl, times=1)` | the same curve with `times` more control points |
75
+ | `arc_length(ctrl)` | the curve's length (Gauss–Legendre quadrature, error around 1e-10) |
76
+ | `animate(ctrl, frames=100, verbose=False, …)` | a `BezierAnimation` with `.save()`, `.show()` and `.to_jshtml()` |
77
+ | `random_control_points(n, seed=None)` | `n` random integer points in [-100, 100) |
78
+ | `set_backend(name)` / `get_backend()` | choose or report the backend: `"numpy"`, `"numba"` or `"auto"` |
79
+
80
+ `animate()` also takes `labels`, `fps`, `hold` (frames to pause on the finished curve), `size` (pixels), `cmap` and `title`. Every function has a docstring with the details; try `help(bv.animate)`.
81
+
82
+ ```python
83
+ import numpy as np
84
+
85
+ left, right = bv.split(ctrl, 0.5)
86
+ velocity = bv.points(bv.derivative(ctrl), n=200) # tangent vectors along the curve
87
+ length = bv.arc_length(ctrl)
88
+ many = bv.points_batch(np.random.rand(10_000, 4, 2), n=100) # (10000, 100, 2)
89
+ ```
90
+
91
+ ## Command line
92
+
93
+ ```bash
94
+ beziervis --points "0,0 5,10 9,7 4,3" --verbose --save curve.gif
95
+ beziervis -n 5 --seed 42 # 5 random control points, reproducible
96
+ python -m beziervis --help # same thing, without the script on PATH
97
+ ```
98
+
99
+ | option | meaning |
100
+ |---|---|
101
+ | `-p, --points "x,y x,y …"` | control points (otherwise random) |
102
+ | `-n N` | number of random control points (default 4) |
103
+ | `--seed S` | random seed |
104
+ | `-v, --verbose` | draw the intermediate construction lines |
105
+ | `-f, --frames F` / `--fps R` | animation steps (default 100) and playback speed (default 30) |
106
+ | `--size PX` / `--no-labels` | figure size in pixels; hide point labels |
107
+ | `-s, --save FILE` | write `.gif` or `.mp4` instead of opening a window |
108
+
109
+ ## How de Casteljau's algorithm works
110
+
111
+ A Bézier curve is defined by its control points (`A0 … A3` below). To find the point at parameter `t`:
112
+
113
+ 1. Walk a fraction `t` along each edge of the control polygon. That gives one fewer point (`B0 … B2`).
114
+ 2. Repeat on the new points (`C0, C1`), then again, until a single point is left.
115
+ 3. That point (red) is on the curve. Sweep `t` from 0 to 1 and it traces the whole curve.
116
+
117
+ ![The same cubic curve at t = 0.2, 0.4, 0.6 and 0.8](https://raw.githubusercontent.com/venkateshwara-gopisetti/Bezier-Curves/master/docs/assets/how-it-works.png)
118
+
119
+ ```python
120
+ steps = bv.casteljau_steps(ctrl, t=0.4) # [A (4×2), B (3×2), C (2×2), point (1×2)]
121
+ ```
122
+
123
+ The construction also splits a curve in two exactly: `A0, B0, C0` and the red point are the control points of the left half. This is how `split()` is implemented.
124
+
125
+ ## Performance
126
+
127
+ Points are evaluated with a precomputed **Bernstein matrix** and one matrix multiply. Above degree 30, where that becomes numerically fragile, a **vectorised de Casteljau** is used instead. When Numba is installed, JIT-compiled kernels (multi-threaded for large inputs) are used automatically. Every backend agrees with the others to within 1e-12.
128
+
129
+ | case | naive Python | NumPy Bernstein | NumPy de Casteljau | Numba serial | Numba parallel |
130
+ |---|---|---|---|---|---|
131
+ | cubic, 1k points | 5.95 ms | 60 µs | 53.6 µs | 22.8 µs | 21.1 µs |
132
+ | cubic, 1M points | – | 71.5 ms | 79.2 ms | 25.9 ms | 10.6 ms |
133
+ | degree 10, 100k points | – | 21.4 ms | 52.4 ms | 7.93 ms | 2.33 ms |
134
+ | degree 50, 100k points | – | – | 1.02 s | 131 ms | 25 ms |
135
+ | 10k cubics × 100 points | – | 36.1 ms | – | – | 4.85 ms |
136
+
137
+ <sub>Best of 5 runs on Windows 11, Python 3.12, NumPy 2.5, Numba 0.68. To reproduce: `python benchmarks/bench.py`.</sub>
138
+
139
+ - **Choosing a backend:** set `BEZIERVIS_BACKEND=numpy|numba|auto` before import, or call `bv.set_backend(...)` at any time.
140
+ - **First Numba call:** it compiles the kernels, which takes about a second the first time and is then cached on disk.
141
+ - **Small inputs:** for a single curve with a few hundred points, every backend takes microseconds, so pick whichever you have installed.
142
+
143
+ ## FAQ
144
+
145
+ **Saving `.mp4` fails.** MP4 needs [ffmpeg](https://ffmpeg.org/download.html) on your `PATH`. GIF needs only Pillow.
146
+
147
+ **The animation is cluttered with many control points.** Use `verbose=False`, which draws only the polygon and the curve, or `labels=False`. Labels switch off automatically above 10 points.
148
+
149
+ **Can I animate 3-D curves?** Not yet; `animate()` draws 2-D curves. The maths functions (`points`, `split`, `derivative`, …) work in any dimension. 3-D animation is on the [roadmap](https://github.com/venkateshwara-gopisetti/Bezier-Curves/issues).
150
+
151
+ **How does this compare with the [`bezier`](https://pypi.org/project/bezier/) package?** `bezier` is a mature computational-geometry library with curve intersection, surfaces and a compiled core, and the right choice for CAD-grade geometry. beziervis is smaller and focused on teaching and visualising the construction, plus fast batch evaluation in pure NumPy or Numba.
152
+
153
+ ## Contributing
154
+
155
+ Bug reports, ideas and pull requests are welcome. See [CONTRIBUTING.md](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/CONTRIBUTING.md) for setup, tests and style. Issues labelled [good first issue](https://github.com/venkateshwara-gopisetti/Bezier-Curves/labels/good%20first%20issue) are a good place to start.
156
+
157
+ ```bash
158
+ git clone https://github.com/venkateshwara-gopisetti/Bezier-Curves && cd Bezier-Curves
159
+ pip install -e ".[dev]" && pytest
160
+ ```
161
+
162
+ ## Citation
163
+
164
+ If beziervis helps your research or teaching, please cite it. GitHub's "Cite this repository" button (from [`CITATION.cff`](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/CITATION.cff)) gives BibTeX and APA.
165
+
166
+ ## License
167
+
168
+ [MIT](https://github.com/venkateshwara-gopisetti/Bezier-Curves/blob/master/LICENSE) © Venkateshwara Gopisetti
@@ -0,0 +1,34 @@
1
+ # Examples
2
+
3
+ Each script is self-contained and writes one image to the current directory:
4
+
5
+ ```bash
6
+ pip install "beziervis[anim]"
7
+ python examples/heart.py # -> heart.png
8
+ ```
9
+
10
+ To regenerate every image in [`docs/assets/gallery/`](../docs/assets/gallery), run `python docs/make_assets.py`. For an interactive tour, open [`quickstart.ipynb`](quickstart.ipynb).
11
+
12
+ | script | shows | uses |
13
+ |---|---|---|
14
+ | [`loop_animation.py`](loop_animation.py) | a degree-7 curve tying a loop, with all seven construction levels | `animate(verbose=True)` |
15
+ | [`generative_art.py`](generative_art.py) | 3,000 swirling cubics evaluated in a single call | `points_batch()` |
16
+ | [`calligraphy.py`](calligraphy.py) | an "S" written with a broad-nib pen along smoothly joined cubic segments | `points()` |
17
+ | [`heart.py`](heart.py) | a heart from two mirrored cubics, as in fonts and vector graphics | `points()` |
18
+ | [`easing.py`](easing.py) | CSS `cubic-bezier()` timing functions, and how to evaluate them | `points()` |
19
+ | [`curvature_comb.py`](curvature_comb.py) | a CAD-style curvature comb | `derivative()` |
20
+ | [`arc_length.py`](arc_length.py) | equal steps in t compared with equal steps in distance | `arc_length()`, `split()` |
21
+ | [`subdivision.py`](subdivision.py) | splitting a curve in half again and again | `split()` |
22
+ | [`degree_elevation.py`](degree_elevation.py) | the same curve with more and more control points | `elevate()` |
23
+ | [`helix_3d.py`](helix_3d.py) | a 3-D spring from quarter-turn cubics, measured against the exact helix | 3-D `points_batch()`, `arc_length()` |
24
+
25
+ <p>
26
+ <img src="../docs/assets/gallery/loop.gif" width="260" alt="Animated degree-7 curve tying a loop">
27
+ <img src="../docs/assets/gallery/generative_art.png" width="260" alt="Thousands of swirling curves">
28
+ <img src="../docs/assets/gallery/calligraphy.png" width="260" alt="Calligraphic letter S">
29
+ <img src="../docs/assets/gallery/heart.png" width="260" alt="Heart from two cubic curves">
30
+ <img src="../docs/assets/gallery/curvature_comb.png" width="260" alt="Curvature comb">
31
+ <img src="../docs/assets/gallery/helix_3d.png" width="260" alt="3-D helix spring">
32
+ </p>
33
+
34
+ Have a nice example? Pull requests are welcome; see [CONTRIBUTING.md](../CONTRIBUTING.md). Each new script needs a `main(out_dir=".")` function that saves its image and returns the path, so the gallery builder and the tests can run it.
@@ -0,0 +1,50 @@
1
+ """Even spacing along a curve: equal steps in t are not equal steps in distance.
2
+
3
+ python examples/arc_length.py # writes arc_length.png
4
+
5
+ Points at evenly spaced t bunch up where the curve moves slowly. To space them
6
+ evenly by distance, measure the length s(t) from the start to each t (the left half
7
+ from split() has exactly that length), then invert s(t) by interpolation.
8
+ """
9
+
10
+ from pathlib import Path
11
+
12
+ import numpy as np
13
+ from matplotlib.figure import Figure
14
+
15
+ import beziervis as bv
16
+
17
+ # Three control points stacked on one spot make the curve crawl past it and race elsewhere.
18
+ CTRL = np.array([[0, 0], [2, 6], [5, 3], [5, 3], [5, 3], [8, 0], [10, 6]], dtype=float)
19
+ N = 21
20
+
21
+
22
+ def even_t(ctrl, n, samples=200):
23
+ """Parameters t at which the curve is divided into n-1 pieces of equal length."""
24
+ grid = np.linspace(0, 1, samples)
25
+ s = np.array([0.0] + [bv.arc_length(bv.split(ctrl, t)[0]) for t in grid[1:]])
26
+ return np.interp(np.linspace(0, s[-1], n), s, grid)
27
+
28
+
29
+ def main(out_dir=".") -> Path:
30
+ """Compare evenly spaced t with evenly spaced distance; save arc_length.png."""
31
+ curve = bv.points(CTRL, n=300)
32
+ fig = Figure(figsize=(10, 3.6), dpi=100, facecolor="white")
33
+ axes = fig.subplots(1, 2)
34
+ panels = [("equal steps in t", np.linspace(0, 1, N)),
35
+ (f"equal steps in length ({bv.arc_length(CTRL):.2f} in total)", even_t(CTRL, N))]
36
+ for ax, (title, t) in zip(axes, panels):
37
+ ax.plot(*CTRL.T, "--", color="0.8", lw=1)
38
+ ax.plot(*curve.T, color="black", lw=2)
39
+ ax.plot(*bv.points(CTRL, t=t).T, "o", color="#e0245e", ms=6, zorder=3)
40
+ ax.set_title(title, fontsize=12)
41
+ ax.set_aspect("equal")
42
+ ax.axis("off")
43
+ fig.tight_layout()
44
+ out = Path(out_dir) / "arc_length.png"
45
+ fig.savefig(out)
46
+ return out
47
+
48
+
49
+ if __name__ == "__main__":
50
+ print(main())