Bresenhams alogorithm for line

SOURCE CODE :

//programed by Paras Wadher
MCA
Nagpur university

//Program for implement Bresenhams alogorithm for doted line

#include<c:\tc\axis.cpp>//included from

http://www.paraswadher.com/2009/04/draw-axis-on-screen-in-cdraw-axis-on-screen-in-c/
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
class brese
{
private :
int x,x0,x1,x2,xa,xb,y,y0,y1,y2,ya,yb,dx,dy,s1,s2,temp,interc,error;
public :
void initgraph1(void);
void getdata(void);
void bresenhams(void);
};
void brese :: initgraph1(void)
{
int gd = DETECT, gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
}
void brese :: getdata(void)
{
cout<<“\n Enter the co-ordinates for line…”;
cout<<“\n Enter (x1,y1) : “;
cin>>xa>>ya;
cout<<“\n Enter (x2,y2) : “;
cin>>xb>>yb;
}
void brese :: bresenhams(void)
{
x0 = getmaxx()/2;
y0 = getmaxy()/2;

x1 = x0 + (2*xa);
y1 = y0 – (2*ya);
x2 = x0 + (2*xb);
y2 = y0 – (2*yb);

x = x1;
y = y1;

//putpixel(x1,y1,WHITE);
//putpixel(x2,y2,WHITE);
//line(x1,y1,x2,y2);

dx = abs(x2-x1);
dy = abs(y2-y1);

if( (x2-x1) < 0 )
{
s1 = -1;
}
else
{
s1 = 1;
}
if( (y2-y1) < 0 )
{
s2 = -1;
}
else
{
s2 = 1;
}

if(dy>dx)
{
temp = dx;
dx = dy;
dy = temp;
interc = 1;
}
else
{
interc = 0;
}

error = 2 * (dy-dx);

for(int i=1; i<=dx; i++)
{
if(i%5 == 0){}
else
{
putpixel(x,y,RED);
}
while(error>0)
{
if(interc == 1)
{
x = x + s1;
}
else
{
y = y + s2;
}
error = error – (2*dx);
}
if(interc == 1)
{
y = y + s2;
}
else
{
x = x + s1;
}
error = error + (2*dy);
}
}

void main()
{
clrscr();
brese b;
axis a;
b.initgraph1();
a.drawaxis();
b.getdata();
b.bresenhams();
getch();
}

Leave a Reply

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