C program to implement Bresenham's line integer algorithm

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,x2,y1,y2,dx,dy,e,m,k;
clrscr();
initgraph (&gd,&gm,"C:\\TC\\BGI");
printf("Enter the co-ordinates of the first point \n");
printf("x1= ");
scanf("%d/n",&x1);
printf("y1= ");
scanf("%d/n",&y1);
printf("Enter the co-ordinates of the second point \n");
printf("x2= ");
scanf("%d/n",&x2);
printf("y2= ");
scanf("%d/n",&y2);
clrscr();
dx= x2-x1;
dy= y2-y1;
m=(float)dy/dx;
e=m-0.5;
x=x1;
y=y1;
for(k=1;k<=dx;k++)
{
putpixel (x,y,RED);
while(e>0)
{y++;
e--;
}
x++;
e=e+m;
}
getch();
closegraph();
}
Previous
Next Post »

2 comments

Write comments
Anonymous
AUTHOR
13 February 2013 at 02:41 delete

Bresenham circle generation algo

# include-stdio.h-
# include-conio.h-
# include-graphics.h-
# include-math.h-

void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;

initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();


printf("Enter the radius ");
scanf("%d",&r);


x=0;
y=r;
putpixel(xc+x,yc-y,1);

p=3-(2*r);

for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;

p=p+((4*(x-y)+10));
}

putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);

}
getch();
closegraph();
}

Reply
avatar
Anonymous
AUTHOR
13 February 2013 at 20:48 delete

http://www.spandan2013.com/

http://www.rscoeinnovision.com/

more fests details on "www.knowafest.com"

Reply
avatar