I recently happened across this really nifty “drawing” program called Context Free Art – it renders images from context-free grammars. For those who aren’t into language theory or computer science, a context-free grammar is a specific kind of formal grammar, basically a set of rules for generating or recognizing sequences of symbols conforming to some pattern.
So, basically you can specify an image in terms of “rules,” which can themselves be specified in terms of basic shapes (circles, squares, triangles) or of other rules. A really simple example would be something like this:
startshape TwoSquares
rule TwoSquares {
SQUARE { size 1 }
SQUARE { size 1 x 1 y 1 }
}
…which would just draw two 1x1 squares, one offset by (1,1) from the other. And basically from there it can get as complex as you like. Rules can also be recursive (i.e., refer to themselves), and have multiple possible derivations (which are selected from randomly), which makes this program very well suited for fractal drawing.
For example, I made this pretty little fractal tree-and-sun scene. It was generated using the following code:
startshape Scene
rule Scene {
Sky {h -30 s 40 60 x -5 y 20 }
Sky {h -120 s 40 60 x -5 y 20 flip 180}
FOOX { s 0.4 z 0 y 15 }
Sun { z 1 }
Tree { z 2 hue 42 }
Roots { }
}
rule Sun { CIRCLE { x -4 y 15 size 15 hue 0 sat 0.2 brightness 1 } }
#
# The tree!
#
rule Tree .1 { Segment { } Branch { } }
rule Tree 1 { Segment { } BranchLeft { } }
rule Tree 1 { Segment { } BranchRight { } }
rule Tree .15 { Segment { } BranchLeft { brightness .02 } BranchRight { brightness .02 } }
#
# Branching behavior
#
rule Branch { Tree { y 1 size 0.98 hue .1 sat 1 } }
rule BranchLeft { Branch { rotate -5 } }
rule BranchLeft { Branch { rotate -10 } }
rule BranchLeft { Branch { rotate -15 } }
rule BranchRight { Branch { rotate 5 } }
rule BranchRight { Branch { rotate 10 } }
rule BranchRight { Branch { rotate 15 } }
#
# Roots
#
rule Roots { 8* { r 12 } Root { r 120 } }
rule Root 1 { Segment { } Root { y 1 s 0.96 } }
rule Root 1 { Segment { } Root { y 1 s 0.96 r 10 } }
rule Root 1 { Segment { } Root { y 1 s 0.96 r -10 } }
rule Root .1 { Segment { } Root { y 1 s 0.94 r -10 } Root { y 1 s 0.94 r 10 } }
#
# The basic segment from which all parts of the tree are built
#
rule Segment {
SQUARE { s 1 1.1 }
CIRCLE { y 0.5 s 1 }
}
#
# Simple gradient "sky" pattern
#
rule Sky {
SQUARE { b 0.5 sat 0.75 a -0.99 }
Sky { s 1 0.8 y 0.1 a 0.001 }
}
The specific variation seed used to generate the image linked above was “HEO.” Render it with different seeds and you’ll get a wildly different tree every time! I also fiddled around with the tree color and background color, and added a starfield (yoinked from this entry in the Context-Free gallery) to get this night-time variation. Purty, ain’t it?
Context-Free Art is completely free and available for Windows, Mac OSX, and Linux, so go on and give it a try! Be sure to check out the included “lesson” examples and the documentation, especially the reference card, which lists all the various things you can do in Context Free. So, have at it: let’s see what kinds of amazing fractal creations Dopers can come up with!