> dda line drawing algorithm in c ~ Online tutorial

dda line drawing algorithm in c

DDA Line drawing Algorithm

The digital differential analyzer (DDA)  is a scan-conversion line algorithm. In this algorithm, we sample the line at unit intervals in one coordinate and determine corresponding integer values nearest the line path of the other coordinate and plot those coordinate (pixel) in computer screen. Consider first a line with positive slope. If the slope is less than or equal to 1, we sample at unit x intervals (dx = 1) and computer each successive y value as y(k+1) = y(k) + m. Subscript k takes integer values starting from 1, for the first point, and increases by 1 until the final endpoint is reached. For lines with a positive slope greater than 1, we reverse the roles of x and y.That is, we sample at unit y intervals (dy = 1) and calculate each succeeding x value as x(k+1) = x(k) + (1/m)




Program

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void DDA(int x1,int y1,int x2,int y2,int col,int del);
void main()
{
int gd=DETECT,gm,x1,x2,y1,y2;
initgraph(&gd,&gm,"");
printf("Enter (x1,y1) and (x2,y2) : ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
DDA(x1,y1,x2,y2,14,20);
getch();
closegraph();
}
void DDA(int x1,int y1,int x2,int y2,int col,int del)
{
int dx,dy,s,i,xi,yi,x,y;
x=x1,y=y1;
dx=x2-x1;
dy=y2-y1;
if(dx>dy)
s=dx;
else
s=dy;
xi=dx/s;
yi=dy/s;
putpixel(x,y,col);
for(i=0;i<s;i++)

{
x+=xi;
y+=yi;

putpixel(x,y,col);
delay(del);

}
}


Output









Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: