CG/DDA-line.cpp

54 lines
1.1 KiB
C++

/*
Digital Differential Analyzer (DDA) algorithm for drawing a line.
Code from Computer Graphics (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-codes/CG
*/
// BEGINNING OF CODE
#include<iostream>
#include<graphics.h>
using namespace std;
int main()
{
float x1,x2,y1,y2,dx,dy,xin,yin,x,y,len;
cout << "Enter the value of x1:\t";
cin >> x1;
cout << "Enter the value of x2:\t";
cin >> x2;
cout <<"Enter the value of y1:\t";
cin >> y1;
cout << "Enter the value of y2:\t";
cin >> y2;
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
dx=x2-x1;
dy=y2-y1;
if (abs(dx)>=abs(dy))
len=abs(dx);
else
len=abs(dy);
xin=dx/len;
yin=dy/len;
x = x1+0.5;
y = y1+0.5;
int i = 0;
while(i<=len)
{
if(i%2==0)
putpixel(x,y,GREEN);
x = x+xin;
y = y+yin;
i++;
}
delay(50000);
closegraph();
return (0);
}
// END OF CODE