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…

]]>PSD to Wordpress Expert ]]>

pant dot lalit at gmail ]]>

All of it remained till POC sometimes done in Java canvas, sometimes in html5 canvas. I wish I could spend more time to create professional visualization libraries.

With this tool, may be I can spend some time building more serious visual illustrations.

I could share some POCs of html5 canvas on your personal email.

]]>Looks like it may prove to be a good simulation tool for teaching Engineering subjects

I have tried to make Kojo powerful enough to support this. I'll be very interested in hearing about any efforts you make in this area.

]]>This is a great platform you have created. Great work buddy!

I am planning to explore it further in my free time.

Looks like it may prove to be a good simulation tool for teaching Engineering subjects like Engineering Drawing, Structural Mechanics, etc.

` ``clear() setBackground(black) setPenColor(red) setFillColor(blue) for (i <- 1 to 5) { forward(200) right(144) }`

**Kojo Design**

` ``clear() setBackground(black) setPenColor(red) setFillColor(blue) for (i <- 1 to 12) { for (i <- 1 to 4) { forward(50) right(90) } penUp() right(180 - 180 * 10 / 12) forward(50) penDown() }`

Suggestion: make overrides of setXxxColor to accept directly 0xAABBCC or 0xAABBCCDD or r, g, b or a, r, g, b

Or maybe overload the color method (It's good to have the input type of setXxxColor be a color)

Accepting some HSB color (via conversion function?) would be useful too.

I'll need to play with that. Staging has all of this stuff, btw…

]]>In line-by-line mode, it just stops at the first problem, and never gets to the internal repeat

Yes, silly me, the code above has no var in it!

Contributor Agreement - will do

Just create a Color with an alpha value e.g. setFillColor(new Color(0, 0, 255, 128))

Ah, tried color() with four parameters, not quite the same thing.

Suggestion: make overrides of setXxxColor to accept directly 0xAABBCC or 0xAABBCCDD or r, g, b or a, r, g, b

Works well in Processing and simplifies the coding.

Accepting some HSB color (via conversion function?) would be useful too.

Just do number.toHexString

Thanks! I expected something that simple… ^_^

]]>Level up! To recursive functions, and beyond. Kojo fits well with geometrical fractals, so here is the Sierpinski square:

` ``clear invisible setAnimationDelay(4) setPenColor(blue) setPenThickness(1) setFillColor(color(0x77FFAA)) def relativeJumpTo(dx: Int, dy: Int) = jumpTo(position.x + dx, position.y + dy) def doSquare(size: Int): Unit = { val diag = size * 0.707 // sqrt(2)/2 penUp setHeading(45) forward(diag) penDown setHeading(-90) repeat(4) { forward(size); right } penUp setHeading(-135) forward(diag) } def doSquares(level: Int, size: Int): Unit = { if (level == 0) { doSquare(size) return } val s = size / 3 for (i <- -1 until 2) { for (j <- -1 until 2) { if (!(i == 0 & j == 0)) { relativeJumpTo(s * i, s * j) doSquares(level -1, s) relativeJumpTo(-s * i, -s * j) } } } } //doSquare(500) doSquares(3, 500)`

Ah, this is quite unexpected, first time I meet a language enforcing the brace style! ;-)

Just to clarify, this is more of a scala interpreter thing (when it is fed input a line at a time) than a Scala thing. If you use the Scala compiler to compile code of this nature, it will work fine.

One more thing in the mix here is the use of a 'var' in your script. If you don't use vars, Kojo is able to submit the whole script (in one batch, not line by line) to the interpreter, and things work fine.

I suppose the rule applies to current scope, since the internal repeat is OK.

In line-by-line mode, it just stops at the first problem, and never gets to the internal repeat

Note: I place all my code under the zlib/libpng license, but small experiments like the one above are just put

in public domain: if you find something of interest, feel free to add it to Kojo examples.

Cool.

Now is probably a good time to tell you that if you wish to contribute more substantial code to Kojo, I will request you to sign the Contributor Agreement.

What about transparency? Is it possible in Kojo or just not yet?

Just create a Color with an alpha value e.g. setFillColor(new Color(0, 0, 255, 128))

For Staging, you can also use the rgba color maker.

And is there a convenient shortcut in Scala to display a number in hexa or do we have to use something like Message.format() or similar?

I missed out on this yesterday. Just do number.toHexString

]]>` ``val ROUND = 360.0 val REPEAT_NB = 7 val ANGLE = ROUND / REPEAT_NB val PENTA_ANGLE = ROUND / 5 val SIDE_LENGTH = 150 clear setAnimationDelay(42) setPenThickness(11) for (n <- 0 until REPEAT_NB) { jumpTo(0, 0) setHeading(n * ANGLE) setPenColor(getColor(n)) repeat (3) { forward(SIDE_LENGTH) turn(PENTA_ANGLE) } forward(SIDE_LENGTH / 4) } penUp; home; penDown def lerp(start: Double, stop: Double, amount: Double): Double = { start + (stop - start) * amount } def getColor(n: Int): Color = { val amount = n.toDouble / REPEAT_NB color(0x80, lerp(0xF0, 0x80, amount).toInt, lerp(0x80, 0xF0, amount).toInt) }`

I understand it is a combination of unfortunate mix, Scala's line ending interpretation, repeat being a method and not a keyword, the interpreter mode, etc.

I suppose the rule applies to current scope, since the internal repeat is OK.

Note: I place all my code under the zlib/libpng license, but small experiments like the one above are just put in public domain: if you find something of interest, feel free to add it to Kojo examples.

I updated the code above to have better contrast on the first three pentagons.

What about transparency? Is it possible in Kojo or just not yet?

And is there a convenient shortcut in Scala to display a number in hexa or do we have to use something like Message.format() or similar?

OK, my first baby steps in Kojo… Purely imperative for now.

Nice and pretty!

There is something that puzzles me: if I replace the for loop with:

var n = 0

repeat (5)

I get an error:

You've stumbled into an edge case within Kojo:

- if a script uses vars, it is interpreted line by line. Your script has a var, so this applies.

- the opening brace of your repeat command starts on the next line. When the scala interpreter encounters the line with the repeat command, it thinks that the code block for repeat is missing.

The Fix - just pull the opening brace to the line with the repeat command:

repeat (5) {