Sunday, September 24, 2017

DDA(Digital Differential Algorithm)

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main(){

    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    float x1,x2,y1,y2;
    cout<<"Enter coordinates\n";
    cout<<"x1:";
    cin>>x1;
    cout<<"y1:";
    cin>>y1;
    cout<<"x2:";
    cin>>x2;
    cout<<"y2:";
    cin>>y2;
    float m=(y2-y1)/(x2-x1);
    putpixel(x1,y1,10);
    if(m<=1){
    while(x1!=x2){
    x1++;
    y1=y1+m;
    putpixel(x1,y1,10);
    }
    }else{
    while(y1!=y2){
    y1++;
    x1=x1+1/m;
    putpixel(x1,y1,10);
    }

    }
    getch();
}


Output: