Thursday, July 5, 2018

DDA line drawing program in C++

source code:

/* Line drawing programme in c++ using DDA algorithm*/
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
void main()
{  float x,y,x1,y1,x2,y2,dx,dy,length;
   int i,gd=DETECT,gm;
   // Read two end point
   cout<<"Enter first point";
   cin>>x1>>y1;
   cout<<"Enter another point ";
   cin>>x2>>y2;
 // initializing the graphics mode
   initgraph(&gd,&gm,"c://TURBOC3//bgi");
   dx=abs(x2-x1);
   dy=abs(y2-y1);
   if(dx>=dy)
   {
   length=dx;
   }
   else {
   length=dy;
   }
    dx=(x2-x1)/length;
    dy=(y2-y1)/length;
    x=x1+0.5; //factor 0.5 is added to round the value
    y=y1+0.5;// factor 0.5 is added to round the value
    i=1;  // initialize the loop counter
    while(i<=length)
    {  putpixel(x,y,WHITE);
    x=x+dx;
    y=y+dy;
    i=i+1;
    delay(50); // to see every iteration or steps
    }
getch();

}

output:

2 comments: