Friday, March 25, 2011

OpenGL Program for sine wave


// tree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include "math.h"
#include <windows.h>


//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include "glut.h"
#endif

using namespace std;

float rotate =0.0;

float start = 0.0;
void drawSinWave()
{
float radius = 7.5;
float x2=0,y2,cx,cy,fx,fy;
float cos_y,cache_cos_y;
int cache = 0;
start += 1.0;
if(start >360) {
start = 0;
}
glBegin(GL_LINES);
float angle = 0;
for(angle=start; ; angle+=1.0) {
if(angle>1020) {
break;
angle = 0.0;
}
float rad_angle = angle * 3.14 / 180;
x2 = x2+0.1;
y2 = radius * sin((double)rad_angle);
cos_y = radius * sin((double)-rad_angle);
if (cache) {
glVertex2f(cx,cy);
glVertex2f(x2,y2);

glVertex2f(cx,cache_cos_y);
glVertex2f(x2,cos_y);

} else {
fx = x2;
fy = y2;
}
cache = 1;
cx = x2;
cy = y2;
cache_cos_y = cos_y;
}
glEnd();
}

void display(void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLineWidth(10);
glLoadIdentity();
glTranslatef(-40,0,-30);
glColor3f(1,0,0);

drawSinWave();
glutSwapBuffers();
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (1200, 800);
glutInitWindowPosition (0, 0);
glutCreateWindow ("A Sine wave");

glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();
return 0;
}

Thursday, March 24, 2011

shannon fano coding program in C


This is program for shanno fano coding .
It is a technique for constructing a prefix code based on a set of symbols and their probabilities (estimated or measured). It is suboptimal in the sense that it does not achieve the lowest possible expected code word length like Huffman coding; however unlike Huffman coding, it does guarantee that all code word lengths are within one bit of their theoretical ideal − logP(x).

// shannon.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdio.h>

int g_level = 0;
void shannon(int start,int end,int arr[20], char code[20][20],int level) {
int i=start;
int j=end;
int isum = arr[i],jsum = arr[j];

if(level>g_level) {
g_level = level;
}
while(i<(j-1)) {
while(isum>jsum && i<(j-1)) {
j--;
jsum += arr[j];
}
while(isum<jsum && i<(j-1)) {
i++;
isum += arr[i];
}
}

if(i==start) {
code[start][level]='0';
} else if((i-start)>=1) {
for(int k=start;k<=i;++k)
code[k][level] = '0';

shannon(start,i,arr,code,level+1);
}
if(j==end) {
code[end][level]='1';
} else if((end-j)>=1) {
for(int k=j;k<=end;++k)
code[k][level] = '1';

shannon(j,end,arr,code,level+1);
}
}

int _tmain(int argc, _TCHAR* argv[])
{
int arr[20];
int n,i,j;
printf("Enter the number of symbols :");
scanf("%d",&n);

// Take the symbols and sort them as user enters them using insertion sort
for(i=0; i<n; ++i) {
int s;
printf("Enter symbol frquency : ");
scanf("%d",&s);
j=i;
while(j && arr[j-1]<s) {
arr[j] = arr[j-1];
j--;
}
arr[j] = s;
}

char code[20][20];

for(i=0; i<n; ++i) {
// Mark row as invalid
for(j=0;j<n;j++) {
code[i][j] = 'X';
}
}

shannon(0,n-1,arr,code,0);

printf("\n\n DATA CODE\n");
// Print the data and code
for(i=0; i<n; ++i) {
printf("\n %4d : ",arr[i]);
for(j=0; j<=g_level; j++) {
if(code[i][j]!='X')
printf("%c",code[i][j]);
}
}

printf("\n\n");
return 0;
}


Wednesday, March 23, 2011

Huffman coding with Opengl


// THis program will output the huffman tree in graphics window and will show the huffman code in command window

// huff.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include "math.h"

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include "glut.h"
#endif

typedef struct node {
char name[20]; // name of the node
float data;
struct node* right;
struct node* left;
struct node* parent;
struct node* next;
}NODE;

NODE* root = NULL;

using namespace std;

void display_list(NODE* head) {
if(head) {
cout<<"\nList contents:\n";
NODE* temp = head;
while(temp) {
cout<<"Node : "<<temp->name<<" probability:"<<temp->data<<endl;
temp = temp->next;
}
} else {
cout<<"\nEmpty List";
}
}

void find_smallest(NODE* head,NODE** smallest1, NODE** smallest2) {
*smallest1 = NULL;
*smallest2 = NULL;
NODE* t = head;
while(t) {
// Skip the nodes who has parent
if(t->parent) {
t = t->next;
continue;
}

if(!*smallest1) {
*smallest1 = t;
} else if(!*smallest2) {
*smallest2 = t;
} else if(t->data < (*smallest1)->data) {
if ((*smallest1)->data < (*smallest2)->data) {
*smallest2 = *smallest1;
}
*smallest1 = t;
} else if(t->data < (*smallest2)->data) {
*smallest2 = t;
} else {
// Do nothing
}

if(*smallest1 && *smallest2) {
if((*smallest1)->data > (*smallest2)->data) {
NODE* temp = *smallest1;
*smallest1 = *smallest2;
*smallest2 = temp;
}
}
t = t->next;
}
}

void add_element(NODE** head,NODE** e) {
if(!*head) {
*head = *e;
} else {
NODE* t = *head;
while(t->next!=NULL) {
t = t->next;
}
t->next = *e;
}
}

void tree_traversal(NODE* root,char code[20]) {
if(!root) {
return;
}
//cout<<"\n NODE : "<<root->name<<" prob: "<<root->data;
if(root->left==NULL && root->right==NULL) {
cout<<" \nNODE : "<<root->name<<" prob : "<<root->data<<" code: "<<code;
return;
}
int len = strlen(code);
if(root->left) {
code[len]='1';
code[len+1]='\0';
tree_traversal(root->left,code);
}
if(root->right) {
code[len]='0';
code[len+1]='\0';
tree_traversal(root->right,code);
}
}

void drawCircle(float segments, float radius, float sx, float sy)
{
float theta = 2 * 3.1415926 / segments;
float tan_factor = tanf(theta);
float radial_factor = cosf(theta);
float x = radius;
float y = 0;

int cache_pt = 0;
double cache_x;
double cache_y;

glBegin(GL_LINES);
for (int ii = 0; ii < segments; ii++) {
if(!cache_pt) {
cache_x = x+sx;
cache_y = y+sy;
cache_pt = 1;
} else {
//glVertex2f(cache_x,cache_y);
glVertex2f(x + sx, y + sy);
cache_x = x+sx;
cache_y = y+sy;
}
float tx = -y;
float ty = x;
x += tx * tan_factor;
y += ty * tan_factor;
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}

void draw_line(float x1,float y1,float x2, float y2) {
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}

void draw_text(char* text,float x, float y) {
GLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_24;
glRasterPos3f(x, y, 0);
for (int i = 0; text[i] != '\0'; i++){
glutBitmapCharacter(font_style, text[i]);
}
}

void drawNode(NODE* t_root,float x1,float y1,int level) {
if (t_root==NULL) {
return;
}
float segments = 25;
float radius = 1.0;
float left_angle = 245;
float right_angle = 115;
float branch_length = 12 - level*2.5;
float angle_change = 20;

// Draw the current circle

drawCircle(segments,radius,x1,y1);

char buff[5];
//itoa(t_root->name,buff,10);
draw_text(t_root->name,x1,y1);

if(t_root->left) {
// Draw the Left circle
float angle = left_angle - level*angle_change;
double radian = angle*3.14/180;
float m = (double)tan((double)radian);
float x2 = x1 + branch_length * sin((double) radian);
float y2 = y1 + branch_length * cos((double) radian);
drawNode(t_root->left,x2,y2,level+1);
draw_line(x1,y1,x2,y2);

float x3 = (x1+x2) / 2;
float y3 = (y1+y2) / 2;
char label[2];
label[0]='1';
label[1]='\0';
draw_text(label,x3,y3);
}

if(t_root->right) {
// Draw the Right circle
float angle = right_angle + level*angle_change;
float radian = angle*3.14/180;
float m = (double)tan((double)radian);
float x2 = x1 + branch_length * sin((double) radian);
float y2 = y1 + branch_length * cos((double) radian);
drawNode(t_root->right,x2,y2,level+1);
draw_line(x1,y1,x2,y2);

float x3 = (x1+x2) / 2;
float y3 = (y1+y2) / 2;
char label[2];
label[0]='0';
label[1]='\0';
draw_text(label,x3,y3);

}

}


void display(void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,10,-30);
glColor3f(1,1,1);

drawNode(root,0,0,0);

glutSwapBuffers();
}


void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int _tmain(int argc, char** argv)
{
NODE* list = NULL;
float arr[10];
int n;
cout<<"Enter n:";
cin>>n;
for(int i=0; i<n; i++) {
cin>>arr[i];
// Create a new node and append it to the list
NODE* current = new NODE;
current->data = arr[i];
current->next = NULL;
current->left = NULL;
current->right = NULL;
current->parent = NULL;
current->name[0] = 65+i; current->name[1] = '\0';

// Appending the node to the list
add_element(&list,&current);
}

// Display the nodes entered by the user along with their probabilities
display_list(list);


while(1) {

// Find the 2 smallest elements from the list
NODE* smallest1 = NULL;
NODE* smallest2 = NULL;
find_smallest(list,&smallest1,&smallest2);

if(!smallest2) {
root = smallest1;
break;
}

float small1 = smallest1->data;
float small2 = smallest2->data;
NODE* current = new NODE;
current->data = small1 + small2;
current->left = smallest1;
current->right = smallest2;
current->parent = NULL;
current->next = NULL;
strcpy(current->name,smallest1->name);
strcat(current->name,smallest2->name);

smallest1->parent = current;
smallest2->parent = current;

add_element(&list,&current);

}

cout<<"\n\nHuffman code :\n\n";
char code[20];
code[0]='\0';
tree_traversal(root,code);

// OPENGL Drawing functions
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (1200, 800);
glutInitWindowPosition (0, 0);
glutCreateWindow ("A Binary search tree");

// Register function pointers to the drawing framework
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();

cout<<endl;
return 0;
}


Programming in OpenGL

This is for All my dear friends who are interested in doing programming in openGL.

All you need is Visual studio. and library files for openGL.

1. Create a new project and select win32 cosole Application .
2. Copy any of my program as it is into the main source file
3. Search for glut.h and glut32.lib and glut32.dll on internet and copy them in your windows folders. For example you can copy glut32.dll and glut32.lib in your windows system32 folder. you can copy glut.h in your program files/visual studio/vc/include folder etc.

4. Now compile and run your program.

Now start reading some nice book of openGL. Understand the concepts and write small small programs.

Welcome to the amazing world of openGL :-)

circle drawing in openGL


// tree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include "math.h"

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include "glut.h"
#endif

using namespace std;


void drawCircle(float radius, float x1, float y1)
{
float angle = 0;
float x2,y2,cx,cy,fx,fy;
int cache = 0;
glBegin(GL_LINES);
for (angle = 0; angle < 360; angle+=1.0) {
float rad_angle = angle * 3.14 / 180;
x2 = radius * sin((double)rad_angle);
y2 = radius * cos((double)rad_angle);
if (cache) {
glVertex2f(cx,cy);
glVertex2f(x2,y2);
} else {
fx = x2;
fy = y2;
}
cache = 1;
cx = x2;
cy = y2;
}
glVertex2f(x2,y2);
glVertex2f(fx,fy);

glEnd();
}

void display(void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-10,0,-30);
glColor3f(1,0,0);
drawCircle(10,0,0);
glutSwapBuffers();
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (1200, 800);
glutInitWindowPosition (0, 0);
glutCreateWindow ("A Pendulum");

glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();
return 0;
}

Program for huffman coding

// huff.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include

typedef struct node {
char name[20]; // name of the node
float data;
struct node* right;
struct node* left;
struct node* parent;
struct node* next;
}NODE;

using namespace std;

void display_list(NODE* head) {
if(head) {
cout<<"\nList contents:\n";
NODE* temp = head;
while(temp) {
cout<<"Node : "<name<<" probability:"<data< temp = temp->next;
}
} else {
cout<<"\nEmpty List";
}
}

void find_smallest(NODE* head,NODE** smallest1, NODE** smallest2) {
*smallest1 = NULL;
*smallest2 = NULL;
NODE* t = head;
while(t) {
// Skip the nodes who has parent
if(t->parent) {
t = t->next;
continue;
}

if(!*smallest1) {
*smallest1 = t;
} else if(!*smallest2) {
*smallest2 = t;
} else if(t->data < (*smallest1)->data) {
if ((*smallest1)->data < (*smallest2)->data) {
*smallest2 = *smallest1;
}
*smallest1 = t;
} else if(t->data < (*smallest2)->data) {
*smallest2 = t;
} else {
// Do nothing
}

if(*smallest1 && *smallest2) {
if((*smallest1)->data > (*smallest2)->data) {
NODE* temp = *smallest1;
*smallest1 = *smallest2;
*smallest2 = temp;
}
}
t = t->next;
}
}

void add_element(NODE** head,NODE** e) {
if(!*head) {
*head = *e;
} else {
NODE* t = *head;
while(t->next!=NULL) {
t = t->next;
}
t->next = *e;
}
}

void tree_traversal(NODE* root,char code[20]) {
if(!root) {
return;
}
//cout<<"\n NODE : "<name<<" prob: "<data;
if(root->left==NULL && root->right==NULL) {
cout<<" \nNODE : "<name<<" prob : "<data<<" code: "< return;
}
int len = strlen(code);
if(root->left) {
code[len]='1';
code[len+1]='\0';
tree_traversal(root->left,code);
}
if(root->right) {
code[len]='0';
code[len+1]='\0';
tree_traversal(root->right,code);
}
}


int _tmain(int argc, _TCHAR* argv[])
{
NODE* list = NULL;
float arr[10];
int n;
cout<<"Enter n:";
cin>>n;
for(int i=0; i cin>>arr[i];
// Create a new node and append it to the list
NODE* current = new NODE;
current->data = arr[i];
current->next = NULL;
current->left = NULL;
current->right = NULL;
current->parent = NULL;
current->name[0] = 65+i; current->name[1] = '\0';

// Appending the node to the list
add_element(&list,¤t);
}

// Display the nodes entered by the user along with their probabilities
display_list(list);

NODE* root = NULL;

while(1) {

// Find the 2 smallest elements from the list
NODE* smallest1 = NULL;
NODE* smallest2 = NULL;
find_smallest(list,&smallest1,&smallest2);

if(!smallest2) {
root = smallest1;
break;
}

float small1 = smallest1->data;
float small2 = smallest2->data;
NODE* current = new NODE;
current->data = small1 + small2;
current->left = smallest1;
current->right = smallest2;
current->parent = NULL;
current->next = NULL;
strcpy(current->name,smallest1->name);
strcat(current->name,smallest2->name);

smallest1->parent = current;
smallest2->parent = current;

add_element(&list,¤t);
//cout<<"\n\n LIST: \n\n";
//display_list(list);
}

cout<<"\n\nHuffman code :\n\n";
char code[20];
code[0]='\0';
tree_traversal(root,code);

cout< return 0;
}

OpenGL program for Pendulum

// tree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
#include //Needed for "exit" function
#include "math.h"

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include
#include
#else
#include "glut.h"
#endif

using namespace std;


float rot = 0;
float angle = 135;
float inc = 1.0;

void drawCircle(float segments, float radius, float sx, float sy)
{
float theta = 2 * 3.1415926 / segments;
float tan_factor = tanf(theta);
float radial_factor = cosf(theta);
float x = radius;
float y = 0;

int cache_pt = 0;
double cache_x;
double cache_y;

glBegin(GL_LINES);
for (int ii = 0; ii < segments; ii++) {
if(!cache_pt) {
cache_x = x+sx;
cache_y = y+sy;
cache_pt = 1;
} else {
//glVertex2f(cache_x,cache_y);
glVertex2f(x + sx, y + sy);
cache_x = x+sx;
cache_y = y+sy;
}
float tx = -y;
float ty = x;
x += tx * tan_factor;
y += ty * tan_factor;
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}


void drawCircles(float x1,float y1,double angle) {
float segments = 25;
float radius = 1.0;
// Draw the original circle
drawCircle(segments,radius,x1,y1);

// Draw the pendulum circle
double radian = angle*3.14/180;
float m = (double)tan((double)radian);
float y2 = 10 * cos((double) radian);
float x2 = 10 * sin((double) radian);
drawCircle(segments,radius,x2,y2);

glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();

}

void display(void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-10,0,-30);
glColor3f(1,1,1);

if (angle>225) {
angle = 225;
inc = -inc;
}
if (angle<135) {
angle = 135;
inc = -inc;
}
angle += inc;
drawCircles(0,0,angle);
glutSwapBuffers();

}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (1200, 800);
glutInitWindowPosition (0, 0);
glutCreateWindow ("A Pendulum");

glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();
return 0;
}

Binary search tree using opengl


// tree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include
#include //Needed for "exit" function
#include "math.h"

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include
#include
#else
#include "glut.h"
#endif

using namespace std;

typedef struct treenode {
int data;
struct treenode* left;
struct treenode* right;
struct treenode* parent;
}TREENODE;

TREENODE* root = NULL;


void add_node(TREENODE** t_root, TREENODE** current) {

if(*t_root==NULL) {
*t_root = *current;
return;
}
if((*current)->data < (*t_root)->data) {
if((*t_root)->left==NULL) {
(*t_root)->left = (*current);
} else {
add_node(&((*t_root)->left),current);
}
} else {
if((*t_root)->right==NULL) {
(*t_root)->right = *current;
} else {
add_node(&((*t_root)->right),current);
}
}
}

void accept_tree(){
int n;
cout<<"\nCREATING BST: (enter 0 to finish)\n";
while(1) {
fflush(stdin);
cout<<"\nEnter node value: ";
cin>>n;
if(n==0) {
break;
}
TREENODE* current = new TREENODE;
current->data = n;
current->right = NULL;
current->left = NULL;
current->parent = NULL;

add_node(&root,¤t);
}
}

void inorder(TREENODE* root) {
if(root!=NULL) {
inorder(root->left);
cout<data;
inorder(root->right);
}
}

void preorder(TREENODE* root) {
if(root!=NULL) {
cout<data;
preorder(root->left);
preorder(root->right);
}
}

void postorder(TREENODE* root) {
if(root!=NULL) {
postorder(root->left);
postorder(root->right);
cout<data;
}
}


void drawCircle(float segments, float radius, float sx, float sy)
{
float theta = 2 * 3.1415926 / segments;
float tan_factor = tanf(theta);
float radial_factor = cosf(theta);
float x = radius;
float y = 0;

int cache_pt = 0;
double cache_x;
double cache_y;

glBegin(GL_LINES);
for (int ii = 0; ii < segments; ii++) {
if(!cache_pt) {
cache_x = x+sx;
cache_y = y+sy;
cache_pt = 1;
} else {
//glVertex2f(cache_x,cache_y);
glVertex2f(x + sx, y + sy);
cache_x = x+sx;
cache_y = y+sy;
}
float tx = -y;
float ty = x;
x += tx * tan_factor;
y += ty * tan_factor;
x *= radial_factor;
y *= radial_factor;
}
glEnd();
}

void draw_line(float x1,float y1,float x2, float y2) {
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}

void draw_text(char* text,float x, float y) {
GLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_24;
glRasterPos3f(x, y, 0);
for (int i = 0; text[i] != '\0'; i++){
glutBitmapCharacter(font_style, text[i]);
}
}

void drawNode(TREENODE* t_root,float x1,float y1,int level) {
if (t_root==NULL) {
return;
}
float segments = 25;
float radius = 1.0;
float left_angle = 245;
float right_angle = 115;
float branch_length = 12 - level*2.5;
float angle_change = 20;

// Draw the current circle

drawCircle(segments,radius,x1,y1);

char buff[5];
itoa(t_root->data,buff,10);
draw_text(buff,x1,y1);

if(t_root->left) {
// Draw the Left circle
float angle = left_angle - level*angle_change;
double radian = angle*3.14/180;
float m = (double)tan((double)radian);
float x2 = x1 + branch_length * sin((double) radian);
float y2 = y1 + branch_length * cos((double) radian);
drawNode(t_root->left,x2,y2,level+1);
draw_line(x1,y1,x2,y2);
}
if(t_root->right) {
// Draw the Right circle
float angle = right_angle + level*angle_change;
float radian = angle*3.14/180;
float m = (double)tan((double)radian);
float x2 = x1 + branch_length * sin((double) radian);
float y2 = y1 + branch_length * cos((double) radian);
drawNode(t_root->right,x2,y2,level+1);
draw_line(x1,y1,x2,y2);
}

}


void display(void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,10,-30);
glColor3f(1,1,1);

drawNode(root,0,0,0);

glutSwapBuffers();
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}


int main (int argc, char **argv) {

accept_tree();
inorder(root);

// OPENGL Drawing functions
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (1200, 800);
glutInitWindowPosition (0, 0);
glutCreateWindow ("A Binary search tree");

// Register function pointers to the drawing framework
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutMainLoop ();


return 0;
}