WAP in C to draw a line using DDA(Digital
Differential Algorithm) algorithm
OUTPUT
C CODE:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#define round(val)(int)(val+0.5)
void main()
{
int gd = DETECT, gm;
void line_daa(int,int,int,int);
int Xa,Xb,Ya,Yb;
clrscr();
printf("ntEnter the values:");
scanf("%d%d%d%d",&Xa,&Ya,&Xb,&Yb);
initgraph(&gd, &gm, "C:TCBGI");
cleardevice();
line_daa(Xa,Ya,Xb,Yb);
getch();
closegraph();
}
void line_daa(int Xa,int Ya,int Xb,int Yb)
{
int Dx=Xb-Xa,Dy=Yb-Ya,steps,k;
float Xin,Yin,X=Xa,Y=Ya;
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),6);
for(k=0;k<steps;k++)
{
X=X+Xin;
Y=Y+Yin;
putpixel(round(X),round(Y),6);
}
}

Wap in c to draw a line using DDA algorithm

  • 1.
    WAP in Cto draw a line using DDA(Digital Differential Algorithm) algorithm OUTPUT C CODE: #include <stdio.h> #include <conio.h> #include <math.h> #include <graphics.h> #define round(val)(int)(val+0.5) void main() { int gd = DETECT, gm; void line_daa(int,int,int,int); int Xa,Xb,Ya,Yb; clrscr(); printf("ntEnter the values:"); scanf("%d%d%d%d",&Xa,&Ya,&Xb,&Yb); initgraph(&gd, &gm, "C:TCBGI"); cleardevice(); line_daa(Xa,Ya,Xb,Yb);
  • 2.
    getch(); closegraph(); } void line_daa(int Xa,intYa,int Xb,int Yb) { int Dx=Xb-Xa,Dy=Yb-Ya,steps,k; float Xin,Yin,X=Xa,Y=Ya; 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),6); for(k=0;k<steps;k++) { X=X+Xin; Y=Y+Yin; putpixel(round(X),round(Y),6); } }