Digital Diffrential Analyzer(DDA)
Imp: View my post Constraints before going through the code
The TurboC code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
int transx(int x,int amag)
{
if(amag>1)
x=x*amag;
x=320+x;
return(x);
}
int transy(int y,int amag)
{
if(amag>1)
y=y*amag;
y=240-y;
return(y);
}
void magnify(int x,int y,int color,int d,int basepix)
{
int i,j;
d=(d*2)-1;
for(i=d;i>=-d;--i)
{
for(j=d;j>=-d;--j)
{
putpixel(x+i,y+j,color);
}
}
putpixel(x,y,basepix);
}
void initgmode()
{
int GraphDriver,GraphMode,ErrorCode;
GraphDriver = DETECT;
initgraph(&GraphDriver,&GraphMode,"E:\\TC\\BGI");
if((ErrorCode=graphresult())!=grOk)
{
printf("\nERROR : %s\n",grapherrormsg(ErrorCode));
exit(1);
}
}
void axes(int color,int basepix,int d,int show)
{
int i,j,k;
int c=getcolor();
if(show==1)
{
for(i=0;i<=480;i++)
{
putpixel(320,i,color);
if(d>1)
for(k=1;k<=(320/d);k++)
{
putpixel(320-(k*d),i,basepix);
putpixel(320+(k*d),i,basepix);
}
}
for(i=0;i<=640;i++)
{
putpixel(i,240,color);
if(d>1)
for(k=1;k<=(240/d);k++)
{
putpixel(i,240+(k*d),basepix);
putpixel(i,240-(k*d),basepix);
}
}
}
setcolor(15);
outtextxy(330,2,"Y");
outtextxy(2,250,"X'");
outtextxy(330,250,"O:(0,0)");
outtextxy(628,250,"X");
outtextxy(330,468,"Y'");
setcolor(c);
putpixel(320,240,basepix);
}
void ddaline(int x0,int y0,int x1,int y1,int lcol,int d,int am)
{
int dx,dy,tempx,tempy;
//int steps,k;
float m,b;
dx=x1-x0;
dy=y1-y0;
tempx = transx(x0,am);
tempy = transy(y0,am);
magnify(tempx,tempy,lcol,d,0);
if(abs(dx)>abs(dy))
{
m=(float)dy/(float)dx;
b=(float)y0-(m*((float)x0));
dx=(dx<0)?-1:1;
while(x0!=x1)
{
x0 +=dx;
y0=floor((m*(float)x0)+b);
tempx = transx(x0,am);
tempy = transy(y0,am);
magnify(tempx,tempy,lcol,d,0);
}
}
else
{
if(dy!=0)
{
m=(float)dx/(float)dy;
b=(float)x0-(m*(float)y0);
dy=(dy<0)?-1:1;
while(y0!=y1)
{
y0 +=dy;
tempx=floor((m*(float)y0)+b);
tempx=transx(tempx,am);
tempy=transy(y0,am);
magnify(tempx,tempy,lcol,d,0);
}
}
}
}
void main()
{
int x0,y0,x1,y1,magstat;
int degree=1;
int axis_color=13;
int axis_mag=1;
int show_grid=1;
int color=14;
int basepix_col=3;
char choice='y';
do
{
clrscr();
printf("\n\nEnter magnification preference: \n");
printf("------------------------------------\n");
printf("i) Press '0' to magnify the pixels only.\n");
printf("ii) Press '1' to magnify the entire grid.\n");
printf("Enter your choice:");
do
{
scanf("%d",&magstat);
if((magstat!=1)&&(magstat!=0))
printf("\n\nERROR in input! Please enter either 0 or 1: ");
}while((magstat!=1)&&(magstat!=0));
if(magstat==1)
{
printf("\nEnter the grid magnificaton degree (>=2): ");
do
{
scanf("%d",&axis_mag);
if(axis_mag<=1)
printf("\nERROR in input! Please enter a value >0");
}while(axis_mag<=1);
}
do
{
printf("\nEnter the pixel magnification degree (>=1): ");
scanf("%d1",°ree);
if(degree<1)
printf("\n\nERROR! Degree sould be > 0. Insert again!");
}while(degree<1);
printf("\n\nEnter the starting X-cordinate : ");
scanf("%d",&x0);
printf("Enter the starting Y-coordinate : ");
scanf("%d",&y0);
printf("\n\nEnter the ending X-cordinate : ");
scanf("%d",&x1);
printf("Enter the ending Y-coordinate : ");
scanf("%d",&y1);
clrscr();
initgmode();
axes(axis_color,basepix_col,axis_mag,show_grid);
ddaline(x0,y0,x1,y1,color,degree,axis_mag);
getch();
closegraph();
do
{
printf("Do You Want to Continue? (y/n) :");
fflush(stdin);
scanf("%c",&choice);
if((choice!='y')&&(choice!='Y')&&(choice!='n')&&(choice!='N'))
printf("ERROR! please enter either 'y' or 'n'\n\n");
}while((choice!='y')&&(choice!='Y')&&(choice!='n')&&(choice!='N'));
}while((choice=='y')||(choice=='Y'));
}