Sunday, August 31, 2014

Easy DDA Line Drawing Algorithm


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,
4. if dx > dy then
pixel=dx
else
pixel =dy.
5. dx=dx/pixel
and dy=dy/pixel
6. x=x1;
y=y1;
7. while (i<=pixel) compute the pixel and plot the pixel with x=x+dx and y=y+dy.

 DDA ALGORITHM : PROGRAM


#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();
}

Get 1000 Free YouTube Views For Your Video!

0 comments:

Post a Comment