Thread: Graphics - Tree algorithm

  1. #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
    /* tree algorithm */
    #include <stdio.h>
    #include <graphics.h>
    #include <math.h>
    /* function prototype */
    int drawPolarLine(double, int);
    int radians;
    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 */
    	linerel((length * cos(radians)), (length * sin(radians)));
    	if(length >= BASE_CASE)
    		/* right branch */
    		drawPolarLine(length / 2, angle + 10);
    		/* left branch */
    		drawPolarLine(length / 2, angle - 10);
    		return 0;
    I am using Turbo C - graphics.h


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM
Website Security Test