WAP to implement DDA Digital Differential Analyzer

//DDA Line Rasterization Algorithm

//Practical assignment under MCA (TYC)- Sem IV
//Guru Nanak Dev University, Amritsar Campus

#include <iostream.h>
#include <math.h>
#include <conio.h>

int main(void)
{
cout<<endl<<endl;

int x1, y1; //starting point
int x2, y2; //ending point


//input points
cout<<"Enter starting point x1 y1 : ";
cin>>x1>>y1;
cout<<"Enter ending point x2 y2 : ";
cin>>x2>>y2;


//Length?
float length;

if (abs(x2-x1) >= abs(y2-y1))
{length=abs(x2-x1);}
else
{length=abs(y2-y1);}


//One raster unit length?
float deltaX, deltaY;

deltaX=(x2-x1)/length;
deltaY=(y2-y1)/length;


//rounding off
//"rounding off is better than truncation", Narsingh Deo, System Simulation
float x, y;

//giving x and y initial values,
//needed because implemented by computer
//needs precise instructions
x=x1;
y=y1;

x=x+0.5;
y=y+0.5;

//printing initial values
cout<<endl<<endl;
cout<<"x1 = "<<x1<<endl;
cout<<"y1 = "<<y1<<endl;
cout<<endl;

cout<<"x2 = "<<x2<<endl;
cout<<"y2 = "<<y2<<endl;
cout<<endl;

cout<<"Length = "<<length;
cout<<endl<<endl;

cout<<"deltaX = "<<deltaX<<endl;
cout<<"deltaY = "<<deltaY;
cout<<endl<<endl;

cout<<"x = "<<x<<endl;
cout<<"y = "<<y;
cout<<endl<<endl;


//begin main loop- calculate and print
int i=1;
int x_to_pixel = 0, y_to_pixel = 0;

while(i<=length)
{x_to_pixel=floor(x);
y_to_pixel=floor(y);

cout<<i<<"t"<<"("<<x_to_pixel<<", "<<y_to_pixel<<")"<<"tt"<<x<<"t"<<y;
cout<<endl;

x=x+deltaX;
y=y+deltaY;

i++;
}

getch();
return 0;
}

Comments