C program for DDA algorithm to generate line.

Posted by Mangesh on April 30, 2018

/* program to draw a line using DDA algorithm. */

Description :

In Digital differential analyzer (DDA) algorithm we first need both the end point of line. And we can take any one point and start moving towards another point.

Program :

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
  int gdriver = DETECT, gmode;
  int i, x1, y1, x2, y2;
  float dx, dy, step;
  float x, y, xinc, yinc;

  initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
  printf("Enter the x - cordinate value of first point : ");
  scanf("%d",&x1);
  printf("Enter the y - cordinate value of first point : ");
  scanf("%d",&y1);

  printf("Enter the x - cordinate value of second point : ");
  scanf("%d",&x2);
  printf("Enter the y - cordinate value of second point : ");
  scanf("%d",&y2);

  dx = abs(x2 - x1);
  dy = abs(y2 - y1);
  if( dx > dy )
    step = dx;
  else
    step = dy;

  xinc = dx / step;
  yinc = dy / step;
  x = x1;
  y = y1;
  for(i=0;i<step;i++)
  {
    x = x + xinc;
    y = y + yinc;
    putpixel(x,y,5);
  }

  getch();
  closegraph();
}

Output :

c program to draw a dda line. Executed and tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program for scaling of 2D object.
2 C program to tramsform 2D objects.
3 C program to draw a line.
4 C program to draw a circle.
5 C program to draw a rectangle.
6 C program for DDA algorithm to generate line.
7 C program for midpoint circle algorithm.
8 C program to draw a bar graph.
9 C program for bresenham's circle drawing algorithm.
Latest Post
1 shubhanshu
2 test
3 C program for Breadth First Search.
4 C program for Depth First Search(DFS).
5 C for circular queue using linked list.