Cgproject Material
Cgproject Material
• CODE:-
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
void tree(float x,float y,float len,float a,float b)
{
if(len<20)
{
delay(30);
setcolor(14);
circle(x,y,5);
setcolor(100);
circle(x,y,6);
return;
}
float x1,y1;
settextstyle(1,0,1);
setcolor(14);
setcolor(10);
x1=x+len*cos(3.1428*a/180);
y1=y-len*sin(3.1428*a/180);
line(x,y,x1,y1);
tree(x1,y1,len*0.75,a-b,b);
tree(x1,y1,len*0.75,a-b,-b);
}
void main()
{
CG MINI PROJECT
int gd=0,gm,i,rd;
float x=320,y=550,t=140;
clrscr();
initgraph(&gd,&gm,"C://TC//BGI");
for(i=0;i<=3;i++)
{
tree(x+i,y,t,90,30);
tree(x+i,y,t,90,-30);
}
getch();
closegraph();
}
LINE BY LINE EXPLANATION:
1. `#include<conio.h>`: This line includes the conio.h header file, which provides
functions for console input/output operations.
2. `#include<stdlib.h>`: This line includes the stdlib.h header file, which provides
functions for memory allocation, process control, and other utility functions.
3. `#include<math.h>`: This line includes the math.h header file, which provides
mathematical functions like sin, cos, etc.
4. `#include<graphics.h>`: This line includes the graphics.h header file, which provides
functions for graphics programming.
5. `#include<dos.h>`: This line includes the dos.h header file, which provides functions for
working with the DOS operating system.
CG MINI PROJECT
6. `void tree(float x,float y,float len,float a,float b)`: This line defines a function named
"tree" that takes five parameters: x, y, len, a, and b. This function is used to draw a tree
recursively.
7. `if(len<20)`: This line checks if the length of the branch is less than 20. If it is, the code
inside the if block is executed.
10. `circle(x,y,5)`: This line draws a circle with a radius of 5 pixels at the coordinates (x, y).
11. `setcolor(100)`: This line sets the current drawing color to a shade of green.
12. `circle(x,y,6)`: This line draws a circle with a radius of 6 pixels at the coordinates (x, y).
13. `return;`: This line exits the current function and returns to the calling function.
14. `float x1,y1;`: This line declares two variables x1 and y1 of type float.
15. `settextstyle(1,0,1)`: This line sets the text style for drawing text.
16. `setcolor(14)`: This line sets the current drawing color to yellow.
17. `setcolor(10)`: This line sets the current drawing color to a shade of green.
18. `x1=x+len*cos(3.1428*a/180)`: This line calculates the new x-coordinate (x1) based on
the current x-coordinate (x), length (len), and angle (a).
CG MINI PROJECT
19. `y1=y-len*sin(3.1428*a/180)`: This line calculates the new y-coordinate (y1) based on
the current y-coordinate (y), length (len), and angle (a).
20. `line(x,y,x1,y1)`: This line draws a line from the coordinates (x, y) to (x1, y1).
21. `tree(x1,y1,len*0.75,a-b,b)`: This line recursively calls the tree function with updated
parameters to draw a smaller branch on the left side.
22. `tree(x1,y1,len*0.75,a-b,-b)`: This line recursively calls the tree function with updated
parameters to draw a smaller branch on the right side.
23. `void main()`: This line defines the main function, which is the entry point of the
program.
24. `int gd=0,gm,i,rd;`: This line declares four variables gd, gm, i, and rd of type int.
25. `float x=320,y=550,t=140;`: This line declares three variables x, y, and t of type float
and initializes them with specific values.
27. `initgraph(&gd,&gm,"C://TC//BGI")`: This line initializes the graphics system with the
specified graphics driver, graphics mode, and path.
28. `for(i=0;i<=3;i++)`: This line starts a for loop that iterates four times.
29. `tree(x+i,y,t,90,30)`: This line calls the tree function to draw a tree branch with specific
parameters.
30. `tree(x+i,y,t,90,-30)`: This line calls the tree function to draw another tree branch with
different parameters.
CG MINI PROJECT
31. `getch()`: This line waits for a key press.
33. `}`: This line marks the end of the main function.