c program for dda algorithm

C program for dda algorithm. DDA (Digital Differential Analyzer) Algorithm also know as Vector Generation algorithm.

Why DDA algorithm ?

DDA Algorithm is used to plot line between two nodes i.e two end points in computer system.
While our computer understand pixels, if we want to plot a line, we should have maximum intermediate vertices of the line i.e intermediate points, so as to generate a straight line. Here DDA does the same.

c program for dda algorithm :

#include <graphics.h>
#include <stdio.h>
#include <math.h>

int main( )
{
    float x,y,x1,y1,x2,y2,dx,dy,pixel;
    int i,gd,gm;

    printf("Enter the value of x1 : ");
    scanf("%f",&x1);
    printf("Enter the value of y1 : ");
    scanf("%f",&y1);
    printf("Enter the value of x2 : ");
    scanf("%f",&x2);
    printf("Enter the value of y1 : ");
    scanf("%f",&y2);

    detectgraph(&gd,&gm);
    initgraph(&gd,&gm,"");

    dx=abs(x2-x1);
    dy=abs(y2-y1);

    if(dx>=dy)
    pixel=dx;
    else
    pixel=dy;

    dx=dx/pixel;
    dy=dy/pixel;

    x=x1;
    y=y1;

    i=1;
    while(i<=pixel)
    {
          putpixel(x,y,1);
          x=x+dx;
          y=y+dy;
          i=i+1;
          delay(100);
    }
    getch();
    closegraph();
}

Output :

C program for DDA algorithm
C program for DDA algorithm

C program to implement DDA algorithm in Graphics

DDA algorithm :

  1. Define the nodes, i.e end points in form of (x1,y1) and (x2,y2).
  2. Calculate the distance between the two end points vertically and horizontally, i.e dx=|x1-x2| and dy=|y1-y2|.
  3. Define new variable name ‘pixel’, and compare dx and dy values,
    if dx > dy then
    pixel=dx
    else
    pixel =dy.
  4. dx=dx/pixel
    and dy=dy/pixel
  5. x=x1;
    y=y1;
  6. while (i<=pixel) compute the pixel and plot the pixel with x=x+dx and y=y+dy.

If you are having any problem related to the post, do comment below.


7 thoughts on “c program for dda algorithm”

  1. without rounding off would it work successfully ??
    the principle of digital differential analyzer is it rounding off the point which contain a fractional value, for which it takes more time to calculate and can’t able draw the original line.

  2. please help me…..jab b me is code ko run krn to aik warning deta hay k (function should have a returned value) and jab m int main() ki jaga void mein likhn to warning to remove ho jati hay but output ni ati just points get kr k wapis coding screen p a jata ha plzzzzzzzzzzzz help me

Add Comment Register



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>