# Recursion Form

Actionscript:
1. x = y = 10
2. graphics.lineStyle(1,0);
3. drawBox(6);
4.
5. function drawBox(iter:Number=10, count:Number=1, y:Number=0, w:Number=500):void{
6.        if (count <iter){
7.                var width:Number = w / count
8.                for (var i:int = 0; i<count; i++){
9.                    graphics.drawRect(i * width, width * count/w, width, width);
10.                }
11.                count++;
12.                drawBox(iter, count, y, width);
13.        }
14. }

This small snippet just draws this image:

If you have an idea for a short recursive snippet. Feel free to post it in the comments.

This entry was posted in Graphics, functions and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

1. Posted April 5, 2010 at 10:44 am | Permalink

nice snippet ! here is my version of the shortest recursive snippet ever for removing all display objects in a MovieClip:
while(holder_mc.numChildren) {
holder_mc.removeChild(holder_mc.getChildAt(0));
}

2. Posted April 5, 2010 at 11:52 am | Permalink

var x0:int = 275;
var y0:int = 200;

graphics.lineStyle(1, 0);
graphics.moveTo(x0, y0);

drawSpiral(300);

function drawSpiral(max:int, r:Number = 0, a:Number = 0, c:int = 0):void {
graphics.lineTo(Math.cos(a) * r + x0, Math.sin(a) * r + y0);
r += 1;
a += 0.1;

if (++c < max) {
drawSpiral(max, r, a, c);
}
}

3. Posted April 5, 2010 at 1:22 pm | Permalink

when you think recursion, who doesn’t think “Fibonacci”? So here’s a 2 fer 1:

var totalIterations:int = 10;
var baseWidth:Number = 5;
var xp:Number = stage.stageWidth >> 1;
var yp:Number = stage.stageHeight >> 1;

plot(0, 0);

function plot(i:int, c:int):void {
var n:Number = fib(i);
var r:Rectangle = getRect(this);
var nr:Rectangle = new Rectangle();
nr.width = nr.height = baseWidth * n;
graphics.lineStyle(0, 0xFF0000);
if (c == 0) {
xp = r.x || xp;
yp = yp - baseWidth * n;
nr.x = xp;
nr.y = yp;
graphics.moveTo(nr.x + nr.width, nr.y);
graphics.curveTo(nr.x, nr.y, nr.x, nr.y + nr.height);
} else if (c == 1) {
xp = xp + r.width;
yp = r.y || yp;
nr.x = xp;
nr.y = yp;
graphics.moveTo(nr.x, nr.y);
graphics.curveTo(nr.x + nr.width, nr.y, nr.x + nr.width, nr.y + nr.height);
} else if (c == 2) {
yp = yp + r.height;
xp = r.x;
nr.x = xp;
nr.y = yp;
graphics.moveTo(nr.x + nr.width, nr.y);
graphics.curveTo(nr.x + nr.width, nr.y + nr.height, nr.x, nr.y + nr.height);
} else if (c == 3) {
xp = xp - baseWidth * n;
yp = r.y;
nr.x = xp;
nr.y = yp;
graphics.moveTo(nr.x, nr.y);
graphics.curveTo(nr.x, nr.y + nr.height, nr.x + nr.width, nr.y + nr.height);
}

graphics.lineStyle(0, 0×000000);
graphics.drawRect(nr.x, nr.y, nr.width, nr.height);

if (++c > 3) c = 0;

if (++i < totalIterations) plot(i, c);
}

function fib(num:int):Number{
if (num <= 1) return num;
else return fib(num-1)+fib(num-2);
}

4. Posted April 5, 2010 at 3:50 pm | Permalink

Posted on wonderfl:
http://wonderfl.net/code/90a836ddb72c706dbc4d40598e761d253bf491cf

and the code:

stage.quality=StageQuality.LOW;
var iw:Number=500;
var ia:Number=0;
function onframe(event:Event):void {
ia+=.01;
graphics.clear();
go(25, iw);
}
function go(iter:uint, w:Number, i:uint=0):void {
if(i<iter) {
var cx:Number, cy:Number;
cx=cy=250+(iw-w)/2;
var a:Number;
var aspan:Number=(Math.PI*2)/(i+1);
for(var c:uint=0; c<i+1; c++) {
var col:uint=c/iter*0xffffff;
graphics.beginFill(col, .15);
graphics.lineStyle(1,col,.2);
a=c*aspan;
graphics.drawCircle(250+Math.cos(a)*w,250+Math.sin(a)*w,w);
}
go(iter, w*(Math.sin(ia)*.85), i+1);
}
}

// Bye

5. Posted April 6, 2010 at 8:31 am | Permalink

very nice work from everyone… It’s nice to see recursive stuff despite the stack limitations for functions.