less than 1 minute read

#include<graphics.h>
#include<iostream.h>
#include<dos.h>
#include<math.h>
#include<conio.h>
void main()
{
	int gd=DETECT,gm;
	int x1,x2,y1,y2;
	int i,flag,d;
	clrscr();
	cout<<"Enter value of (x1,y1)= ";
	cin>>x1>>y1;
	cout<<"Enter value of (x2,y2)= ";
	cin>>x2>>y2;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	int dx,dy;
	dx=abs(x2-x1);
	dy=abs(y2-y1);
	int x,y,t,s1,s2;
	x=x1;
	y=y1;
	if((x2-x1)>0)
	s1=1;
	else
	s1=-1;
	if((y2-y1)>0)
	s2=1;
	else
	s2=-1;
	if(dy>dx)
	{
		t=dx;
		dx=dy;
		dy=t;
		flag=1;
	}
	else
	flag=0;
	d=2*dy-dx;
	outtextxy(x1,y1,"(x1,y1)");
	outtextxy(x2,y2,"(x2,y2)");
	i=1;
	a:
	putpixel(x,y,3);
	delay(40);
	while(d>=0)
	{
		if (flag==1)
		x=x+s1;
		else
		y=y+s2;
		d=d-2*dx;
	}
	if (flag==1)
		y=y+s2;
	else
		x=x+s1;
		
	d=d+2*dy;
	i++;
	if(i<=dx)
	goto a;
	getch();

	closegraph();
}

Leave a comment