bezierkit: Bézier curve toolkit for Python
A small, renderer-independent Python toolkit for Bézier curves.
from bezierkit import BezierCurve, Point
from bezierkit.sampling import UniformSampler
curve = BezierCurve.cubic(
Point(0, 0),
Point(1, 2),
Point(3, 2),
Point(4, 0),
)
# Point(coords=(2.0, 1.5))
print(curve.at(0.5))
# cut the curve in two
left, right = curve.split(0.3)
sample = UniformSampler(200).sample(curve)
print(len(sample.points), "sample points")
Features
-
Curves of any degree
BezierCurve,CubicBezierSegmentandPiecewiseBezier, with immutablePoint,VectorandPointSetvalue objects. -
Evaluation and subdivision
Evaluate with de Casteljau's algorithm, then differentiate, split, restrict and reverse a curve.
-
Construction from slopes
Build a cubic from end points and end slopes with
PlanarSlopes, or from Hermite data. -
Fitting and level sets
Adaptive fitting of a function graph with a measured error, polyline simplification, and tracing of implicit level sets.
-
Native export
A versioned JSON format, SVG cubic path data and TikZ
controlscommands, never flattened into line pieces. -
Optional extras
A Matplotlib path adapter and a command line interface.
Mathematics and proofs
The guides state the properties the algorithms rely on as theorems: what the Bernstein basis guarantees, why de Casteljau's algorithm evaluates and subdivides a curve, how far a Hermite interpolant can stray, and what the exporters lose to rounding. Every proof is folded under its theorem, so the pages read as API documentation until you open one. The standard references are Farin (2002) and Prautzsch et al. (2002); see References.
Notation
A point lives in \(\mathbb{R}^d\) for some \(d \ge 1\); most figures use \(d = 2\). A Bézier curve of degree \(n\) has \(n + 1\) control points \(P_0, \dots, P_n\) and is the map
where \(b_{i,n}(t) = \binom{n}{i} t^i (1-t)^{n-i}\) are the Bernstein polynomials. Every curve in the package is
parameterized over \([0, 1]\); a parameter outside it raises ParameterOutOfDomain.
Reading guide
| Topic | Page |
|---|---|
| Points, vectors, parameters, errors | Geometry values |
| Bernstein basis, curves, evaluation | Bézier curves |
| Derivatives, subdivision, reversal | Derivatives and subdivision |
| Cubic segments and piecewise paths | Cubic segments and paths |
| Constructions and Hermite interpolation | Constructions |
| Fitting functions and polylines | Fitting |
| Tracing level sets | Level sets |
| Sampling, JSON, SVG, TikZ, Matplotlib | Sampling and export |
| Command line | Command line |
On first use, read the Quick start and Bézier curves.
Scope
Rendering style and diagram semantics are intentionally outside this package: it draws nothing. Intersections, B-splines and NURBS are future work.
Install
Requires Python 3.10 or later. See Installation for extras and the development setup, or go straight to the Quick start.
Documentation
The following chapters cover bezierkit 1.1.0.
- Introduction
- Installation
- Quick start
- Geometry values
- Bézier curves
- Derivatives, reversal and subdivision
- Cubic segments and paths
- Constructions and Hermite interpolation
- Fitting
- Level sets
- Sampling and export
- Command-line interface
- Proofs
- Approximation proofs
- Change history
Examples
The scripts that draw the figures in the manual and on these pages come as one zip, sorted by topic.