IFS Tree

I’ve been playing a lot with fractals, iterated function systems, chaos theory, attractors, etc. lately.

Here’s a tree, done in AS2 timeline code. A quick and dirty experiment, but worked well.

[kml_flashembed movie=”http://www.bit-101.com/misc/tree.swf” width=”600″ height=”600″ /]

Here’s the code:

[as]import flash.geom.Point;
import flash.display.BitmapData;

var rules:Array = new Array();
rules[0] = {r:.05, s: .60, t: 0, p: 0, e:0, f:0.0, prob:.1};
rules[1] = {r:.05, s:-.50, t: 0, p: 0, e:0, f:1.0, prob:.1};
rules[2] = {r:.60, s: .50, t: 40, p: 40, e:0, f:0.6, prob:.2};
rules[3] = {r:.50, s: .45, t: 20, p: 20, e:0, f:1.1, prob:.2};
rules[4] = {r:.50, s: .55, t:-30, p:-30, e:0, f:1.0, prob:.2};
rules[5] = {r:.55, s: .40, t:-40, p:-40, e:0, f:0.7, prob:.2};

var p:Point = new Point();
var bitmap:BitmapData = new BitmapData(600, 600, false, 0x6666ff);
attachBitmap(bitmap, 0);

function onMouseDown():Void
{
bitmap.fillRect(bitmap.rectangle, 0x6666ff);
}

function onEnterFrame():Void
{
for(var j:Number = 0; j < 100; j++) { bitmap.setPixel(300 + p.x * 300, 600 - p.y * 300, 0x443322); var rand:Number = Math.random(); for(var i:Number = 0; i < 6; i++) { if(rand < rules[i].prob) { var rule:Object = rules[i]; break; } else { rand -= rules[i].prob; } } p.x *= rule.r; p.y *= rule.s; var cos:Number = Math.cos(rule.t * Math.PI / 180); var sin:Number = Math.sin(rule.p * Math.PI / 180); var x1:Number = p.x * cos - p.y * sin; var y1:Number = p.y * cos + p.x * sin; p.x = x1; p.y = y1; p.x += rule.e; p.y += rule.f; } }[/as] Basically, it starts with a point, (0, 0), which winds up being bottom center. On each iteration, it randomly chooses one of the six rules, which are weighted so that there's a 10 percent chance for the first two, 20 percent for the rest. Then, using those rules, it scales, rotates and translates the position of that point, and plots it. Then it randomly chooses another rule and begins again. Somehow this winds up drawing a pretty damn good lookin' tree. Note that it's doing 100 iterations per frame. Even in AS2, it could support a lot more, but this lets it build up at a nice pace. The concept for this came from a book, Chaos Under Control by David Peak and Michael Frame. I picked up a copy at the library last week and just ordered a used copy on Amazon for a couple of bucks. An older book, but cool stuff in there.

This entry was posted in Flash. Bookmark the permalink.

6 Responses to IFS Tree

  1. Michael says:

    Really interesting.

    Might be nice to see the colours of the dots change according to which step in the fractal you’re currently at – eg, first few steps are brownish and the last few steps are greenish etc.

    I agree with you, though. That’s a damn nice looking tree! 🙂

  2. polyGeek says:

    This time you beat me to it. I was working on the same thing yesterday but got called away to run errands with the wife before finishing. 🙁

    Are we like the proverbial women who show up to the party wearing the same dress? 🙂

  3. kp says:

    Ha. OK, maybe we should coordinate these things. 🙂

  4. kp says:

    Simon. I see what you’re doing there.

Leave a Reply