Sei sulla pagina 1di 1

/*Progarm to implement Bresenham's line drawing algorithm for a line with slope

less than 1
(both positive and negative) with origin at the centre of the screen*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void line1(int x1,int y1,int x2,int y2)
{
int dx,dy,x,y,p,xend;
dx=(x2-x1);
dy=(y2-y1);
p=2*dy-dx;
if(p<1)
if(x2>x1)
{
x=x1;
y=y1;
xend=x2;
}
else
{
x=x2;
y=y2;
xend=x1;
}
while(x<xend)
{
putpixel(x,y,RED);
x++;
if(p<0)
p=p+2*dy;
else
{
y++;
p=p+2*(dy-dx);
}
}
}
void main(void)
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"");
setbkcolor(1);
line1(200,0,300,50);
line(319,0,319,479);
line(0,239,639,239);
outtextxy(322,242,"(0,0)");
getch();
closegraph();
}

Potrebbero piacerti anche