In computer graphics, a hardware or software implementation of a digital differential analyzer (DDA) is used for linear interpolation of variables over an interval between start and end point. DDAs are used for rasterization of lines, triangles and polygons. In its simplest implementation the DDA Line drawing algorithm interpolates values in interval [(xstart, ystart), (xend, yend)] by computing for each xi the equations xi = xi−1+1/m, yi = yi−1 + m, where Δx = xend − xstart and Δy = yend − ystart and m = Δy/Δx.

The dda is a scan conversion line algorithm based on calculating either dy or dx. A line is sampled at unit intervals in one coordinate and corresponding integer values nearest the line path are determined for other coordinates.

Considering a line with positive slope, if the slope is less than or equal to 1, we sample at unit x intervals (dx=1) and compute successive y values as

Subscript k takes integer values starting from 0, for the 1st point and increases by until endpoint is reached. y value is rounded off to nearest integer to correspond to a screen pixel.

For lines with slope greater than 1, we reverse the role of x and y i.e. we sample at dy=1 and calculate consecutive x values as

Similar calculations are carried out to determine pixel positions along a line with negative slope. Thus, if the absolute value of the slope is less than 1, we set dx=1 if i.e. the starting extreme point is at the left.

For more information on the algorithm see Digital differential analyzer (graphics algorithm).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <graphics.h> #include <stdio.h> #include <conio.h> #include <math.h> void main() { int gd = DETECT, gm = DETECT, s, dx, dy, m, x1, y1, x2, y2; float xi, yi, x, y; clrscr(); printf("Enter the sarting point x1 & y1\n"); scanf("%d%d", &x1, &y1); printf("Enter the end point x2 & y2\n"); scanf("%d%d", &x2, &y2); initgraph(&gd, &gm, ""); cleardevice(); dx = x2 - x1; dy = y2 - y1; if (abs(dx) > abs(dy)) s = abs(dx); else s = abs(dy); xi = dx / (float) s; yi = dy / (float) s; x = x1; y = y1; putpixel(x1, y1, 4); for (m = 0; m < s; m++) { x += xi; y += yi; putpixel(x, y, 4); } getch(); } |

**Download the code using the below link:**

Hope, it helps!

Hello !! there is no line draw in vedio

Its there, a red one. Its a one pixel line. Look carefully. Maybe i should have used white color to plot the points.

This is very useful in study…..

What about negative co-ordinates For example (-50,150) and (100, 200). The part of the line is missing as the origin is left bottom of the screen. Can we set the origin to the center of the screen?

@Pragatheesh:

My code is just an example on how to implement the DDA line drawing algorithm. Please enhance the code to meet your needs.

Thanks very much you made my day thanks

continue your work…

do we need to add graphic file seperatly in c so that programs can get execute. bcoz my pc is showing error and if it has no error still its not showing the output. what to do???? plz rpy

Which complier are you using?