Thursday, August 16, 2007

Implementing DDA Straight Line drawing Algorithm

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'));
}

Tuesday, August 14, 2007

Implementing Simple Straight Line drawing Algorithm

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 simpleline(int x0,int y0,int x1,int y1,int lcol,int d,int am)
{
int dx,dy,tempx,tempy;
float m,b;
dx=x1-x0;
dy=y1-y0;
tempx = transx(x0,am);
tempy = transy(y0,am);
magnify(tempx,tempy,lcol,d,0);
if(dx!=0)
{
m=(float)dy/(float)dx;
b=(float)y0-(m*((float)x0));
dx=(x1>x0)?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);
}
}
}

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);
simpleline(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'));
}

Plotting a single pixel on screen

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 main()
{
int x0,y0,magstat;
int degree=1;
int axis_color=4;
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 X-cordinate : ");
scanf("%d",&x0);
printf("Enter the Y-coordinate : ");
scanf("%d",&y0);
x0=transx(x0,axis_mag);
y0=transy(y0,axis_mag);
clrscr();
initgmode();
axes(axis_color,basepix_col,axis_mag,show_grid);
magnify(x0,y0,color,degree,basepix_col);
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'));
}

Constraints

Within each code I've introduced some constraints. They are more or less same for each code and are as follows:

i) Magnification Preference: You have the option to magnify
a) the plotted pixel only or
b) you can magnify the entire grid points alongwith the pixel

ii) Showing grids: If you opt not to magnify the grids there is no use of plotting it. In this case the grids will cover the entire screen(A nice food for thought).
When you magnify the grids you have an option to show the grid system. (variable name: show_grid)// default=1 shows the grid
However, the abscissa and ordinate will be showed throughout.(You can chalk out a strategy also to enable this option. Just a few more lines)

iii) Choosing Colors: For choosing colors you just have to change some values within the code. You can also take user input by just adding 'scanf's with the mentioned variable names:
a) You can choose the colors of the abscissa and the ordinate (variable name: axis_color) //default = 4;
b) Pixel Color: When you magnify the pixel the color of the plotted pixel (variable name: color) //default=14;
c) When a pixel is magnified, the basic pixel color(only the pixel point) can be chosen. It is also the color of the grids apart from the abscissa and ordinate. (variable name: basepix_col) //default=3;

One more thing to say, change the hardquoted "E://TC/BGI" directory to you own "TC//BGI" Directory. Best Wishes

Welcome

Hi,
This is Ritwick. In this blog you can find the necessary TurboC codes for preliminary computer graphics. Best of luck.