This post was inspired mostly by the arrival of the new Julia package Literate.jl by Fredrik Ekre. Literate lets you write a Julia source file, a Markdown blog post, and a Jupyter notebook all at the same time. It’s magic, or, at least, indistinguishable from magic… As a result, you might be able to find a Jupyter notebook version of this Markdown-converted Julia source file somewhere nearby (look in the github repo for this blog, perhaps). The code uses the following packages, Literate, Luxor, Colors, Roots, Fontconfig, DataFrames, Iterators, ColorSchemes, and should work in Julia v0.6 (some packages such as Colors have yet to be updated to work with version 0.7).
Luxor provides some support for Bézier curves, but there’s no room for any more documentation—it’s already too big. So the intention of this post is to provide some of the missing information about what’s up with Bézier curves and how you might use them. And I confess in advance to wasting some of your bandwidth with some pointlessly colourful graphics.
There’s not a lot of mathematical material here, but fortunately the internet is awash with high-quality information about Bézier curves. The two articles you should definitely read instead of this post, or at least before, are:
The story is quite well known: two engineers employed in the French car industry, Paul de Faget de Casteljau, at Citroen, and Pierre Etienne Bézier, at Renault, worked—mostly independently—on the mathematics of curves in the early 1960s, as the industry made its first tentative steps towards using computers for design and production.
(This is the Citroen DS, which was once voted the most beautiful car ever made, apparently. Image from the WikiMedia Commons photographed by Klugschnacker)
De Casteljau and Bézier were interested in mathematical tools that would allow designers to intuitively construct and manipulate complex shapes. This problem was especially critical for “free–form” shapes that couldn’t easily be specified by centre points, axes, angles, and dimensions. The motivation was also partly to replace the laborious, variable, and expensive process of sculpting clay models to specify the desired shape.
De Casteljau found some resistance when his mathematical researches were introduced into the design studio. He observed that:
the designers were astonished and scandalized. Was it some kind of joke? It was considered nonsense to represent a car body mathematically. It was enough to please the eye, the word ‘accuracy’ had no meaning. [quoted in Farouki]
Bézier popularized, but did not actually create, what we know today as the Bézier curve. He mainly developed the notation, and devised the idea of nodes with attached “control handles”, which the designers could use to adjust the shapes as easily as they used the turn indicators on their beloved Citroen DSs.
De Casteljau is also remembered for the algorithm that bears his name.
The shortest distance?
With the lingering thought of old Renaults and Citroens in mind, it’s tempting to think of a Bézier curve as a line that takes, not the shortest distance between two points, but takes instead the scenic route.
Let’s define four points:
The first and last points, P1 and P2, are the start and end of the line. The second point, CP1, controls the direction of the line as it leaves the first point, and the third point, CP2, determines the way the line approaches the fourth point. PostScript guru Don Lancaster (see footnote) uses the terms ‘influence point’ and ‘enthusiasm’—so the second influence point determines the enthusiasm with which the curve travels towards its final destination.
The graphics primitive curve() function draws a Bézier curve between P1 and P2, taking into account the positions of control points CP1 and CP2. It takes just three points, using the current position as the starting point.
The control points are like handles, controlling the shape of the curve. If you’ve used Adobe Illustrator or some other vector graphics software, you’ll be familiar with the idea of interactively dragging the handles around to get interesting curves. In Luxor, though, we sacrifice interactivity in favour of ruthless machine-driven automation. In the following animation, the control handles explore the geometry of a couple of hypotrochoids, while the helpless Bézier curve is pinned between them and forced into sinuous contortions:
Can I make this in Adobe Illustrator? Hold my beer…
While you’re waiting, have a look at another animation; this is my artist’s impression of the De Casteljau algorithm dividing the control polygons around a Bézier curve as the parameter n moves from 0 to 1. The idea is that as p1 divides A to A1, p2 divides A1 to B1 and p3 divides B1 to B. So, pp1 divides p1 to p2, and pp2 divides p2 to p3. And you keep doing this until you can’t divide any more, and eventually the point P plots the course of the final Bézier curve. The red and blue parts of the curve show that this technique is also a good way to split a single Bézier curve into two separate ones, and the red and blue parts are separate control polygons.
In Luxor, a BezierPathSegment type contains four 2D points, stored in the fields p1, cp1, cp2, and p2, and a BezierPath is an array of one or more of these BezierPathSegments. The drawbezierpath() function draws a BezierPath or BezierPathSegment, with similar results to the curve() function:
This last function, drawbezierpath() is a typical Luxor drawing function, in that you can provide :fill as an alternative action to :stroke.
An easy way to make a BezierPath is to use makebezierpath() and supply a polygon. For example, let’s make a triangle with ngon() and use it as the skeleton for a new Bézier path:
Here makebezierpath() converted the three points into an array of three separate Bézier path segments. The control points are positioned so that the curve flows freely from one segment to the segment.
Bézier curves can have straight bits too. This animation shows the control points moving towards the points they’re controlling. When they merge, the Bézier path appears to become a series of straight lines:
This is a process known to very young Adobe Illustrator users as ‘putting the Bézier handles to bed’.
It’s also fun to move the control points somewhere else. Here, they’re multiplied by 2, while the first and last points are left unchanged:
We could make even more copies, multiplying the control points each time through:
Try changing the initial triangle to a pentagon or heptagon by changing the 3 in ngon(). And try multiplying by less than 1.05 too…
Should you ever want to draw Bézier curves “the hard way”, try this. The standard Bézier function is available in Luxor as bezier(), and we could draw a small hue-varying circle at each point:
What happens if you step u from say, -25.0 to 25.0? And is that supposed to happen? (Spoiler: the curve doubles back and shoots off to (±∞/∞).)
Out of control
Suppose you wanted to draw a Bézier curve but you didn’t know where the control points were, but you did know a couple of points that should lie on the line? Again, Luxor has the answer for you, in the form of a function called bezierfrompoints(). You supply four points, and the function returns the points of the Bézier curve that passes through them.
Note that this function returns all four points, but of course you already knew the first and last ones (in corners), you just wanted the two control points.
On the right path?
In Luxor the three main ways to make graphic lines and shapes are: paths, polygons, and BezierPaths. Paths are the fundamental building blocks of graphics, consisting of one or more sequences of straight and Bézier curves. A polygon is an array of points, which will be converted to a path when you draw it. And BezierPaths consist of a list of BezierPathSegments, which will also be converted to ordinary paths when they’re drawn.
It’s useful to be able to convert between the different types:
polygon to BezierPath
current path to array of polygons
current path to array of BezierPaths
BezierPathSegment to polygon
BezierPath to polygon
bezierfrompoints(p1, p2, p3, p4)
convert four points to BezierPathSegment
Can’t draw a circle?
Having witnessed the ability of a simple Bézier curve to adopt so many shapes, it’s a bit surprising to find out that you can’t use a Bézier curve to draw a circle. Well, it can do a very good impression of one, of course, but mathematically it can’t produce a purely circular curve. You can see this if you draw a very large circle and a very large matching Bézier segment. For a circle with radius 10 meters (and that’s one big PDF), the discrepancy between the pure circle and the Bézier approximation isn’t much bigger than the size of this period/full stop.←
We non-scientists are lucky in not having to worry about errors of this magnitude…
In the above picture, the red circle is made by circle(), the green one is made with Bézier curves (as used by circlepath() and ellipse()).
“0.55228474983 is the magic number”
To draw approximate circles using Bézier curves, you need to know the magic number, usually called kappa, which has the value 0.552284…. We’re looking at a Bézier curve pretending to be a circular quadrant, with control points positioned at a certain distance kappa from the end points. But how far? What value of kappa—what length of handle—will give us the most circular curve?
A picture of the problem, using a deliberately not-very-good guess at a value for kappa of 0.5:
To solve this, we’ll define a function that takes a value for kappa and works out the radius at the center of the Bézier curve.
And we’ll use one of the many excellent packages in JuliaMath, Roots.jl, to find the zero point:
So that’s kappa.
Alternatively, we can use algebraic sorcery and the parametric equation for the Bézier cubic function to conjure the value from the following incantations:
I suspect most applications simply hard-code the magic number 0.552284… directly.
The following code shows how we could animate the process of changing the length of the handles by changing the value of kappa. Blink and you’ll miss the sweet spot, though, and the movement of the handles is imperceptible, so perhaps this isn’t the best way of illustrating the construction.
Luxor already provides a Luxor.circlepath() function that uses four Bézier curve segments to build a path that draws a circle. The main advantage of using this instead of the default arc-based circle is that it’s easier to build circles with holes:
Detour into Typomania
In practice, not being able to draw perfect circles with Bezier curves isn’t a big problem. Font designers (you knew this was going to go ‘all typographical’ sooner or later) are big users of Bézier curves, and typically they don’t like drawing perfect circles anyway, because of the optical corrections required to make things ‘look right’.
There are a number of optical illusions that demonstrate that the human eye and the brain don’t always see reality accurately. The following is one of the simplest, but most people would be prepared to bet that the horizontal bar is thicker and shorter than the vertical bar. I had to draw a grid to double-check…
It’s something to do with how our eyes, set side by side and trained to move side to side horizontally with great speed and precision, underestimate width. Type designers spend much of their time adjusting the relative widths and thicknesses of letter shapes so that illusions like this are compensated for in advance. What happens if you turn your display on its side (apart from possibly spilling your coffee)?
Here’s a short script to examine the bounding boxes of the inner and outer loops of the letter ‘o’ in various fonts.
The fonts on your system will be different, of course.
The story of “o”
I wondered which fonts used the most circular circles for the letter “o”. This script wanders through all the fonts registered with Fontconfig and stores the bounding boxes of the letter “o”, then finds “the most circular”.
To analyse the results, we’ll put everything into a DataFrame.
Few typefaces are perfectly circular. Even Circular, LineTo’s trendy geometric sans typeface, isn’t perfectly circular.
Most of the least circular ones, according to this rough examination, are in the Condensed and Compressed sections of the font libraries. No surprise, Sherlock.
The most circular ones on my computer are the classic geometric sans serif fonts.
In the ‘most circular o’ fonts, there are a few examples from Rudolf Koch’s Kabel family.
Kabel belongs to the “geometric” style of sans-serifs, which was becoming popular in Germany at the time of Kabel’s creation [1920s]. Based loosely on the structure of the circle and straight lines, it nonetheless applies a number of unusual design decisions, such as a delicately low x-height (although larger in the bold weight), a quirky tilted ‘e’ and irregularly angled terminals, to add delicacy and an irregularity suggesting stylish calligraphy, of which Koch was an expert.
Eye magazine isn’t convinced by the apparent geometrical precision of Kabel:
its eccentricities reveal Koch’s unwavering expressionistic and humanist instincts
But at least the ‘o’s are circular…
Moving back to Bézier curves, you can summarize the behaviour of a Bézier curve at a point by finding the curvature. The Luxor function beziercurvature() returns a number, called kappa, that varies and flips from positive to negative as the Bézier path varies in ‘curviness’. We’re using the first and second derivatives to find the kappa value:
The following drawcurvature() function uses the kappa value to work out the slope, and draw a perpendicular to the curve, with lengths varying according to the value of kappa, indicating the way the curvature changes.
(This is another kappa, by the way, no relation to the Bézier circularity kappa. Or indeed to the Lancia Kappa…
…or to many of the other things called kappa, such as the curvature of the universe, the torsional constant of an oscillator, Einstein’s constant of gravitation, the coupling coefficient in magnetostatics—and that’s just in physics.)
Here it is in action:
Users of CAD systems (particularly industrial designers) like to display these curvature combs (in both 2D and 3D) to make sure that their shapes don’t introduce noticeably abrupt (and possible weak) transitions. Type designers use them too, but as usual they like to let their eyes have the final say.
The dots over the ‘i’ and ‘j’ don’t look like perfect circles; these are defined by 8 points, not 4, for some reason.
Osculate my Béziers
The value of kappa is typically very small, so the radius of curvature, which is defined as , can become very large. This radius value defines a circle that just touches the curve and follows the curvature at that point. Mathematicians, in typically romantic mood, call it the osculating circle, osculate being from the Latin noun osculum, meaning “kiss”.
Drawing osculating circles can be a challenge; they grow very large when the curve looks flat. To be honest it’s a tricky diagram to style up, and there’s a lot of information that it would be cool to keep and a shame to throw away. There’s quite a bit of osculation going on here…
A blot on the landscape
Bézier curves became popular because they allow us to specify gently curving shapes that would be impractical and cumbersome to specify with circular arcs. The Comprehensive Taxonomy of Irregular Amoeboid Shapes is perhaps still waiting to be written, but here’s my contribution to the ‘random ink blot’ chapter. Sometimes you’ll get lucky and get a nice one.
The control handles of alternate points are positioned on a line from the center to the point.
You could analyse the curvature of these blobs, if you really wanted to, using the drawcurvature() function from earlier:
A brush in the rough
Not all lines generated by computers have to be rigidly straight and precise. What if we could easily make graphics that are a bit more relaxed in style, rather than the rigid CAD-like (and yes, awesome) precision graphics we’re used to?
“A line is a breadthless length.” (Euclid)
The idea here is that a single line between two points is replaced with some BezierPathSegments that together define a shape that can vary in thickness along its length. This shape can be then filled, and is independent of the set line thickness.
The experimental brush() function is bristling with built-in randomness, so you never know what you’re going to get. There are some control knobs available which you can play with.
To be honest, I think it’s a bit daft to abandon the machine-like precision that our graphics software usually gives us for this variable hand-made look.
Today, our relationship with mechanical production and product design is inconsistent; some of the things we desire we want to be hand-made, but others we’d prefer to be machine-made. Only the most expensive cars claim to be made ‘by hand’; the cheaper models flaunt their nanometre precision instead. Hipsters seek the authentic analogue roughness of the products of the Second Industrial age, but are secretly grateful for and rely on the smooth precisions afforded by the Third. Handmade shoes yes, handmode iPhones, no.
There are a number of plotting packages that offer a hand-drawn aesthetic—this site is web-based. So you can definitely announce your latest scientific discovery using XKCD-style presentation graphics. There are XKCD-styling kits for most of the software used by people who have heard of XKCD.
Unfortunately you’ve got the imprecise finish without the reassuring and lovable hand-made quirks. Like those imitation hand-writing fonts, it’s presenting the illusion of manual labour.
A fist full of brushes
But it’s always fun to explore an idea to see where it leads:
The line quality can make for simple painterly graphics, good for the occasional Bob Ross painting:
Each time you evaluate this the result is slightly different, yet always the same. Perhaps the next one will be better — wait, no, perhaps I preferred the previous one…
And because we started in France, let’s paint some graffiti:
…and then speed off in our curvaceous old Citroen DS and drive to the France/Swiss border, where CERN are busy recreating the big bang:
Don Lancaster is the totally awesome dude who was active in the very early days of personal computing, and probably knows more about PostScript than most of the current Adobe Systems employees put together. Travel back in time by visiting his wacky website at http://www.tinaja.com.
All the images in this post are in PNG, but they looked better in the vector-based SVG format. However, there’s an annoying ‘bug’ in Jupyter/IJulia/IPython involving text in SVG images created by Cairo. What happens is that Cairo tries to be smart and stores text in XML symbols, suitable for re-use. A good idea, but unfortunately they’re stored in the notebook’s ‘global XML scope’, and so later cells accidentally pick up symbol definitions from earlier cells and re-use them, even though that’s not always what you want. A solution would be to somehow encapsulate the SVG image in a cell to prevent the definitions leaking. I don’t know how to do that yet, but there’s an open issue if you can help me find a workround…