Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

Chaotic Fractal Tree

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 following animation is a chaotic fractal tree that is different from the previous example in the fractal tree tutorial. Keep clicking in the animation, and you will find various randomly generated branches:

[ a fractal tree...that changes with every click ]

Here, I just threw in as many Math.random() statements as I could: a random number of sub-branches, a random angle, and a random shrinking ratio. Click the scene to regenerate :)

/*** Constants ***/

  max_sub_branch = 4 ;

  max_sub_angle = 3*Math.PI/4 ;

  max_size = 8 ;

  branch_length = 50 ;

  /*** Function ***/

  function makeBranch ( start_x, start_y, length, angle, size ) {

  if ( size > 0 ) {

  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 ) ;

  var sub_branch = random (max_sub_branch - 1) + 2 ;

  var branch_length_dimin = .5 + Math.random()/2 ;

  for ( var i=0; i < sub_branch; i ++ ) {

  var newLength = length * branch_length_dimin ;

  var newAngle = angle + Math.random() * max_sub_angle - max_sub_angle / 2
  ;

  var newSize = size - 1 ;

  makeBranch ( end_x, end_y, newLength, newAngle, newSize ) ;

  }

  }

  }

  /*** Function call ***/

  makeBranch ( 200, 300, branch_length, -Math.PI/2, max_size ) ;

Here, this tutorial is over! I hope you enjoyed it. And remember that fractal trees are just one aspect of what you can do with recursion. I hope that this tutorial will encourage you to discover the others.

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 //--