Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
Practical Paper
Industrial Training
Fractal Tree
Like a Tree Data Structure, every tree has its branches and leafs. To access the sub branches or its leafs (nodes), we use recursion. In the similar manner for drawing a fractal tree, recursion is implemented for drawing the branches and its leafs.
The following program generates a Fractal tree using the recursion:
#include <math.h>
#include <stdlib.h>
main( )
{
int gd = DETECT, gm ;
int drawtree ( int x1, int y1, float a, float l, float f, int n ) ;
initgraph ( &gd, &gm, "c:\\tc\\bgi" ) ;
drawtree ( 280, 350, 270, 60, 90, 6 ) ;
getch( ) ;
closegraph( ) ;
restorecrtmode( ) ;
}
drawtree ( int x1, int y1, float a, float l, float f, int n )
{
int i, num = 3, x2, y2 ;
float spreadratio = 0.75, lenratio = 0.9 ;
float delang, ang ;
if ( n > 0 )
{
switch ( n )
{
case 1:
setcolor ( LIGHTGREEN ) ;
break ;
case 2:
setcolor ( GREEN ) ;
break ;
case 3:
setcolor ( RED ) ;
break ;
case 4:
setcolor ( BROWN ) ;
break ;
default:
setcolor ( DARKGRAY ) ;
break ;
}
x2 = x1 + l * cos ( 0.0174 * a ) ;
y2 = y1 + l * sin ( 0.0174 * a ) ;
for ( i = 0 ; i < n ; i++ )
{
line ( x1 + i, y1, x2 + i, y2 ) ;
line ( x1 - i, y1, x2 - i, y2 ) ;
}
randomize( ) ;
num = random ( 5 ) + 2 ;
if ( num > 1 )
delang = f / ( num - 1.0 ) ;
else
delang = 0.0 ;
ang = a - f / 2.0 - delang ;
for ( i = 1 ; i <= num ; i++ )
{
ang += delang ;
delay ( 10 ) ;
drawtree ( x2, y2, ang , l * lenratio, random ( 90 ) + 1 , n - 1 ) ;
}
}
else
{
setfillstyle ( SOLID_FILL, random ( 15 ) + 1 ) ;
fillellipse ( x1, y1, 3, 6 ) ;
}
}
Here we are calling a function drawtree( ) to it we are passing first two parameters as the x and y coordinates from which the tree is being started. Then the third parameter is the angle at which you want to draw the tree branch. Then the fourth parameter is the spread ratio of the length of branch of each level, means as you go up the length of branch decreases by 40%. Then the fifth parameter is the angle between the each branch at a particular level. And the last parameter is the levels of branches of tree.
Now when the drawtree( ) function gets called firstly if the number of branches are not less or equal to one then a color is selected and the x and y coordinates are calculated for the end point of a particular branch. Next the braches of tree are drawn through a loop and it is drawn thicker at the base and as we go up it becomes thinner and thinner. A random number is generated for number of branches and the delta angle is calculated so that the total angle is divided into those many parts. drawtree( ) function has to be called recursively each time for the number of branches. Finally in the else block, a fruit is drawn on the tip of last branch, means an ellipse is drawn.