How To: Program in Context Free

Program in Context Free

            In my free time, I like to program in an image programming language called Context Free.  You can download it at www.contextfreeart.org for free.

            The first thing to know about Context Free is that there are three basic shapes to which the program will understand and respond.  They are circles, squares, and triangles. These three shapes can make very complex images even though they themselves are very simple.  In this tutorial, I will be walking you through the steps to make a tree-like image.  In addition, during this tutorial, you will see text that is Italisized and Bolded.  When you see the Italisized words, enter that text into the text area in the program.  When you see the Bolded words, it tells you what you have already entered into the text area in the program.

Step 1

Define the name of the shape. 

startshape TREE

Step 2

            Define the variable TREE because the program cannot understand what TREE is because it is not one of the three shapes it knows.

startshape TREE

rule TREE {

BRANCH { }

}

Step 3

            Define the variable BRANCH because the program still cannot understand what the shape TREE is because it doesn't know what BRANCH is either. Now, once you are finished typing the code, because the program knows what CIRCLE is, if you render this you will get a nice, round, black circle (As shown below).

startshape TREE

rule TREE {

BRANCH { }

How to Program in Context Free}

rule BRANCH {

CIRCLE { }

}

Step 4

          To make the image look more interesting, add some attributes to BRANCH.  Do this and you will get a nice, black spiral.  This happens because it takes the original "rule BRANCH" and it moves what the rule creates 0.2 units on the y-axis, makes it 0.99 smaller than the last "rule BRANCH", and rotates the circle 3 units.  It does this by specifying these attributes to BRANCH, making it have those attributes when it runs the rule BRANCH.  By putting the rule BRANCH inside of its definition, it causes the rule to run itself again, and because when it runs again it sees BRANCH in the rule so, it runs itself again, and again, and again, resulting in endless repetition until what it is rendering would be smaller than a single pixel making it impossible to display so the program tells itself to stop rendering that rule because it won't be able to display it because it is too small.  By doing this, it creates a spiral of shrinking circles that are turning, creating a perfect spiral composed of circles.

startshape TREE

rule TREE {

BRANCH { }

}

rule BRANCH {

CIRCLE { }

BRANCH { y 0.2 size 0.99 rotate 3 }

}

How to Program in Context Free

Step 5

    To make the spiral look more interesting, I am going to add a new object into rule BRANCH. By rendering that you will end up with a black and white spiral.  This is because you are making a white circle that is one size smaller than the last one (which was black) making you only see a black outline because the white circle is covering the middle of the black circle.

startshape TREE

rule TREE {

BRANCH { }

}

rule BRANCH {

CIRCLE { }

CIRCLE { size 0.9 brightness 1 }

BRANCH { y 0.2 size 0.99 rotate 3 }

}

How to Program in Context Free

Step 6

            To make the shape more organic looking, I will add randomization.  By doing this you will see that the spiral has turned into an organic, randomized vine shape.  That is because the numbers after the "rule BRANCH" are percentages so that it uses one rule 98% of the time while it uses the other 2% of the time, and because "flip" makes the branch flip itself the number of units specified, making it no longer be a perfect spiral.

startshape TREE

rule TREE {

BRANCH { }

}

rule BRANCH 98 {

CIRCLE { }

CIRCLE { size 0.9 brightness 1 }

BRANCH { y 0.2 size 0.99 rotate 3 }

}

rule BRANCH 2 {

CIRCLE { }

CIRCLE { size 0.9 brightness 1 }

BRANCH { y 0.2 size 0.99 flip 90 }

}

How to Program in Context Free 

Step 7

             Now it looks much more interesting but there is still much more that can be done to it... How about we make other BRANCHes fork off the main BRANCH?  Render that and you will see the vine-like shape that we made in the last section now has little fading BRANCHes coming off of the one main BRANCH making it look somewhat like a tree because it seems to give a spot for where the leaves might be.

startshape TREE

rule TREE {

BRANCH { }

}

rule BRANCH 98 {

CIRCLE { }

CIRCLE { size 0.9 brightness 1 }

BRANCH { y 0.2 size 0.99 rotate 3 }

}

rule BRANCH 2 {

CIRCLE { }

CIRCLE { size 0.9 brightness 1 }

BRANCH { y 0.2 size 0.99 flip 90 }

BRANCH { y 0.2 size 0.6 brightness 0.2 }

}

How to Program in Context Free

Step 8

    To make it look more like a tree I am going to add more detail (Like giving it multiple BRANCHes etc.).  By rendering that we will add a so-called "trunk" to the shape, it makes multiple BRANCHes, making it look a lot more like a tree.  At this point, you have a wonderful and very complex image.

startshape TREE

rule TREE {

BRANCH { }

BRANCH { flip 90 }

}

rule BRANCH 98 {

CIRCLE { }

CIRCLE { size 0.9 brightness 1 }

BRANCH { y 0.2 size 0.99 rotate 3 }

}

rule BRANCH 2 {

CIRCLE { }

CIRCLE { size 0.9 brightness 1 }

BRANCH { y 0.2 size 0.99 flip 90 }

BRANCH { y 0.2 size 0.6 brightness 0.2 }

}

How to Program in Context Free

            Because the results of this are very pleasing, it would probably make you feel that there is no need to complicate the image any more…  Most of the time, that is how I end up deciding when an image is done, once it is very complicated and that when I show it to other people they ask for a copy of it on their flash drive (those are all indicators that is very complicated and interesting).

            This image, along with many others has been the product of lots of my time and work.  All the ones that I have made look good to me and I am  happy about that, as would anyone who had spent a lot of time working on this sort of stuff.  Here are some other pictures that I am proud of (I am proud of all my images but because there is only so much space on a page and I don't want this tutorial to be 12 pages long so I have chosen a few random images) that I have created with Context Free:

These are only a few of many, many more, which are all equally unique.  One of the reasons that I like to do this is because I always like seeing the results that my time, effort, and thought creates...  I recommend this to other people because it is a fun hobby to have and it makes very nice results.  I do it in my free computer time on my computer whenever I get the chance because I like it so much.  I hope that whatever you do with what you have learned, that you have fun doing it!

Just updated your iPhone to iOS 18? You'll find a ton of hot new features for some of your most-used Apple apps. Dive in and see for yourself:

3 Comments

I hope everyone likes it... Because I made it.

People wouldn't come to it and rate it good if they didn't like it...

I like it SOOOO much!!! I learned SOOOOO much!!! Problem is, you showed it to me a long time ago.

Share Your Thoughts

  • Hot
  • Latest