A picture of a tree, drawn using fractal or mathematical methods. It is relatively easy to write a computer program to draw a tree when one realizes how self similar a tree is. The twigs on trees look like the branches on which they grow, and the branches look like the trunk. One could write a function "Branch" which simply draws one branch, and then calls copies of itself to make sub-branches on that branch.

In C'ish pseudo-code, a simple 2d example:
--------------


#include "math.h" // for the trig stuff and random number generator

#include ??? // whatever you need to do graphics on your platform

void Branch(int x, int y, float len, float angle)
// x,y are the starting coords on screen,

// len is the branch length in pixels,

// and angle is the angle of the branch in radians.

{

   int color,x2,y2; // branch color and endpoint..

   float childlen; // the length of any children off this branch..

   float childangle; // the offshoot angles of any children


  if(len>3) // if its a long branch, make it brown.

     color=BROWN;

  else

     color=GREEN; // otherwise make it green.

  // draw the 'branches' and 'leaves' as a simple lines.
  // Get more fancy than this, please.

   x2=x+len*cos(angle);
   y2=y-len*sin(angle);

   DrawLine(x,y,x2,y2,color); // draw branch

  if(len>3) // if this branch is long enough to have children...
   {

     childlength=len*(float)(rand()%80+10)/100.0;

     childangle =3.14159/2.0*(float)(rand()%100)/100.0;

     Branch(x2,y2,childlength,angle-childangle);
     Branch(x2,y2,childlength,angle+childangle);

   }

}

void main() // time to plant our tree
{

   // set up your graphics here...

   // ...648x480 pixels, for example..

   Branch(320,480,100,3.14159/2.0); // and get to it!

  Wait_For_Keypress(); // all done.

}

The following PostScript code is probably ugly to the eyes of a seasoned postscript-hacker, but it illustrates the above point. I assume you could send it directly to a ps printer.


%!

% To draw a tree of complexity n:
%       * Draw a trunk.       
%       * If n > 0, draw several smaller trees of
%         complexity n-1 on top of the trunk, at angles.


42 srand

/frand { rand 1024 mod 1024 div } bind def
/peturb { frand 0.6 mul 0.4 add mul } bind def

/trunk {
    newpath
    0 0 moveto
    0 150 translate
    0 0 lineto
    stroke
} bind def

/tree {
    dup 0 gt { %if
        1 sub
        trunk
        0.7 dup scale
        [-45 45] { %forall
            gsave
                peturb rotate
                dup tree
            grestore
        } forall
    } if
    pop
} bind def

300 150 translate
6 setlinewidth
6 tree

Log in or register to write something here or to contact authors.