You are on page 1of 2

#include<stdio.

h>
#include<conio.h>
#include<graphics.h>
int Round(float);
void main()
{

int x1,x2,y1,y2,x,y,dx,dy,steps,abs_dx,abs_dy,k;
float xincrement,yincrement;

int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");


//algo linedda(x1,y1,x2,y2)
//here the endpoints are (x1,y1) and (x2,y2)
//Round(x)=int(x+0.5)

printf("Enter first  endpoint= ");


scanf("%d %d",&x1,&y1);
printf("Enter second endpoint= ");
scanf("%d %d",&x2,&y2);

dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
if(dx<0)
abs_dx=dx*(-1);
else
abs_dx=dx;

if(dy<0)
abs_dy=dy*(-1);
else
abs_dy=dy;

if(abs_dx>=abs_dy)
steps=abs_dx;
else
steps=abs_dy;

xincrement=(abs_dx/steps);
yincrement=(abs_dy/steps);
putpixel(Round(x),Round(y),2);

for(k=1;k<=steps;k++)
{
  x=x+xincrement;
  y=y+yincrement;
  putpixel(Round(x),Round(y),2);
}

getch();
}

int Round(float z)
{
int z1,z2;
z1=z+0.5;
z2=(int)z;
if((z2-z1)>=0.5)
{
z=z2+1;
}
return z;
}

You might also like