interpretetion of Bresenhams alogorithm

SOURCE CODE :

//programed by Paras Wadher
MCA
Nagpur university

//program for create a display file and interprete it using Bresenhams algo .

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<alloc.h>
#include<process.h>
#include<stdlib.h>

class display
{
public :
int HEIGHT_START,WIDTH_START;//Starting indices of frame buffer
int HEIGHT_END,WIDTH_END;//Ending indices of frame buffer
int WIDTH,HEIGHT;//Dimenssion of frame
int DF_OP[100],DF_X[100],DF_Y[100],FREE,DFSIZE;
int FRAME,START,COUNT,INDEX;
int FRAME_PEN_X,FRAME_PEN_Y,currentX,currentY;
void *buffer;
private :
int op,x,y,nth;

public :
void initgraph1(void);
void area(void);
void erase(void);
void display_file(void);
void put_point(int,int,int);
int get_point(int);
void interprete(int,int);
void do_move(int,int);
void do_line(int,int);
//int max(int,int);
//int min(int,int);
void retrive(int);
void menu(void);
};

void display :: initgraph1(void)
{
int gd = DETECT, gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
}

void display :: area(void)
{
FREE = 0;
HEIGHT_START = 58;
HEIGHT_END = 300;
WIDTH_START = 392;
WIDTH_END = 630;
HEIGHT = HEIGHT_END-HEIGHT_START;
WIDTH = WIDTH_END-WIDTH_START;

FRAME = imagesize(WIDTH_START,HEIGHT_START,WIDTH_END,HEIGHT_END);
buffer = malloc(FRAME);
getimage(WIDTH_START,HEIGHT_START,WIDTH_END,HEIGHT_END,buffer);
setbkcolor(WHITE);
setcolor(RED);
rectangle(WIDTH_START,HEIGHT_START,WIDTH_END,HEIGHT_END);
//erase();
return;
}

void display :: put_point(int op, int x, int y)
{
//cout<<“FREE = “<<FREE;
if(FREE>DFSIZE)
{
cout<<“\n\n\t Display File Full.”;
}
DF_OP[FREE] = op;
DF_X[FREE] = x;
DF_Y[FREE] = y;
FREE++;
//cout<<“free = “<<FREE;
return;
}

int display :: get_point(int nth)
{
INDEX = nth;
if(INDEX>FREE)
{
cout<<“\n\t Wronge Index. Plz. View Display File.”;
}
else
{
op = DF_OP[INDEX];
x = DF_X[INDEX];
y = DF_Y[INDEX];
}
return(DF_OP[INDEX]);
}

void display :: display_file()
{
cout<<“\n\t Your display file is : “;
cout<<“\n\tDF-OP DF-X DF-Y “;
for(int i=0;i<FREE;i++)
{
cout<<“\n\t “<<DF_OP[i]<<” “<<DF_X[i]<<” “<<DF_Y[i];
}
cout<<“\n\n\n”;
}

void display :: interprete(int s, int c)
{
START = s-1;
COUNT = c-1;
if(COUNT >= FREE)
{
cout<<“\n\t Not too much instruction in display file.”;
}
else if(START >= FREE)
{
cout<<“\n\t No instruction at this position.”;
}
else
{
for(INDEX=START;INDEX<=(START+COUNT);INDEX++)
{
int opcode = get_point(INDEX);
if(opcode == 1)
{
do_move(DF_X[INDEX],DF_Y[INDEX]);
}
else if(opcode == 2)
{
do_line(DF_X[INDEX],DF_Y[INDEX]);
}
else
{
cout<<“\n\t OP-CODE Error.”;
}
}
}
}

/*
int display :: max(int a, int b)
{
return ( (a > b) ? a : b);
}

int display :: min(int a, int b)
{
return( (a < b) ? a : b);
}
*/
void display :: do_move(int x,int y)
{
if(currentX == 511 && currentY == 179)
{
FRAME_PEN_X = currentX+(2*x);//max(WIDTH_START,min(WIDTH_END,(x*WIDTH+WIDTH_START)));
FRAME_PEN_Y = currentY-(2*y);//max(HEIGHT_START,min(HEIGHT_END,(y*HEIGHT+HEIGHT_START)));
}
else
{
FRAME_PEN_X = (currentX-(2*(DF_X[INDEX-1]-DF_X[INDEX])));
FRAME_PEN_Y = (currentY+(2*(DF_Y[INDEX-1]-DF_Y[INDEX])));
}
putpixel(currentX,currentY,RED);
setcolor(BLUE);
putpixel(currentX,currentY,RED);
putpixel(FRAME_PEN_X,FRAME_PEN_Y,RED);
currentX = FRAME_PEN_X;
currentY = FRAME_PEN_Y;
}
void display :: do_line(int x,int y)
{
if(currentX == 511 && currentY == 179)
{
FRAME_PEN_X = currentX+(2*x);//max(WIDTH_START,min(WIDTH_END,(x*WIDTH+WIDTH_START)));
FRAME_PEN_Y = currentY-(2*y);//max(HEIGHT_START,min(HEIGHT_END,(y*HEIGHT+HEIGHT_START)));
}
else
{
FRAME_PEN_X = (currentX-(2*(DF_X[INDEX-1]-DF_X[INDEX])));
FRAME_PEN_Y = (currentY+(2*(DF_Y[INDEX-1]-DF_Y[INDEX])));
}

putpixel(currentX,currentY,RED);
setcolor(BLUE);
line(currentX,currentY,FRAME_PEN_X,FRAME_PEN_Y);
currentX = FRAME_PEN_X;
currentY = FRAME_PEN_Y;
}

void display :: retrive(int nth)
{
if(FREE == 0)
{
cout<<“\n\t Nothing in Display file.”;
}
else if(nth>FREE-1)
{
clrscr();
cout<<“\n\t No instruction at this position.”;
}
else
{
DF_OP[nth] = 0;
DF_X[nth] = 0;
DF_Y[nth] = 0;
for(int i = nth;i<=FREE-1;i++)
{
DF_OP[i] = DF_OP[i+1];
DF_X[i] = DF_X[i+1];
DF_Y[i] = DF_Y[i+1];
}
DF_OP[FREE-1] = 0;
DF_X[FREE-1] = 0;
DF_Y[FREE-1] = 0;
FREE–;
}
}

void display :: menu(void)
{
int ch;
cout<<“\n\t menu is…”
<<“\n\t 1. Insert in display file.”
<<“\n\t 2. view display file.”
<<“\n\t 3. Retrive from display file.”
<<“\n\t 4. Interprete display file.”
<<“\n\t 5. Exit.”;
cout<<“\n\t Enter Your Choice : “;
cin>>ch;
switch(ch)
{
case 1 :
{
clrscr();
cout<<“\n\t Enter the opration code : “;
cin>>op;
cout<<“\n\t Enter the co-ordinate for x : “;
cin>>x;
cout<<“\n\t Enter the co-ordinate for y : “;
cin>>y;
if(op==1 || op==2)
{
if(x>60 || y>60)
{
cout<<“\n\t Error : x & y never exceed more than 60.”;
}
else
{
put_point(op,x,y);
clrscr();
}
}
else
{
cout<<“\n\t Error : The OP-CODE must be 1 OR 2”;
}
menu();
}
break;
case 2 :
{
clrscr();
if(FREE == 0)
{
cout<<“\n\t Nothing in display file.”;
}
else
{
display_file();
}
menu();
}
break;
case 3 :
int nth;
{
clrscr();
cout<<“\n\t Enter the location : “;
cin>>nth;
retrive(nth-1);
menu();
}
break;
case 4 :
{
if(FREE == 0)
{
clrscr();
cout<<“\n\t Nothing in display file for interprete.”;
}
else
{
setcolor(RED);
int x1 = 392, x2 = 630, y1 = 58, y2 = 300;
rectangle(x1,y1,x2,y2);
setcolor(YELLOW);
int Xmidpt = (y1+y2)/2; //mid pt. of vertical line
int Ymidpt = x1+((x2-x1)/2);//mid pt. of horizontal line
line(x1,Xmidpt,x2,Xmidpt); //Horizontal axis
line(Ymidpt,y1,Ymidpt,y2); //vertical axis
int x = x1+((x2-x1)/2);
int y = y1+((y2-y1)/2);
putpixel(x,y,RED);
outtextxy(x+5,y+5,”0″);
int scale = 0;
char ch[10]= ” “;
for(int i=0; i<=5;i++)
{
scale = scale + 10;
itoa(scale,ch,10);
putpixel(x+(2*scale),y,RED);
outtextxy(x+(2*scale),y+5,ch);
putpixel(x,y-(2*scale),RED);
outtextxy(x+5,y-(2*scale),ch);
}
scale = 0;
for(i=0;i<=5;i++)
{
scale = scale – 10;
itoa(scale,ch,10);
putpixel(x+(2*scale),y,RED);
outtextxy(x+(2.5*scale),y+5,ch);
putpixel(x,y-(2*scale),RED);
outtextxy(x,y-(2*scale),ch);
}
cout<<“\n\t Starting pen position : (0,0)”;
currentX = x;
currentY = y;
int start = 0, count = 0;
cout<<“\n\tEnter the starting index : “;
cin>>start;
cout<<“\n\tEnter the no. of instruction : “;
cin>>count;
interprete(start,count);
//display_file();
}
menu();
}
break;
case 5 :
{
exit(0);
}
break;
default :
{
cout<<“\n\t Wronge Choice.”;
menu();
}
}

}

void main()
{
clrscr();
display d;
d.initgraph1();
d.area();
d.menu();
getch();
}

2 thoughts on “interpretetion of Bresenhams alogorithm

  1. Hi, good post. I have been thinking about this topic,so thanks for writing. I’ll certainly be subscribing to your site.

Leave a Reply

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