DDA Line Drawing Algorithm in C

On October 7, 2008, in Algorithms, by , Views : 82270


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:

Please 'Like' or '+1' us to get the download link! (If you have already liked us on facebook, then like us either on G+ or twitter)



Hope, it helps! :)



8 Responses to DDA Line Drawing Algorithm in C

  1. prakash chawla says:

    Hello !! there is no line draw in vedio

  2. sagar says:

    This is very useful in study…..

  3. Pragatheesh says:

    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?

  4. swapnil says:

    Thanks very much you made my day thanks :)
    continue your work… :)

  5. rewa says:

    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

Leave a Reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

  • Dipin Krishna

Archives

All entries, chronologically...

Read more:
kite
Flying Kite

CIRCLE
Circle using Mid-Point Algorithm

Final translated Image
2D Transformations: Translations

MinMaxClose