DDA Line Drawing Algorithm

Write a program to implement DDA line drawing algorithm.

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define ROUND(a) ((int)(a+.5))

void dda(int,int,int,int);
void main()
{
	int gd=DETECT,gm,x1,x2,y1,y2;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter the coordinates: ");
	scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
	dda(x1,y1,x2,y2);
	getch();
	closegraph();
}

void dda(int x1,int y1,int x2,int y2)
{
	int dx,dy,steps,k;
	float xin,yin,x,y;
	dx=x2-x1;
	dy=y2-y1;
	x=x1;
	y=y1;
	if(abs(dx)>abs(dy))
		steps=abs(dx);
	else
		steps=abs(dy);
	xin=dx/(float)steps;
	yin=dy/(float)steps;
	putpixel(ROUND(x),ROUND(y),WHITE);
	for(k=0;k<steps;k++)
	{
		x+=(xin);
		y+=(yin);
		putpixel(ROUND(x),ROUND(y),WHITE);
	}
}

Output
1

Leave a Reply

Your email address will not be published. Required fields are marked *