Sofox Central


Welcome to my page of stuff!

Yeah, not too much stuff yet...

Here's a fun thing:

If you don't have Canvas support on your browser, you'll see this you poor, poor, creature. Maybe go and download Firefox or something.

What you see there (presuming your browser supports HTML5 Canvas), has all been automatically drawn by your own web browser.

The code I wrote to generate that image is here:

function drawEquiTriangle(x, y, radius,recursionAmount){ var hside = (radius * 2.3094010767585034)/2; if(0==recursionAmount){ context.beginPath(); context.moveTo(x,y-radius); context.lineTo(x+hside, y+radius); context.lineTo(x-hside, y+radius) context.closePath(); context.fill(); }else{ drawEquiTriangle(x,y-radius/2, radius/2, recursionAmount-1); drawEquiTriangle(x-hside/2,y+radius/2, radius/2, recursionAmount-1); drawEquiTriangle(x+hside/2,y+radius/2, radius/2, recursionAmount-1); } } //Called with: drawEquiTriangle(200,175,150,7); in this example.

It's recursive. This means that it keeps calling itself. Each time it tries to draw a triangle, it instead draws the 3 filled in sections of the triangle, and when it tries to draw each of those triangle sections, it instead tries to draw the 3 sections of THAT triangle.

This could go on forever, or more likely until your browser runs out of memory or throws a recursion error, but for the purposes of displaying this, I put a limit of 7 repeats so we'd actually be able to see something.

I think it's cool, a good example of how a simple function can have an interesting effect. Did you know a simple line recursive function only three lines long can solve any Tower of Hanoi problem? (It's that puzzle with the 3 pegs and the disc of different sizes.)

Recursion is brilliant, it's even better than itself! (I could have resisted that, I didn't.)