
Context Free — ars ex machina
Machina
Context Free is a program that generates images from written instructions called a grammar. The program follows the instructions in a few seconds to create images that can contain millions of shapes.
CFDG (Context Free Design Grammar) is language for describing a context-free grammar, unsurprisingly. Unlike the usual, however, it is not used to prescribe a grammar and parse a given source text, but to be recursively applied to reify the "grammar" into an image. Context Free, on the other hand, is a tool that implements that transformation, allowing hours to pass by as a perfect (or not) image emerges from simple, but structured, text.
What makes CFDG and Context Free really tubular is infinite recursion: the ability to apply rules inside the same rules inside the same rules inside the same rules. Until the details become so small that they no longer can be drawn. Or until the specified size. Maybe until they are no longer in-bounds. Because, in the end, it's the user who is in control of the generation.
Now, seeing is believeing or something like that, so let's just jump into it with a small example:
// Make the background transparent
CF::Background = [alpha -1]
// Set the starting shape (required before version 3.4),
// and set the desired color in HSB
startshape triangle [hue 227.9 saturation 0.5064 brightness 0.4608]
// Define the shape rule
shape triangle {
▲ [] // Boring "TRIANGLE []" can be used, too
triangle [size 0.87 rotate 5 brightness -0.1] // Recurse
}
Now, by default Context Free adds some borders to the generated image. This makes sense, yes. However, this concrete file uses only a single unit-sized shaped at most (all primitives have a size of 1 unit), so borders would be gigantic. Not only that, but default positioning is also off, so cropping needs to be done. And, of course, we want a nice vector image to be served on the internet, so SVG makes more sense. So, let's generate the image with cfdg --crop --bordersize=0 --svg triangle.cfdg triangle.svg
and see this beauty. Context Free on Windows and macOS probably have a graphical interface to do this. I wouldn't know.
Well, that's a dapper triangle! But it is quite blocky. If a smoother gradient is desired, adjustments in recursive triangle
calls can be made. For example, triangle [size 0.97 rotate 1 brightness -0.03]
makes almost the same shape, but the gradient becomes hard to notice (even if not actually completely smooth). This is achieved by Context Free generating significantly more shapes through the recursion, making the resulting SVG file ≈4 times larger and increasing the number of drawn shapes: going from 54 to 244.
Now, for something more interesting, how about randomness? Context Free allows for a few different ways to randomize things, such as choosing one shape out of a set, looping a random number of times, or using randomized values for adjustments. Let's look at a garden:
shape GARDEN {
// Generate random number of plants at random positions
loop 4…10 [x 1…5] {
GROUND [z -10]
transform [y -5…-3.5] {
PLANT []
}
}
}
...
// Possible flower
if (randint(3) == 0) {
FLOWER [y 1 z 1]
}
...
shape FLOWER
rule 50% {
loop 6 [r 60] PETAL []
CIRCLE [s 0.5 b 1 h 60 sat 0.3]
}
rule {
// Bud
CIRCLE [s 0.5 0.3 y -0.9 b 0.6 h 120 sat 0.5]
PETAL [s 1.1 y -1]
PETAL [x 0.2 y -1 r 10]
PETAL [x -0.2 y -1 r -10]
SEPAL [s 1 x 0.1 y -1 r -20]
SEPAL [s 1 x -0.1 y -1 r 20]
}
Neat, wouldn't you say? This is just a taste of what Context Free can do. People have used CFDG to create all sorts of images, from abstract patterns and fractals to fairly detailed painstakingly-crafted pictures. Of course, it's mostly simple things, but with enough time and effort really crazy stuff can be done too.
Ars
Explore the Context Free gallery












