Showing posts with label Bresenham's line drawing program. Show all posts
Showing posts with label Bresenham's line drawing program. Show all posts

Tuesday, October 8, 2013

Program using Bresenham's line drawing algorithm ( all quadrants)

/* This program will draw x- y coordinate system and will draw lines in all quadrants using   Bresenham's line drawing algorithm  */

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x,y,dy,dx,p,gap=50,temp;
float m;
char str[3];
clrscr();
initgraph(&gd,&gm,"..\\bgi");

printf("Enter first co-ords of line\n");
scanf("%d",&x1);
scanf("%d",&y1);
printf("Enter second co-ords of line\n");
scanf("%d",&x2);
scanf("%d",&y2);
if (y1>y2)    // needed for -ve slope to work
{temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
}
dy=abs(y2-y1);
dx=abs(x2-x1);
m=(float)(dy)/(dx);
dy=abs(dy);
dx=abs(dx);
xmid= getmaxx()/2;
ymid =getmaxy()/2;
x2=x2+xmid;
y2=ymid-y2;
x=x1;y=y1;
if(m>1.0)
{
x=y1;
y=x1;
}


line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);

for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);

}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);

}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}

x=x+xmid;
y=ymid -y;
p=2*dy-dx;
while(x<x2)
{
if(p>=0)
{
putpixel(x,y,5);

y=y-1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,5);
p=p+2*dy;
}
x=x+1;
}
getch();
closegraph();
}