📜 ⬆️ ⬇️

www.contextfreeart.org - programming in the service of art

Post to those who want a little bit of distraction from work and having fun with the mind.

Today came across an interesting project: contextfree

Everyone remembers the course of the theory of compilers from the university? contextfree is an image generator based on context-free grammars . The terminals are simple graphic primitives (CIRCLE, SQUARE, TRIANGLE, etc.). No terminals we ask. Inference rules can include so-called transformations (rotate, flip, shift, change color). Based on this simple model, you can get very interesting things ... just a few lines of code.
')
Let's start with a simple example:
startshape JustASquare //

background { b -1 } // . b -1 -
// (brightness = 1), brightness 0.

rule JustASquare { // .
SQUARE { b 1 r 45 } // , 45 .
}




Let's try something more complicated. Something recursive.
startshape Circles
background { b -1 }

rule Circles {
CIRCLE { b 1 } //
CIRCLE { s 0.7 } // (s - ), .
Circles { x 1 s 0.9 r 30 } // . .
// ,
// — .
}


Notice, just a few lines, but already something beautiful :)

Let's implement with you the simplest recursive tree. On it, I will show that there can be several inference rules with identical left parts, then they are chosen randomly based on the “weight” of the rule.
startshape begin

background { b -1 }

rule begin {
tree { b 1 }
}

rule tree { // .
CIRCLE { s 1 2 }
tree { y 1 s 0.99 r 7 b -0.005}
}

rule tree 0.2 { // . 0.2 - "" .
tree { flip 90 }
}

rule tree 0.1 { // .
tree { r 10 }
tree { s 0.5 r -20 }
}


The code is already a little more difficult to figure out, but who cares, just try ... And you will figure it out very quickly.

Well, finally, what happened with me ... not very original, but he wrote himself, not peeping anywhere :)
startshape begin

background { b -1 }

rule begin {
tree { x 10 }
}

rule tree {
branch {}
}

rule branch {
CIRCLE { s 0.5 2 b .4 h 120 saturation .7}
branch { y 1 s 0.97 r 3 b -.2 }
}

rule branch 0.1 { //
branch { flip 90 }
}

rule branch 0.3 {
branch { r 3 }
branch {s 0.5 r -30}
}

rule branch 0.01 {
branch2 {}
}

rule branch2 { //
CIRCLE { s 0.5 2 b .4 h 120 saturation .7}
branch2 { y 1 s 0.99 r 3 b -.2 }
}

rule branch2 0.1 {
branch2 { flip 90 }
}

rule branch2 0.01 { //
Rose { s 5 }
}

rule Rose { // , ..
CIRCLE { b 0.5 saturation 0.7 }
CIRCLE { b 1 s 0.8 saturation 0.7}
Rose { r 45 x 0.5 s 0.95 }
}

On the picture you can click to view closer.


Here is such an interesting program.
She has a very small wiki .
And frontend on ruby ​​+ gnome.

That's all. Creative success to you!

PS I'm thinking of writing about shoes - a framework for creating lightweight GUIs for Ruby from the famous maniac _why. Perhaps in the article we will implement a simple cross-platform GUI for contextfree. And maybe something else. We'll see.

Source: https://habr.com/ru/post/28393/


All Articles