Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

Fractal Branch

by ilyas usal   | filed under Flash and ActionScript

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

The first thing we need for a recursive function is a function. A function that will draw a branch. I shall call that function makeBranch. I want that function to draw a branch at a given position, in a given direction, I want it to be of a given length, and of a certain diameter. All those requirements will be input as be parameters of the makeBranch function.

function makeBranch(start_x, start_y, length, angle, size) {
 this.lineStyle(size, 0x333333, 100);
 this.moveTo(start_x, start_y);
 var end_x = start_x+length*Math.cos(angle);
 var end_y = start_y+length*Math.sin(angle);
 this.lineTo(end_x, end_y);
}
makeBranch(0, 200, 50, 0, 10);
makeBranch(100, 200, 50, Math.PI/2, 10);
makeBranch(200, 200, 50, Math.PI, 10);
makeBranch(300, 200, 50, -Math.PI/2, 10);
makeBranch(400, 200, 50, Math.PI/4, 10);
makeBranch(500, 200, 50, -Math.PI/6, 10);

[ the makeBranch function: copy and paste into a frame's actions ]

The parameters we were talking about previously:

If you've forgotten how the drawing API works, please refer to the corresponding tutorial. For all the others, let's see how this works:

If you preview your movie, you'll see a few branches, all of them starting from the _y position 200. The only thing we changed, apart from the _x position, is the direction. If you compare the angles and what Flash draws, you'll see that:

If you wonder how this works, the trigonometry tutorial I mentioned before is your best friend. There are more tutorials similar to this, so be sure to check out the ones that follow this.

Ilyas Usal
{Pictures 1 | 2}

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.

Your support keeps this site going! 😇

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--