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

Friday, 7 August 2009

Expressions in context free

A bit like the perl and ruby crowd, there has been the occasional competition to produce a minimal set of context free rules (sic one-liners), to do something quite fantastic. The rules are freer in the context free game as the target is to produce some fabulous bit of art, rather than parse some obscure file (such as a gedcom file of the royal family) as often is the case with the perl and ruby competitions. The winner of one these competitions threw up some intriguing code, with a couple of rand_static() functions. This was something I had not come across before, but it is explained in the Expressions section of the documentation. The key to understanding how it works is in the postfix "static", the randomize functions are converted into random numbers at compile time so during the run you are looking at a constant number. However each time the code is run you will get a different number. Which is all a bit unfortunate for every good run expect a couple of duffers, this where my python multi run script comes in handy.
However I will try and incorporate some of the more useful expressions (cos, sin, sqrt, exp etc) in my future rules, note that you may need to completely parenthesize these expressions, owing the terse syntax of cfdg (no separators, the parser will get confused).

If you've got some complicated expression you would like to incorporate in several places in your cfdg checkout this from Florian Brucker you can use the C preprocessor to do the dirty work for you. This is my previous mondrian example where the phi(A) is substituted (on the fly if you use Florians cfdgpp script). Note the very necessary extra parenthesis with the negative phi.

   1:startshape mondrian
3:# define phi(A) (A * (sqrt(5) +1)/2)
5:rule mondrian {
6: white_box{}
7: yellow_box{y 1.005}
8: red_box{x 1.005}
9: blue_box{y 1.005 x 1.005}
12:path red_box {
13: FILL{brightness 1 hue 360 sat 1}
14: MOVETO{x 0.5 y phi(0.5)}
15: LINETO{x -0.5 y phi(0.5)}
16: LINETO{x -0.5 y (-phi(0.5))}
17: LINETO{x 0.5 y (-phi(0.5))}
19: STROKE{}
21: path white_box{
22: MOVETO{x 0.5 y phi(0.5)}
23: LINETO{x -0.5 y phi(0.5)}
24: LINETO{x -0.5 y (-phi(0.5))}
25: LINETO{x 0.5 y (-phi(0.5))}
27: STROKE{}
29:path yellow_box {
30: FILL{brightness 1 hue 60 sat 1}
31: MOVETO{x 0.5 y 0.5}
32: LINETO{x -0.5 y 0.5}
33: LINETO{x -0.5 y 0.25}
34: LINETO{x -0.5 y -0.25}
35: LINETO{x 0.5 y -0.25}
37: STROKE{}
39:path blue_box {
40: FILL{brightness 1 hue 220 sat 1}
41: MOVETO{x 0.5 y 0.5}
42: LINETO{x -0.5 y 0.5}
43: LINETO{x -0.5 y -0.5}
44: LINETO{x 0.5 y -0.5}
46: STROKE{}

No comments:

Post a Comment


Blog Archive

About Me

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