Mar 18, 2010

Bresenham's line drawing algorithm

//This is Bresenham's line drawing algorithm developed in C language
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void jayubre(int,int,int,int);
void main(void)
{
int gdriver = DETECT, gmode,x1,y1,x2,y2;
initgraph(&gdriver, &gmode, "..\\bgi");
printf("Enter starting points(x1,y1):\n"),scanf("%d%d",&x1,&y1);
printf("Enter ending points(x2,y2):\n"),scanf("%d%d",&x2,&y2);
jayubre(x1,y1,x2,y2);
getch();
}
void jayubre(int x1,int y1,int x2,int y2)
{
int p0,dy,dx,sx=1,sy=1,xmax, ymax,x,y;
float m;
putpixel(x1,y1,8);
putpixel(x2,y2,8);
if(x1>x2)
sx=-1;
if(y1>y2)
sy=-1;
dx=abs(x2-x1);
dy=abs(y2-y1);
m=dy/dx;
if(m<1)
{
p0=2*dy-dx;
while(x1!=x2)
{
if(p0<0)
{
p0=p0+2*dy;
x1=x1+sx;
y1=y1;
putpixel(x1,y1,4);
}
else if(p0>=0)
{
p0=p0+2*dy-2*dx;
x1=x1+sx;
y1=y1+sy;
putpixel(x1,y1,4);

}
}
}
else
{
p0=2*dx-dy;
while(y1!=y2)
{
if(p0<0)
{
p0=p0+2*dx;
y1=y1+sy;
x1=x1;
putpixel(x1,y1,4);
}
else if(p0>=0)
{
p0=p0+2*dx-2*dy;
y1=y1+sy;
x1=x1+sx;
putpixel(x1,y1,4);
}
}
}
}

No comments:

Post a Comment