Tuesday, May 15, 2012

Recursive Drawing with Kojo

A few days ago, I discovered (via Hacker News) a site called recursivedrawing.com. This site contains a very interesting tool for making drawings based on the idea of recursion. And even though the site focuses on a visual exploration of this idea, I was immediately struck by the conceptual similarity of this tool to Pictures within Kojo. So, without further ado, here are some of the drawings from the demo video on the site - done in Kojo.

And btw, doing art of this nature works very well with the recently introduced Interactive Program Moulding feature within Kojo.


Recursive Circles

 

Here's a Kojo based rendition of the drawing:




And here's the corresponding Kojo (Scala) code:

def C = penColor(noColor) * fillColor(Color(0, 136, 204)) -> Picture {
    circle(50)
}

clear()
setBackground(Color(255, 170, 29))
invisible()
def drawing(n: Int): Picture = {
    if (n == 1) 
        C
    else 
        GPics(
            C,
            brit(0.05) * trans(124, 0) * rot(-35) * scale(0.9) -> drawing(n-1)
        )
}

val pic = rot(60) -> drawing(50)
draw(pic)

 

Binary Tree

 

Here's a Kojo version of the drawing:




And here's the corresponding code:

val size = 100
def S = Picture {
    repeat (4) {
        forward(100)
        right()
    }
}

def stem = scale(0.13, 1) * penColor(noColor) * fillColor(black) -> S

clear()
setBackground(Color(255, 170, 29))
invisible()

def drawing(n: Int): Picture = {
    if (n == 1) 
        stem
    else 
        GPics(stem,
              trans(0, size-5) * brit(0.05) -> GPics(
                rot(25) * scale(0.72) -> drawing(n-1),
                rot(-50) * scale(0.55) -> drawing(n-1)
            )
        )
}

val pic = trans(0, -100) -> drawing(10)
draw(pic)

 

Fib Tree

 

Here's a Kojo version of the drawing:




And here's the corresponding code:

val size = 100
def S = Picture {
    repeat (4) {
        forward(100)
        right()
    }
}

def stem = scale(0.13, 1) * penColor(noColor) * fillColor(black) -> S

clear()
setBackground(Color(255, 170, 29))
invisible()

def drawing(n: Int): Picture = {
    if (n == 1) 
        stem
    else 
        GPics(stem,
              trans(2, size-5) * brit(0.05) -> GPics(
                rot(25) * scale(0.72) -> drawing(n-1),
                rot(25) * trans(0, size * 0.72) * rot(-75) * scale(0.55) -> drawing(n-1)
            )
        )
}

val pic = trans(0, -100) -> drawing(10)
draw(pic)

Happy recursing!

No comments: