Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Saturday, 13 June 2009

Combining Two CFDG files

One of the examples that you get with context free code distribution is demo1.cfdg, I would have called it trees, now there is another little example I found which has grass, adding grass to trees is not as simple as you might think. This sent me off in search of tutorials again, there's not much out there. However an amazing wrinkle concerns how you introduce random behaviour into your context free rules. Essentially you give the program more than one rule for a given shape, it will then randomly choose one of those rules. To do this efficiently you should make use of the principles of binary search. Say for example you want to randomly choose value between 0 and 360 (angle/color). Then you make your first split 0 or 180, the second 0 or 90 and so on you know the sort of thing, well before long you've got it covered, well in truth a lot of programs do this sort of thing for you, but here you need to be explicit (it also saves writing 0 to 360 rules). For the mac user you can get an omnigraffle (whatever that is) or for the rest of us pdf reference card at
http://www.contextfreeart.org/mediawiki/index.php/Reference_Card
that explains context free syntax in a 'nutshell'.


Anyway finally I managed to combine the two files, see what you think. If you run the rules yourself and don't like what you get run them again, the trees are random (by the way in terse syntax r == rotate not random as I first thought).

   1:startshape forest // modified martinstannard grass.cfdg && forest == demo1.cfdg
2:
3:background {hue 200 sat 0.5}
4:rule forest {
5: seed {s 400 800}
6: seed {x -20}
7: seed {x -40}
8: world{h 120 sat 1 b 0 r 90 }
9: world{h 100 sat 1 b 0 r 90 }
10: world{h 80 sat 1 b 0 r 90 }
11:}
12:
13:rule seed {branch {}}
14:rule seed {branch {r 1}}
15:rule seed {branch {r -1}}
16:rule seed {branch {r 2}}
17:rule seed {branch {r -2}}
18:rule seed {fork {}}
19:
20:rule branch {leftbranch {flip 90}}
21:rule branch {leftbranch {}}
22:
23:rule leftbranch 4 {block {} leftbranch {y 0.885 r 0.1 s 0.99}}
24:rule leftbranch 4 {block {} leftbranch {y 0.885 r 0.2 s 0.99}}
25:rule leftbranch {block {} leftbranch {y 0.885 r 4 s 0.99}}
26:rule leftbranch {block {} fork {}}
27:
28:
29:rule block {
30: SQUARE {r 1}
31: SQUARE {r -1}
32: SQUARE {}
33:}
34:
35:rule fork {
36: branch { }
37: branch {s 0.5 r 40}
38:}
39:rule fork {
40: branch { }
41: branch {s 0.5 r -40}
42:}
43:rule fork {
44: branch {s 0.5 r -20}
45: branch { }
46:}
47:rule fork {
48: branch {s 0.7 y 0.1 r 20}
49: branch {s 0.7 y 0.1 r -20}
50:}
51:
52:rule world
53:{
54: 800*{y 1 } blade{ sat 0.2 b 0.5}
55:}
56:
57:rule blade 25
58:{
59: SQUARE{}
60: blade{x 0.95 r -2 s 0.98 }
61:}
62:
63:rule blade 18
64:{
65: SQUARE{}
66: blade{x 0.90 r 1 s 0.97 b 0.1 }
67:}
68:
69:rule blade 16
70:{
71: SQUARE{}
72: blade{x 0.90 r 2 s 0.93 b 0.1 }
73:}
74:
75:rule blade
76:{
77: SQUARE{}
78:}


One thing I read about context free is that the program is unlikely to fail on error, if there are ambiguous commands, the program will just follow one. SQUARE and CIRCLE are the big built in features, infinite recursion is allowed so long as results tend to zero....
I don't know that there are any strict conventions on case so I've used lowercase for non built in rules.

Click on image to see the picture properly.

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2