Context Free Art

Shape rules

From Context Free Art

Jump to: navigation, search

Shape Rules

Shape rules describe how to draw a given shape. A shape is composed of some number of primitive shapes (squares, circles, and triangles) and non-primitive shapes. There must be at least one rule for each shape referenced or Context Free/CFDG will give you an error. A rule has the word rule followed the name of the shape being defined and then the shape replacements in curly braces. There can also be an rule weight between the name and the left curly brace. If the rule weight is omitted then the default weight of 1 is used. Rule weights must be positive real numbers.

After Context Free/CFDG picks a rule it executes it by drawing each of the shape replacements contained within the rule. Primitive shapes are drawn right away and non-primitive shapes are put in the evaluation queue and drawn later.

Overlapping Shapes and the Order of Drawing

There is no guaranteed order to the drawing of shapes. The currently implemented rendering algorithm renders larger shapes first, so smaller shapes will be drawn later, on top of them. But this must not be relied upon, as the algorithm has changed in the past and may change again. There are only two guaranteed orders to drawing: primitive shapes in a rule draw in order before anything else, and shapes with a larger z-axis coordinate draw on top of shapes with smaller z, regardless of when they were rendered.

rule foo1 {
    SQUARE {}
    CIRCLE { b 1 }           // draws on top of the blank square
    foo1 { r 45 x 1 s 0.9 }  // draws on top of both
}
 
rule foo2 {
    CIRCLE { b 1 z 1 }
    SQUARE {}                // drawn under the white circle
    foo2 { r 45 x 1 s 0.9 }
}
 
rule foo3 {
    SQUARE {}
    CIRCLE { b 1 }
    foo3 { z -1 r 45 x 1 s 0.9 }  // drawn under both
}

Randomness, Rule Weights, and Having Multiple Rules for a Shape

If there is only one rule for all the shapes in a CFDG file then the result will always look the same. But when there is more than one rule for a given shape then Context Free/CFDG has to choose which one to use at random. The probability that a particular rule will be used is proportional to its weight. The exact equation is the rule's weight divided by the sum of the rule weights for the given shape.

rule aShape 0.5 {  // probability is 0.5 / (0.5 + 1 + 2), or 1/7
    SQUARE {}
}
 
rule aShape {      // probability is 1 / (0.5 + 1 + 2), or 2/7
    CIRCLE {}
}
 
rule aShape 2 {    // probability is 2 / (0.5 + 1 + 2), or 4/7
    TRIANGLE {}
}

Another use for multiple rules is to provide an ending condition to an infinite recursion:

startshape triArc
background { b -1 }  // looks more cool on black
 
rule triArc {
    TRIANGLE {r -2 sat 1 b 1 }
    triArc { x 1 r 4 hue 4 }  // Never shrinks! This recursion will never end...
}
 
rule triArc 0.01 {
    TRIANGLE { r -45 sat 1 b 1 }
    triArc { y -1 r -90 hue 4 }
}
 
rule triArc 0.001 {} // ... unless we execute this rule
Views
Personal tools
Navigation
Toolbox
MediaWiki
Attribution-Share Alike 2.5
book coverSee our new book:
Community of Variation