To draw a line by using Bresenham’s line drawing algorithm

0 51

to draw a line by using Bresenham’s line drawing algorithm

// modified version checks slope sign and function realization

#include

#include

#include

#include

#include

void func(int, int, int, int);

void main()

{

int gd=DETECT, gm;

initgraph(&gd, &gm, “c:\\tc\\bgi”);

int x1, y1, x2, y2, dx, dy, p;

cout<

cin>>x2>>y2;

dx= abs(x2-x1);

dy= abs(y2-y1);

int slope= dy/dx;

p=(2*dy-dx);

int x=x1;

int y=y1;

if (slope<1)

func(x1,y1, x2, y2);

else

{

int tempx1=y1;

int tempy1=x1;

int tempx2=y2;

int tempy2=x2;

func(tempx1, tempy1, tempx2, tempy2);

}

getch();

closegraph();

}

void func(tempx1, tempy1, tempx2, tempy2)

{

int p, i, dx, dy, x, y, x1, y1;

putpixel(x1, y1,RED);

for(i=0; i

{

if(p<0)

{

p=(p+2*dy);

x=x+1;

putpixel(x,y,RED);

}

else

{

p=(p+2*dy-2*dx);

x=x++;

y=y++;

putpixel(x,y,RED);

}

}

closegraph();

}

You might also like More from author

Leave a Reply

Font Resize