Pangram verdict · v3.3
We believe that this entire text is human-written.
AI likelihood · overall
HumanArticle text · 1,624 words · 1 segments analyzed
Variable-width Bézier Curves © 2025-12-30 Luther Tychonievich How to draw them with current 2D graphics APIs, needed to supporting board work tablet tools. In 2013 I began teaching university classes, and quickly realized that I wanted to be able to do board work – where I write and draw by hand live in front of the class – but that my rooms did not have large enough boards to be clearly seen by the number of students they contained. So I purchased a graphics tablet11 Initially a Wacom Intuos 4, though I’ve purchased several others since then and tried various tools that were extant at that time. I was dissatisfied with them all so the summer of 2014 I wrote my own boardwork application, and have kept tweaking and re-writing so that I now have eight completed: three in JavaScript22 Two used an old Firefox plugin to get pressure data, one backed by SVG and the other by Canvas; both stopped working when Firefox updated their plugin system. The other uses HTML5’s PointerEvent API and is backed by Canvas. and five in D33 Two using GTK3 and Cairo, one using GTK4 and GSK, two using software renderers of my own creation.. Throughout this development experience I’ve been continually frustrated by the lack of an important primitive in 2D graphics libraries: Bézier curves with a pen width coordinate. This post is devoted to that topic. Pressure There are many ways that a pressure input can be used by a 2D drawing program. Art programs often do quite complicated things with pressure, using it to vary opacity and scale and to pick a pen profile from a pressure-and-distance-varying sample. That’s too much for a simple board-work application: it’s too computationally complex, results in too-large files, and can be distracting for those close enough to see it and difficult for those farther back in the room to make out. Most programs suited to board work use pressure to indicate the width of a stroke with clear edges. This allows much more fluid writing and drawing than a fixed-width stroke system while also being visually crisp, computationally simple, and highly compressible for later file export. There are three models for pressure-based stroke width that I’ve seen: fixed-axis linear offset, stroke-aligned linear offset, and circular offset. Fixed-axis linear offset simulates how western calligraphy works when using a flexible nib. It can look fancy and appealing with a skilled scribe, but lacks the desired legibility for board work. Stroke-aligned linear offsets fill a line perpendicular to stroke direction at each point along the stroke, the width of which is dependent on stroke pressure. This technique is computationally simple and in theory can create an appealing filled stroke shape, but it’s very sensitive to the noise and jitter that come with most hand motions and digital stylus discretization because even small offsets of the pen can create arbitrary changes in the stroke direction, with a large visual impact on the resulting stroke. To counteract this jitter, applications often add a smoothing effect, sometimes marketed as the mass or inertia of the virtual pen; such smoothing can make slow, careful strokes render nicely but removes high-frequency details and makes rapid writing look garbled. Circular offsets fill a circle at each point along the stroke, the radius of which is dependent on stroke pressure. This technique is robust against jitter, rendering attractive strokes even at high frequencies, but may be computationally more expensive than linear offsets. My board work tools all use circular offsets; circular offsets are the subject of the rest of this post. Line or Spline Pen data is delivered by the tablet through the operating system device drivers as a sequence of (x,y,p)(x, y, p) triples, where pp is a device-defined pressure input. I generally map p↦rp \mapsto r using some weighting function based both on desired stroke width and how quickly I want the stroke radius to react to pressure; I find that something between r=wp1.5r = w p^{1.5} and r=wp2r = w p^{2} works well for me, depending on the specific tablet I’m using, where ww is a stroke width multiplier I connect to a keyboard shortcut and adjust while the application is running. My first application created a pill shape between each pair of points: a circle at each point, both the same radius, connected by a rectangle the same width as the circles. This is legible and simple to implement, but has bumpy strokes when pressure varies and doesn’t zoom in very well. My second application created a trapezoidal pill shape between each pair of points: a circle at each point with different radii connected by a trapezoid tangent to them both. This looks much smoother than the pill approach, at the expense of slightly more computation. Both of those approaches connect sample points with straight strokes. But when I am writing quickly I might only have half a dozen total (x,y,r)(x,y,r) triples for the entire circle of an o, and not equally spaced because my pen accelerates and decelerates as I write, which can make straight lines look noticeably angular. With a high-end graphics tablet and a close-to-native driver I get more samples per second and this effect is reduced, but with cheap tablets or an event-folding API like JavaScript the angularity is visually significant. Enter the world of splines. Instead of connecting tablet-provided points with straight lines (with variable-width stroking) I connect them with smooth curves; in particular, cubic Bézier curves. There are many ways to pick the curves based on the sample points, but I am satisfied with the visual quality of picking the slope of the curve at point ii based on the angle between points i−1i-1 and i+1i+1, delaying the drawing by one sample to allow point i+1i+1 to be input before drawing the curve between i−1i-1 and ii. Bézier curves are the workhorse of curve drawing on computers. They nicely mix computational efficiency, artist intuition, and numerical stability. Bézier curves are a specific instance of Bernstein basis polynomials; I’ll not further distinguish the two here, referring to them as Bézier even in contexts where Bernstein might be more appropriate. Each Bézier curve is specified with a sequence of control points. The curve touches the first and last control points and travels towards those in between in order while being smooth and not exiting the convex hull of the control points. The more control points there are, the less influence each one exerts over the curve, with influence being more limited for those in the middle then those near the ends. In the vast majority of cases, we use four control points, yielding a cubic Bézier curve44 So called because the curve can be expressed as a cubic polynomial.. An important algorithm for Bézier curves is de Casteljau’s algorithm which can split a single Bézier curve into two Bézier curves at any point along the original. Given a 0<t<10 < t < 1, the first and last point are part of the front and back split curves, respectively; then a new point is found tt along each line segment between adjacent points; the first and last of those new points are part of the front and back split curves, respectively; then a new point is found tt along each line segment between adjacent points; and so on until there’s just one new point, which is part of both split curves. de Casteljau’s Algorithm Input Control points 𝐜0,𝐜1,…,𝐜n𝐜_0, 𝐜_1, …, 𝐜_n Parameter value t∈[0,1]t \in [0, 1] Output Control points 𝐚0,𝐚1,…,𝐚n𝐚_0, 𝐚_1, …, 𝐚_n Control points 𝐛0,𝐛1,…,𝐛n𝐛_0, 𝐛_1, …, 𝐛_n These split the curve at tt: 𝐚0=𝐜0𝐚_0 = 𝐜_0, 𝐛n=𝐜n𝐛_n = 𝐜_n, and 𝐚n=𝐛0=𝐚_n = 𝐛_0 = the point on the curve at parameter tt. Define s=1−ts = 1-t Let 𝐝i,j={s𝐝i−1,j+t𝐝i−1,j+1if 0<i≤n and 0≤j≤n−i𝐜jif i=0 and 0≤j≤n𝐝_{i,j} = \begin{cases}s 𝐝_{i-1,j} + t 𝐝_{i-1,j+1} & \text{if } 0 < i \le n \text{ and } 0 \le j \le n-i\\ 𝐜_j&\text{if }i=0\text{ and }0 \le j \le n\end{cases} Let 𝐚i=𝐝i,0𝐚_i = 𝐝_{i,0} Let 𝐛i=𝐝n−i,i𝐛_i = 𝐝_{n-i,i} If a single value on the curve is wanted, de Casteljau’s algorithm can provide it, but uses n(n+1)n(n+1) multiplications to do so. For curves with many control points, Horner’s algorithm uses 5n−75n - 7 multiplications and n−2n-2 divisions to do so, making it more efficient for curves with more than three control points. Horner’s Algorithm Horner’s algorithm operates in power basis, not Bernstein basis, so we compute Binomial coefficients to convert between those basis during the Horner’s algorithm loop. Input Control points 𝐜0,𝐜1,…,𝐜n𝐜_0, 𝐜_1, …, 𝐜_n Parameter value t∈[0,1]t \in [0, 1] Output The point 𝐩𝐩 on the curve at parameter tt Define s=1−ts = 1-t Initialize b=(n0)=1b = \binom{n}{0} = 1 Initialize k=t0=1k = t^0 = 1 Initialize 𝐩=𝐜0𝐩 = 𝐜_0 Repeat for ii from 11 to nn: Multiply kk by tt (makes k=tik = t^i) Multiply bb by (n−i+1)(n-i+1) and divide it by ii (makes b=(ni)b = \binom{n}{i}) Replace 𝐩𝐩 with 𝐩s+kb𝐜i𝐩 s + k b 𝐜_i An important property of Bézier curves is that their derivative (the rate at which their coordinates change) is also a Bézier curve, called the curve’s hodograph. The hodograph has one fewer control points than the original curve; if the control points of the original are labeled 𝐂i𝐂_i for ii from 00 to nn, then the control points of the hodograph are 𝐇i=nΔt(𝐂i+1−𝐂i)𝐇_i = \frac{n}{\Delta t}(𝐂_{i+1} - 𝐂_i) for ii from 00 to n−1n-1; Δt\Delta t is the total parameter range of the curve (often taken to be 1). Bézier curves are separable, in the sense that working with a Bézier curve with (x,y,r)(x,y,r) control points yields exactly the same results as working with three Bézier polynomials, one with xx control points, one with yy, and one with zz.