I hope this code here will be a bit easier on the eye. :) I also added some comments to make it more readable.

`/* Penrose tilings of the P2 type. There are two functions in this program that call themselves and each other recursively: halfKite and halfDart. Note that each function draws an equilateral triangle from the top, and that both functions always exit with the turtle having the same heading as when they entered. */ val Phi = (1 + math.sqrt(5)) / 2 // The golden ratio var kiteColor = green var dartColor = blue def halfKite(depth: Int, length: Double, mirrored: Int): Unit = { if(depth == 0) { // Base case: setFillColor(kiteColor) right(mirrored * 162) forward(length) right(mirrored * 108) forward(length / Phi) right(mirrored * 108) hop(length) left(mirrored * 18) } else { // Recursive case: we draw a half kite by means of // drawing two smaller half kites and a half dart. // // Move the turtle to the right position for the // half dart... right(mirrored * 162) hop(length / (Phi + 1)) left(mirrored * 54) // And draw it! halfDart(depth - 1, length / (Phi + 1), mirrored) right(mirrored * 54) hop(length / Phi) left(mirrored * 54) halfKite(depth - 1, length / Phi, mirrored) right(mirrored * 36) // Note that this half kite is mirrored with // respect to the larger one it is a part of. halfKite(depth - 1, length / Phi, -mirrored) left(mirrored * 162) hop(length) right(mirrored * 18) } } def halfDart(depth: Int, length: Double, mirrored: Int): Unit = { // As in the case of the half kite, we again // distinguish between a base case and a recursive // case. The recursive half dart is composed of one // half kite (mirrored) and a half dart (not mirrored). if(depth == 0) { setFillColor(dartColor) right(mirrored * 126) forward(length) right(mirrored * 144) forward(length * Phi) right(mirrored * 144) hop(length) left(mirrored * 54) } else { left(mirrored * 126) hop(length) right(mirrored * 18) halfKite(depth - 1, length, -mirrored) left(mirrored * 162) hop(length) right(mirrored * 126) halfDart(depth - 1, length / Phi, mirrored) right(mirrored * 126) hop(length / Phi) right(mirrored * 18) } } // The sun pattern can be drawn by drawing 10 half kites, // half of which are mirrored. def sun(depth: Int, length: Double): Unit = { repeat(5) { halfKite(depth, length, -1) right(36) halfKite(depth, length, 1) right(36) } } // Some scripting to call the sun function six times, // with a depth ranging from 1 to 6. // // After each call, a spin is given to the color of // both kite and dart. toggleFullScreenCanvas() repeatFor(1 to 6) { i => cleari() setBackground(black) setPenColor(black) setPenThickness(6 - i) setSpeed(superFast) right(18) sun(i, 300) kiteColor = kiteColor.spin(60) dartColor = dartColor.spin(60) Thread.sleep(2500) } toggleFullScreenCanvas()`

I think the program below should be easy to understand, well, at least considering that Penrose tilings are not the simplest recursive programs imaginable. But Kojo allows for doing this in a way that it is, algorithmically, completely transparent without the code 'getting in the way' at all.

`/* Penrose tilings of the P2 type. There are two functions in this program that call themselves and each other recursively: halfKite and halfDart. Note that each function draws an equilateral triangle from the top, and that both functions always exit with the turtle having the same heading as when they entered. */ val Phi = (1 + math.sqrt(5)) / 2 // The golden ratio var kiteColor = green var dartColor = blue def halfKite(depth: Int, length: Double, mirrored: Int): Unit = { if(depth == 0) { // Base case: setFillColor(kiteColor) right(mirrored * 162) forward(length) right(mirrored * 108) forward(length / Phi) right(mirrored * 108) hop(length) left(mirrored * 18) } else { // Recursive case: we draw a half kite by means of // drawing two smaller half kites and a half dart. // // Move the turtle to the right position for the // half dart... right(mirrored * 162) hop(length / (Phi + 1)) left(mirrored * 54) // And draw it! halfDart(depth - 1, length / (Phi + 1), mirrored) right(mirrored * 54) hop(length / Phi) left(mirrored * 54) halfKite(depth - 1, length / Phi, mirrored) right(mirrored * 36) // Note that this half kite is mirrored with // respect to the larger one it is a part of. halfKite(depth - 1, length / Phi, -mirrored) left(mirrored * 162) hop(length) right(mirrored * 18) } } def halfDart(depth: Int, length: Double, mirrored: Int): Unit = { // As in the case of the half kite, we again // distinguish between a base case and a recursive // case. The recursive half dart is composed of one // half kite (mirrored) and a half dart (not mirrored). if(depth == 0) { setFillColor(dartColor) right(mirrored * 126) forward(length) right(mirrored * 144) forward(length * Phi) right(mirrored * 144) hop(length) left(mirrored * 54) } else { left(mirrored * 126) hop(length) right(mirrored * 18) halfKite(depth - 1, length, -mirrored) left(mirrored * 162) hop(length) right(mirrored * 126) halfDart(depth - 1, length / Phi, mirrored) right(mirrored * 126) hop(length / Phi) right(mirrored * 18) } } // The sun pattern can be drawn by drawing 10 half kites, // half of which are mirrored. def sun(depth: Int, length: Double): Unit = { repeat(5) { halfKite(depth, length, -1) right(36) halfKite(depth, length, 1) right(36) } } // Some scripting to call the sun function six times, // with a depth ranging from 1 to 6. // // After each call, a spin is given to the color of // both kite and dart. toggleFullScreenCanvas() repeatFor(1 to 6) { i => cleari() setBackground(black) setPenColor(black) setPenThickness(6 - i) setSpeed(superFast) right(18) sun(i, 300) kiteColor = kiteColor.spin(60) dartColor = dartColor.spin(60) Thread.sleep(2500) } toggleFullScreenCanvas()`

I made a little demo out of it, called Roger.

You can download the demo here (my apologies, the source code is still much less elegant than it ought to be): http://joostwinter.net/crazywizardsoftware/roger/

…or alternately watch the video on Youtube: https://www.youtube.com/watch?v=FrruqBlXbcM

Enjoy!

]]>`var baseOne = 2 * math.sin(math.Pi * 54 / 180) var baseTwo = 2 * math.sin(math.Pi * 18 / 180) def triangleOne(depth: Int, length: Double, mirrored: Int): Unit = { if(depth == 0) { setFillColor(cm.green) forward(length) right(mirrored * 72) forward(length) right(mirrored * 144) hop(length * baseOne) right(mirrored * 144) } else { right(mirrored * 36) hop(length) left(mirrored * 144) triangleOne(depth - 1, length / baseOne, -mirrored) hop(length / baseOne) right(180) triangleTwo(depth - 1, length / baseOne, -mirrored) hop(length / baseOne) right(-mirrored * 36) hop(length / baseOne) right(180) triangleOne(depth - 1, length / baseOne, mirrored) hop(length * baseOne) right(mirrored * 144) } } def triangleTwo(depth: Int, length: Double, mirrored: Int): Unit = { if(depth == 0) { setFillColor(cm.blue) forward(length) right(mirrored * 144) forward(length) right(mirrored * 108) hop(length * baseTwo) right(mirrored * 108) } else { hop(length * baseTwo * baseTwo) right(mirrored * 108) triangleTwo(depth - 1, length * baseTwo, mirrored) hop(length * baseTwo) right(mirrored * 180) triangleOne(depth - 1, length * baseTwo, mirrored) left(mirrored * 36) hop(length * baseTwo) right(mirrored * 108) } } def decagon(depth: Int, length: Double) { clear() setBackground(cm.black) setSpeed(superFast) invisible() setPenColor(cm.black) left(90) hop(length) right(180) repeat(5) { triangleTwo(depth, length, 1) triangleTwo(depth, length, -1) right(72) hop(length * baseTwo) left(36) hop(length * baseTwo) left(108) } } clear() toggleFullScreenCanvas() var depth = 0 repeat(7) { decagon(depth, 400) depth = depth + 1 Thread.sleep(3000) }`

My compliments to the Kojo devs for creating an environment that makes it surprisingly easy to do things like this…

]]>