# Graphics - Tree algorithm

This is a discussion on Graphics - Tree algorithm within the C Programming forums, part of the General Programming Boards category; I am trying to make a basic tree algorithm using recursion, where the trunk splits into 2 branches, the two ...

1. ## Graphics - Tree algorithm

I am trying to make a basic tree algorithm using recursion, where the trunk splits into 2 branches, the two branches split into other 2, and so on... as they change angle and decrease in size.

To do this, I have the following code, but it does not seem to work. It compiles without any errors, but the lines do not get drawn to the screen the way they were supposed to
Code:
```/* tree algorithm */
#include <stdio.h>
#include <graphics.h>
#include <math.h>

/* function prototype */
int drawPolarLine(double, int);
int wait;

#define	BASE_CASE	5
#define PI	3.1415392

int main()
{
/* automatically detect video driver */
int gdriver = DETECT, gmode;
int screen_width, screen_height;

/* initialize graphic mode, seach for the necesary files in the
turboc directory
*/
initgraph(&gdriver, &gmode, "C:\\TURBOC");

/* get the dimensions of the window to determine its mid point */
screen_width = getmaxx();
screen_height = getmaxy();

/* move -brush- to a certain position */
moveto(screen_width / 2, screen_height / 2);

/* draw main trunk */
linerel(0, 100);

/* draw first banch, to the left */
drawPolarLine(50, 110);

scanf("%d", &wait);
}

int drawPolarLine(double length, int angle)
{
/* change to radians to use sin, cos */
radians = angle / 180 * PI;

/* draw line */

if(length >= BASE_CASE)
{
/* right branch */
drawPolarLine(length / 2, angle + 10);

/* left branch */
drawPolarLine(length / 2, angle - 10);
}
else
return 0;
}```
I am using Turbo C - graphics.h

Thanks.

2. linerel isn't recursive, it just draws from the last point

If you want to back up the tree to draw further lines from higher up the tree, then drawPolarLine needs to call moveto to establish the root point before drawing left and right sub-trees