## Simplicity and the Artist

Some of my favorite of Pablo Picasso’s works are his line drawings. He did a number of them about animals: an owl, a camel, a butterfly, etc. This piece called “Dog” is on my wall:

These paintings are extremely simple but somehow strike the viewer as deeply profound. They give the impression of being quite simple to design and draw. A single stroke of the hand and a scribbled signature, but what a masterpiece! It simultaneously feels like a hasty afterthought and a carefully tuned overture to a symphony of elegance. In fact, we know that Picasso’s process was deep. For example, in 1945-1946, Picasso made a series of eleven drawings (lithographs, actually) showing the progression of his rendition of a bull. The first few are more or less lifelike, but as the series progresses we see the bull boiled down to its essence, the final painting requiring a mere ten lines. Along the way we see drawings of a bull that resemble some of Picasso’s other works (number 9 reminding me of the sculpture at Daley Center Plaza in Chicago). Read more about the series of lithographs here.

Now I don’t pretend to be a qualified artist (I couldn’t draw a bull to save my life), but I can recognize the mathematical aspects of his paintings, and I can write a damn fine program. There is one obvious way to consider Picasso-style line drawings as a mathematical object, and it is essentially the Bezier curve. Let’s study the theory behind Bezier curves, and then write a program to draw them. The mathematics involved requires no background knowledge beyond basic algebra with polynomials, and we’ll do our best to keep the discussion low-tech. Then we’ll explore a very simple algorithm for drawing Bezier curves, implement it in Javascript, and recreate one of Picasso’s line drawings as a sequence of Bezier curves.

## The Bezier Curve and Parameterizations

When asked to conjure a “curve” most people (perhaps plagued by their elementary mathematics education) will either convulse in fear or draw part of the graph of a polynomial. While these are fine and dandy curves, they only represent a small fraction of the world of curves. We are particularly interested in curves which are *not* part of the graphs of any functions.

For instance, a French curve is a physical template used in (manual) sketching to aid the hand in drawing smooth curves. Tracing the edges of any part of these curves will usually give you something that is *not* the graph of a function. It’s obvious that we need to generalize our idea of what a curve is a bit. The problem is that many fields of mathematics define a curve to mean different things. The curves we’ll be looking at, called Bezier curves, are a special case of *single-parameter polynomial* *plane curves. *This sounds like a mouthful, but what it means is that the entire curve can be evaluated with two polynomials: one for the values and one for the values. Both polynomials share the same variable, which we’ll call , and is evaluated at real numbers.

An example should make this clear. Let’s pick two simple polynomials in , say and . If we want to find points on this curve, we can just choose values of and plug them into both equations. For instance, plugging in gives the point on our curve. Plotting all such values gives a curve that is definitely not the graph of a function:

But it’s clear that we *can* write any single-variable function in this parametric form: just choose and . So these are really more general objects than regular old functions (although we’ll only be working with polynomials in this post).

Quickly recapping, a single-parameter polynomial plane curve is *defined* as a pair of polynomials in the same variable . Sometimes, if we want to express the whole gadget in one piece, we can take the coefficients of common powers of and write them as *vectors* in the and parts. Using the example above, we can rewrite it as

Here the coefficients are points (which are the same as vectors) in the plane, and we represent the function in boldface to emphasize that the output is a point. The linear-algebraist might recognize that pairs of polynomials form a vector space, and further combine them as . But for us, thinking of points as coefficients of a single polynomial is actually better.

We will also restrict our attention to single-parameter polynomial plane curves for which the variable is allowed to range from zero to one. This might seem like an awkward restriction, but in fact every finite single-parameter polynomial plane curve can be written this way (we won’t bother too much with the details of how this is done). For the purpose of brevity, we will henceforth call a “single-parameter polynomial plane curve where ranges from zero to one” simply a “curve.”

Now there are some very nice things we can do with curves. For instance, given any two points in the plane we can describe the straight line between them as a curve: . Indeed, at the value is exactly , at it’s exactly , and the equation is a linear polynomial in . Moreover (without getting too much into the calculus details), the line travels at “unit speed” from to . In other words, we can think of as describing the motion of a particle from to over time, and at time the particle is a quarter of the way there, at time it’s halfway, etc. (An example of a straight line which doesn’t have unit speed is, e.g. .)

More generally, let’s add a third point . We can describe a path which goes from to , and is “guided” by in the middle. This idea of a “guiding” point is a bit abstract, but computationally no more difficult. Instead of travelling from one point to another at constant speed, we want to travel from one *line* to another at constant speed. That is, call the two curves describing lines from and , respectively. Then the curve “guided” by can be written as a curve

Multiplying this all out gives the formula

We can interpret this again in terms of a particle moving. At the beginning of our curve the value of is small, and so we’re sticking quite close to the line As time goes on the point moves along the line between the points and , which are themselves moving. This traces out a curve which looks like this

This screenshot was taken from a wonderful demo by data visualization consultant Jason Davies. It expresses the mathematical idea quite superbly, and one can drag the three points around to see how it changes the resulting curve. One should play with it for at least five minutes.

The entire idea of a Bezier curve is a generalization of this principle: given a list of points in the plane, we want to describe a curve which travels from the first point to the last, and is “guided” in between by the remaining points. A Bezier curve is a realization of such a curve (a single-parameter polynomial plane curve) which is the inductive continuation of what we described above: we travel at unit speed from a Bezier curve defined by the first points in the list to the curve defined by the last points. The base case is the straight-line segment (or the single point, if you wish). Formally,

**Definition: **Given a list of points in the plane we define the *degree * *Bezier curve *recursively as

We call the *control* *points* of .

While the concept of travelling at unit speed between two lower-order Bezier curves is the real heart of the matter (and allows us true *computational* insight), one can multiply all of this out (using the formula for binomial coefficients) and get an explicit formula. It is:

And for example, a cubic Bezier curve with control points would have equation

Higher dimensional Bezier curves can be quite complicated to picture geometrically. For instance, the following is a fifth-degree Bezier curve (with six control points).

The additional line segments drawn show the recursive nature of the curve. The simplest are the green points, which travel from control point to control point. Then the blue points travel on the line segments between green points, the pink travel along the line segments between blue, the orange between pink, and finally the red point travels along the line segment between the orange points.

Without the recursive structure of the problem (just seeing the curve) it would be a wonder how one could actually compute with these things. But as we’ll see, the algorithm for drawing a Bezier curve is very natural.

## Bezier Curves as Data, and de Casteljau’s Algorithm

We will now derive and implement the algorithm for painting a Bezier curve to a screen using only the ability to draw straight lines. For simplicity, we’ll restrict our attention to degree-three (cubic) Bezier curves. Indeed, every Bezier curve can be written as a combination of cubic curves via the recursive definition, and in practice cubic curves balance computational efficiency and expressiveness. All of the code we present in this post will be in Javascript, and is available on this blog’s Github page.

So then a cubic Bezier curve is represented in a program by a list of four points. For example,

var curve = [[1,2], [5,5], [4,0], [9,3]];

Most graphics libraries (including the HTML5 canvas standard) provide a drawing primitive that can output Bezier curves given a list of four points. But suppose we aren’t given such a function. Suppose that we only have the ability to draw straight lines. How would one go about drawing an approximation to a Bezier curve? If such an algorithm exists (it does, and we’re about to see it) then we could make the approximation so fine that it is visually indistinguishable from a true Bezier curve.

The key property of Bezier curves that allows us to come up with such an algorithm is the following:

Any cubic Bezier curve can be split into two, end to end,

which together trace out the same curve as .

Let see exactly how this is done. Let be a cubic Bezier curve with control points , and let’s say we want to split it exactly in half. We notice that the formula for the curve when we plug in , which is

Moreover, our recursive definition gave us a way to evaluate the point in terms of smaller-degree curves. But when these are evaluated at 1/2 their formulae are similarly easy to write down. The picture looks like this:

The green points are the degree one curves, the pink points are the degree two curves, and the blue point is the cubic curve. We notice that, since each of the curves are evaluated at , each of these points can be described as the midpoints of points we already know. So , etc.

In fact, the splitting of the two curves we want is precisely given by these points. That is, the “left” half of the curve is given by the curve with control points , while the “right” half has control points .

How can we be completely sure these are the same Bezier curves? Well, they’re just polynomials. We can compare them for equality by doing a bunch of messy algebra. But note, since only travels halfway along , to check they are the same is to equate with , since as ranges from zero to one, ranges from zero to one half. Likewise, we can compare with .

The algebra is very messy, but doable. As a test of this blog’s newest tools, we present this screen cast of this author performing the algebra involved in proving the two curves are identical.

Now that that’s settled, we have a nice algorithm for splitting a cubic Bezier (or any Bezier) into two pieces. In Javascript,

function subdivide(curve) { var firstMidpoints = midpoints(curve); var secondMidpoints = midpoints(firstMidpoints); var thirdMidpoints = midpoints(secondMidpoints); return [[curve[0], firstMidpoints[0], secondMidpoints[0], thirdMidpoints[0]], [thirdMidpoints[0], secondMidpoints[1], firstMidpoints[2], curve[3]]]; }

Here “curve” is a list of four points, as described at the beginning of this section, and the output is a list of two curves with the correct control points. The “midpoints” function used is quite simple, and we include it here for compelteness:

function midpoints(pointList) { var midpoint = function(p, q) { return [(p[0] + q[0]) / 2.0, (p[1] + q[1]) / 2.0]; }; var midpointList = new Array(pointList.length - 1); for (var i = 0; i < midpointList.length; i++) { midpointList[i] = midpoint(pointList[i], pointList[i+1]); } return midpointList; }

It just accepts as input a list of points and computes their sequential midpoints. So a list of points is turned into a list of points. As we saw, we need to call this function times to compute the segmentation of a degree Bezier curve.

As we explained earlier, we can keep subdividing our curve over and over until each of the tiny pieces are basically lines. That is, our function to draw a Bezier curve from the beginning will be as follows:

function drawCurve(curve, context) { if (isFlat(curve)) { drawSegments(curve, context); } else { var pieces = subdivide(curve); drawCurve(pieces[0], context); drawCurve(pieces[1], context); } }

In words, as long as the curve isn’t “flat,” we want to subdivide and draw each piece recursively. If it *is* flat, then we can simply draw the three line segments of the curve and be reasonably sure that it will be a good approximation. The context variable sitting there represents the canvas to be painted to; it must be passed through to the “drawSegments” function, which simply paints a straight line to the canvas.

Of course this raises the obvious question: how can we tell if a Bezier curve is flat? There are many ways to do so. One could compute the angles of deviation (from a straight line) at each interior control point and add them up. Or one could compute the volume of the enclosed quadrilateral. However, computing angles and volumes is usually not very nice: angles take a long time to compute and volumes have stability issues, and the algorithms which are stable are not very simple. We want a measurement which requires only basic arithmetic and perhaps a few logical conditions to check.

It turns out there is such a measurement. It is is originally attributed to Roger Willcocks, but it is quite simple to derive by hand.

Essentially, we want to measure the “flatness” of a cubic Bezier curve by computing the distance of the actual curve at time from where the curve *would* be at time if the curve were a straight line.

Formally, given with control points as usual, we can define the *straight-line Bezier cubic* as the colossal sum

There is nothing magical going on here. We’re simply giving the Bezier curve with control points . One should think about this as points which are a 0, 1/3, 2/3, and 1 fraction of the way from to on a straight line.

Then we define the function to be the distance between the two curves at the same time . The flatness value of is the maximum of over all values of . If this flatness value is below a certain tolerance level, then we call the curve flat.

With a bit of algebra we can simplify this expression. First, the value of for which the distance is maximized is the same as when its square is maximized, so we can omit the square root computation at the end and take that into account when choosing a flatness tolerance.

Now lets actually write out the difference as a single polynomial. First, we can cancel the 3′s in and write the polynomial as

and so is (by collecting coefficients of the like terms )

Factoring out the from both terms and setting , , we get

Since the maximum of a product is at most the product of the maxima, we can *bound* the above quantity by the product of the two maxes. The reason we want to do this is because we can *easily* compute the two maxes separately. It wouldn’t be hard to compute the maximum without splitting things up, but this way ends up with fewer computational steps for our final algorithm, and the visual result is equally good.

Using some elementary single-variable calculus, the maximum value of for turns out to be . And the norm of a vector is just the sum of squares of its components. If and , then the norm above is exactly

And notice: for any real numbers the quantity is exactly the straight line from to we know so well. The maximum over all between zero and one is obviously the maximum of the endpoints . So the max of our distance function is bounded by

And so our condition for being flat is that this bound is smaller than some allowable tolerance. We may safely factor the 1/16 into this tolerance bound, and so this is enough to write a function.

function isFlat(curve) { var tol = 10; // anything below 50 is roughly good-looking var ax = 3.0*curve[1][0] - 2.0*curve[0][0] - curve[3][0]; ax *= ax; var ay = 3.0*curve[1][1] - 2.0*curve[0][1] - curve[3][1]; ay *= ay; var bx = 3.0*curve[2][0] - curve[0][0] - 2.0*curve[3][0]; bx *= bx; var by = 3.0*curve[2][1] - curve[0][1] - 2.0*curve[3][1]; by *= by; return (Math.max(ax, bx) + Math.max(ay, by) <= tol); }

And there we have it. We write a simple HTML page to access a canvas element and a few extra helper functions to draw the line segments when the curve is flat enough, and present the final result in this interactive demonstration (you can perturb the control points).

The picture you see on that page (given below) is this author’s rendition of Picasso’s “Dog” drawing as a sequence of nine Bezier curves. This author thinks the resemblance is uncanny :)

While we didn’t invent the drawing itself (and hence shouldn’t attach our signature to it), we did come up with the representation as a sequence of Bezier curves. It only seems fitting to present *that* as the work of art. Here we’ve distilled the representation down to a single file: the first line is the dimension of the canvas, and each subsequent line represents a cubic Bezier curve. Comments are included for readability.

Because standardizing things seems important, we define a new filetype “.bezier”, which has the format given above:

int int (int) curve (int) curve ...

Where the first two ints specify the size of the canvas, the first (optional) int on each line specifies the width of the stroke, and a “curve” has the form

[int,int] [int,int] ... [int,int]

If an int is omitted at the beginning of a line, this specifies a width of three pixels.

In a general .bezier file we allow a curve to have arbitrarily many control points, though the code we gave above does not draw them that generally. As an exercise, write a program which accepts as input a .bezier file and produces as output an image of the drawing. This will require an extension of the algorithm above for drawing arbitrary Bezier curves, which loops its computation of the midpoints and keeps track of which end up in the resulting subdivision. Alternatively, one could write a program which accepts as input a .bezier file with only cubic Bezier curves, and produces as output an SVG file of the drawing (SVG only supports cubic Bezier curves). So a .bezier file is a simplification (fewer features) and an extension (Bezier curves of arbitrary degree) of an SVG file.

We didn’t go as deep into the theory of Bezier curves as we could have. If the reader is itching for more (and a more calculus-based approach), see this lengthy primer. It contains practically everything one could want to know about Bezier curves, with nice interactive demos written in Processing.

## Low-Complexity Art

There are some philosophical implications of what we’ve done today with Picasso’s “Dog.” Previously on this blog we’ve investigated the idea of low-complexity art, and it’s quite relevant here. The thesis is that “beautiful” art has a small description length, and more formally the “complexity” of some object (represented by text) is the length of the shortest program that outputs that object given no inputs. More on that in our primer on Kolmogorov complexity. The fact that we can describe Picasso’s line drawings with a small number of Bezier curves (and a relatively short program to output the bezier curves) is supposed to be a deep statement about the beauty of the art itself. Obviously this is very subjective, but not without its proponents.

There has been a bit of recent interest in computers generating art. For instance, this recent programming competition (in Dutch) gave the task of generating art similar to the work of Piet Mondrian. The idea is that the more elegant the algorithm, the higher it would be scored. The winner used MD5 hashes to generate Mondrian pieces, and there were many many other impressive examples (the link above has a gallery of submissions).

In our earlier post on low-complexity art, we explored the possibility of representing all images within a coordinate system involving circles with shaded interiors. But it’s obvious that such a coordinate system wouldn’t be able to represent “Dog” with very low complexity. It seems that Bezier curves are a much more natural system of coordinates. Some of the advantages include that length of lines and slight perturbations don’t affect the resulting complexity. A cubic Bezier curve can be described by any set of four points, and more “intricate” (higher complexity) descriptions of curves require a larger number of points. Bezier curves can be scaled up arbitrarily, and this doesn’t significantly change the complexity of the curve (although scaling many orders of magnitude will introduce a logarithmic factor complexity increase, this is quite small). Curves with larger stroke are slightly more complex than those with smaller stroke, and representing many small sharp bends require more curves than long, smooth arcs.

On the downside, it’s not so easy to represent a circle as a Bezier curve. In fact, it is impossible to do so exactly. Despite the simplicity of this object (it’s even defined as a single polynomial, albeit in two variables), the best one can do is approximate it. The same goes for ellipses. There are actually ways to overcome this (the concept of *rational Bezier curves* which are quotients of polynomials), but they add to the inherent complexity of the drawing algorithm and the approximations using regular Bezier curves are good enough.

And so we define the *complexity* of a drawing to be the number of bits in its .bezier file representation. Comments are ignored in this calculation.

The real prize, and what we’ll explore next time, is to find a way to generate art automatically. That is to do one of two things:

- Given some sort of “seed,” write a program that produces a pseudo-random line drawing.
- Given an image, produce a .bezier image which accurately depicts the image as a line drawing.

We will attempt to explore these possibilities in the follow-up to this post. Depending on how things go, this may involve some local search algorithms, genetic algorithms, or other methods.

Until then!

Addendum: want to buy a framed print of the source code for “Dog”? Head over to our page on Society6.

Great post! Looking forward to the next one.

An ever so slightly simpler yet still robust test for flatness was discussed here and on comp.graphics.algorithms: http://antigrain.com/research/adaptive_bezier/index.html#toc0013

Also of interest, “Pyramid Algorithms” by Ron Goldman covers Wang’s formula (chap. 5.6.3) for determining in advance how many levels of subdivision you need to achieve a specified degree of flatness. Wang’s formula is also discussed in: DEC Paris Research Laboratory report #1, May 1989. Clearly this approach will be more conservative than testing each segment.

That’s a lot of great info! From what I understand the metric I presented here is what’s used in the Postscript language. Not to say whether that makes it good or not, but at least it’s stood the test of time (and engineers).

Another good read is here: “A History of Curves and Surfaces in CAGD” [ http://kowon.dongseo.ac.kr/~lbg/cagd/history1.pdf ].

the href link to ” this blog’s Google code page.” is incorrect

What tools did you use to make the video? It’s great!

Sketchbook for drawing with a Wacom Bamboo tablet, and Screenflow to capture it all. Made possible by donations to this blog :)

Thanks for your answer, and congratulations on getting enough out of the blog to afford such awesome tools. Screenflow especially looks great; too bad I don’t use Macs…

I tried to find out in which year Picasso made the dog drawing. Do you know (since you have a copy)? A few years after 1957? See http://en.wikipedia.org/wiki/Lump_(dog)

The bull lithographs are from 1945 according to your link.

So my guess is that Bezier curves came after Picasso’s lines, namely in the sixties, see http://en.wikipedia.org/wiki/B%C3%A9zier_curve

How stable is your drawing? (Sensitivity to small changes in the numbers)

A related effort to yours, although less numerical, is at http://i-work-in-pages.blogspot.ca/2011/04/picasso-dog-in-one-line.html

Cheers,

Stephan

Apparently between 1936 and 1942. http://sapergalleries.com/PicassoLeChienDetail.html

The drawing is quite stable. The nature of Bezier curves makes them stable to small perturbations of the control points.

In fact, the original inventor of Bezier curves was Paul de Casteljau, and he published (or made public) his work on Bezier curves in 1959. So it’s quite amazing how close together these two ideas are in history.

Thanks, I saw that page, but couldn’t make out that it also related to the simple line drawing. So you think it is not a drawing of Lump, the 1957 dog.

There’s another sense of closeness, Picasso living in France during those years, as did, I take it, Bézier and de Casteljau.

Cheers,

S

It seems you left out the ’3′ coefficients in the equation of the cubic Bezier curve?

Yeah, I found I do that a lot.

Picasso couldn’t have used Bezier curves because they hadn’t been invented in the 14th Century when Picasso was painting.

You must certainly be trolling.

Fantastic article! It’s wonderful to see different disciplines being explored at the same time.

Great article, it inspired me to make something like it in Racket.

It took me more time that what i wanted, but now anyone can do this in DrRacket:

#lang s-exp (planet tomcoiro/doodle-draw:1:0/lang)

500 500

(180 280 183 268 186 256 189 244)

(191 244 290 244 300 230 339 245)

(340 246 350 290 360 300 355 210)

(353 210 370 207 380 196 375 193)

(375 193 310 220 190 220 164 205)

(164 205 135 194 135 265 153 275)

(153 275 168 275 170 180 150 190)

(149 190 122 214 142 204 85 240)

(86 240 100 247 125 233 140 238)

(show “Picasso’s Dog”)

And get a frame with a dog drawed on it.

Very cool. I’ll have to try this.