Computer Graphics Program source codes with full description.
Study of Various C Graphics Functions
Implementation and Using mouse in DOS
Implementation of DDA line algorithm with source code in C/C++.
Implementation of Bresenham Line algorithm with source code in C/C++.
Implementation of Midpoint Line algorithm with source code in C/C++.
Implementation of Bresenham Circle algorithm with source code in C/C++.
Implementation of Mid-point Circle algorithm with source code in C/C++.
Implementation of Mid-point Ellipse algorithm with source code in C/C++.
Implementation of Polygon Filling using Scan Fill with source code in C/C++.
Implementation of Polygon Filling using Flood Fill with source code in C/C++.
Implementation of Polygon Filling using Boundary Fill Algorithms.
Implementation of algorithm of 2D Transformation of an Object with source code in C/C++.
Implementation of Line Clipping using Cohen- Sutherland algorithm with source code in C/C++.
Implementation of Line Clipping using Liang-Barky algorithm with source code in C/C++.
Implementation of Polygon clipping using Sutherland-hodgeman algorithm with source code in C/C++.
Search
Saturday, 30 April 2011
Implementation of DDA line algorithm with source code in C/C++.
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
#define ROUND(a) ((int)(a+0.5))
void lineDDA(int,int,int,int);
void lineDDA_dotted(int,int,int,int);
void lineDDA_dash(int,int,int,int);
void dottdash(int,int,int,int);
void lineDDA(int xa,int ya,int xb,int yb)
{
int dx=xb-xa, dy=yb-ya,steps,k;
float xIncrement, yIncrement,x=xa,y=ya;
if(abs(dx) > abs(dy)) steps=abs(dx);
else steps=abs(dy);
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),15);
for(k=0;k<steps;k++)
{
x+=xIncrement;
y+=yIncrement;
putpixel(ROUND(x),ROUND(y),15);
}
}
void lineDDA_dotted(int xa,int ya,int xb,int yb)
{
int dx=xb-xa, dy=yb-ya,steps,k;
float xIncrement, yIncrement,x=xa,y=ya;
if(abs(dx) > abs(dy)) steps=abs(dx);
else steps=abs(dy);
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),15);
for(k=0;k<steps;k++)
{
x+=xIncrement;
y+=yIncrement;
if(k%2==0) putpixel(ROUND(x),ROUND(y),15);
}
}
void lineDDA_dash(int xa,int ya,int xb,int yb)
{
int dx=xb-xa, dy=yb-ya,steps,k;
float xIncrement, yIncrement,x=xa,y=ya;
int flag=0;
if(abs(dx) > abs(dy)) steps=abs(dx);
else steps=abs(dy);
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),15);
for(k=0;k<steps;k++)
{
x+=xIncrement;
y+=yIncrement;
if(flag==0) putpixel(ROUND(x),ROUND(y),15);
if(k%5==0)
{
if(flag==0) flag=1;
else flag=0;
}
}
}
void dottdash(int xa,int ya,int xb,int yb)
{
int dx=xb-xa, dy=yb-ya,steps,k;
float xIncrement, yIncrement,x=xa,y=ya;
int flag1=0;
if(abs(dx) > abs(dy)) steps=abs(dx);
else steps=abs(dy);
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),15);
for(k=0;k<steps;k++)
{
x+=xIncrement;
y+=yIncrement;
if(k%2==0) putpixel(ROUND(x),ROUND(y),15);
else
{
if(flag1==0) putpixel(ROUND(x),ROUND(y),15);
if(k%5==0)
{
if(flag1==0) flag1=1;
else flag1=0;
}
}
}
}
void main()
{
int x,y,xmax,ymax,q;
char str[20];
int gdriver = DETECT,gmode,errorcode;
start: clrscr();
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
xmax=getmaxx();
ymax=getmaxy();
rectangle(0,100,xmax,ymax);
lineDDA(xmax/2,80,xmax/2,ymax);
lineDDA(0,ymax/2,xmax,ymax/2);
while(1)
{
moveto(xmax/2,ymax/2);
gotoxy(0,2);
printf("0:Exit,1:Solid,2:Dotted,3:Dash 4:Dotted_Dash");
gotoxy(1,3);
printf("What to do: ");
gotoxy(12,4);
printf("x: ");
gotoxy(12,5);
printf("y: ");
gotoxy(12,6);
printf(" ");
gotoxy(12,3);
scanf("%d",&q);
if(q==0) break;
if(q!=1&&q!=2&&q!=3&&q!=4)
{
gotoxy(20,6);
printf("Enter valid choice.");
getch();
continue;
}
gotoxy(14,4);
scanf("%d",&x);
gotoxy(14,5);
scanf("%d",&y);
if(q==1) lineDDA(xmax/2,ymax/2,(xmax/2)+x,(ymax/2)+y);
else if(q==2) lineDDA_dotted(xmax/2,ymax/2,(xmax/2)+x,(ymax/2)+y);
else if(q==3) lineDDA_dash(xmax/2,ymax/2,(xmax/2)+x,(ymax/2)+y);
else if(q==4) dottdash(xmax/2,ymax/2,(xmax/2)+x,(ymax/2)+y);
getch();
goto start;
}
}
No comments:
Post a Comment