Recursive graphics in Turbo C++ 3.0

This code is using graphics.lib library and requires Borland Graphics Interface. I was using for my tests EGAVGA.bgi copied to from Turbo C++ 3.0 BGI\ folder (if you do not want to copy .bgi file to the folder with your application you can precise the path in initgraph() third argument).

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
/*
x,y - center coordinated
n - rays count
level - subrays level number
*/
void snowflake(int x, int y, int n, int level)  {
	if (level == 4) return; // the level 4 is maximum
	int i, nx, ny;
	float dAngel = 2.0 * M_PI/n ; // agnle step

	for(i=0;i<n;i++) {

	  setcolor(random(14) + 1); // random color
	 
	  // drawing the line
	  moveto(x,y);
	  nx = x + floor((200/(level*level))*sin((float)i*dAngel + level - 1) );
	  ny = y - floor((200/(level*level))*cos((float)i*dAngel + level - 1) );
	  lineto(nx,ny);
	  
	  //recursive call for sublevel
	  snowflake(nx,ny,n,level+1);
	}
}

int main() {
	int gd = DETECT, gm ;
	initgraph(&gd,&gm,""); // graphics initialization, put here the path to BGI if needed
	snowflake(getmaxx()/2,getmaxy()/2,12,1); // drawing our recursive showflake
	closegraph(); // properly closing graphics interface
	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *