0% found this document useful (0 votes)
6K views

Computer Graphics Lab Programs

The document contains algorithms for computer graphics line drawing, circle drawing, and polygon filling. It includes the Digital Differential Analyzer (DDA) line algorithm, Bresenham's line and circle algorithms, the midpoint circle algorithm, and a recursive flood-fill algorithm for filling polygons. C implementations with functions like line_DDA(), circle_mid_point(), and fillpolygon() are provided to demonstrate how to code these algorithms in C and use graphics functions like putpixel() to draw to the screen.

Uploaded by

Rahul Hellsanxel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6K views

Computer Graphics Lab Programs

The document contains algorithms for computer graphics line drawing, circle drawing, and polygon filling. It includes the Digital Differential Analyzer (DDA) line algorithm, Bresenham's line and circle algorithms, the midpoint circle algorithm, and a recursive flood-fill algorithm for filling polygons. C implementations with functions like line_DDA(), circle_mid_point(), and fillpolygon() are provided to demonstrate how to code these algorithms in C and use graphics functions like putpixel() to draw to the screen.

Uploaded by

Rahul Hellsanxel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 26

Digital Differential Analyzer(D.D.A.

) Algorithm

Void Line_DDA(int x1, int y1, int x2, int y2)


{
int dx = x2-x1;
int dy = y2-y1;
int step,k;
float x_inc,y_inc,x,y;
x=x1;
y=y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
x_inc=dx/(float)step;
y_inc=dy/(float)step;
setpixel(ROUND(x),ROUND(y));
for(k=0;k<step;k++)
{
x=x+x_inc;
y=y+y_inc;
setpixel(ROUND(x),ROUND(y));
}
}
Digital Differential Analyzer(D.D.A.)
C-Implementation

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

void line_DDA(int,int,int,int);
int ROUND(float);

void main()
{
int x1,x2,y1,y2;
int gd=DETECT,gm;
clrscr();
printf("\n Enter the end coordinates of the line : ");
printf("\n x1,y1 : ");
scanf("%d%d",&x1,&y1);
printf("\n x2,y2 : ");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"C:\\TC\\BGI");
line_DDA(x1,y1,x2,y2);
getch();
}

void line_DDA(int x1,int y1,int x2,int y2)


{
int dx = x2-x1;
int dy = y2-y1;
int step,k;
float x_inc,y_inc,x,y;
x=x1;
y=y1;
if(abs(dx)>abs(dy))
{
step=abs(dx);
}
else
{
step=abs(dy);
}
x_inc=dx/(float)step;
y_inc=dy/(float)step;

putpixel(ROUND(x),ROUND(y),6);
for(k=0;k<step;k++)
{
x+=x_inc;
y+=y_inc;
putpixel(ROUND(x),ROUND(y),6);
}
}

int ROUND(float x)
{
int y;
if(x>((int)x+0.5))
{
y=floor(x);
}
if(x<((int)x+0.5))
{
y=ceil(x);
}
return(y);
}
Bresenhams Algorithm

void Line_Bres(x1 , y1 , x2 , y2)


{
Int dx,dy,x,y,x_end,p;
dx=abs(x1-x2);
dy=abs(y1-y2);
p=2*dy-dx;
if(x1 > x2)
{
x=x1;
y=y1;
x_end=x1;
}
else
{
x=x1;
y=y1;
x_end=x2;
}
setpixel(x,y,1);
while(x < x_end)
{
x=x+1;
if(p<0)
p=p+2*dy;
else
{
y=y+1;
p=p+2*(dy-dx);
}
setpixel(x,y,1);
}
}
Bresenhams C-Implementation

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

void line_bres(int,int,int,int);

void main()
{
int x1,x2,y1,y2;
int gd=DETECT,gm;
clrscr();

printf("\n Enter the end coordinates of the line : ");


printf("\n x1,y1 : ");
scanf("%d%d",&x1,&y1);
printf("\n x2,y2 : ");
scanf("%d%d",&x2,&y2);

initgraph(&gd,&gm,"C:\\TC\\BGI");
line_bres(x1,y1,x2,y2);

getch();
}

void line_bres(int x1,int y1,int x2,int y2)


{
int dx,dy,x,y,x_end,p;

dx=abs(x1-x2);
dy=abs(y1-y2);
p=2*dy-dx;

if(x1>x2)
{
x=x2;
y=y2;
x_end=x1;
}
else
{
x=x1;
y=y1;
x_end=x2;
}
putpixel(x,y,1);

while(x<x_end)
{
x+=1;

if(p<0)
{
p+=2*dy;
}
else
{
y+=1;
p+=2*(dy-dx);
}
putpixel(x,y,1);
}
}
MID-POINT CIRCLE ALGORITHM
CIRCLE_MID_POINT(int x_c,int y_c,int r)
{
int x, y, p;
p = 1- r;
x = 0;
y = r;
PLOT_POINT(x_c,y_c,x,y);
while(x < y)
{
if(p < 0)
{
x = x + 1;
p = p + 2 * x + 1;
}
else
{
x = x+1;
y = y-1;
p=p+2*(x-y)+1;
}
PLOT_POINT(x_c,y_c,x,y);
}
}

PLOT_POINT(int x_c, int y_c, int x, int y)


{
putpixel( (x_c + x), (y_c + y),5);
putpixel( (x_c - x), (y_c + y),5);
putpixel( (x_c + x), (y_c - y),5);
putpixel( (x_c - x), (y_c - y),5);
putpixel( (x_c + y), (y_c + x),5);
putpixel( (x_c - y), (y_c + x),5);
putpixel( (x_c + y), (y_c - x),5);
putpixel( (x_c - y), (y_c - x),5);

}
MID-POINT CIRCLE ALGORITHM
C-IMPLEMENTATION
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void plot_point(int,int,int,int);
void circle_mid_point(int,int,int);
void main()
{
int x_c,y_c,r;
int gd=DETECT,gm;
clrscr();

initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n Enter the coordinates of center of the circle : ");
scanf("%d%d",&x_c,&y_c);
printf("\n Enter the radius of circle : ");
scanf("%d",&r);
circle_mid_point(x_c,y_c,r);
getch();
}

void circle_mid_point(int x_c,int y_c,int r)


{
int x,y,p;
p=1-r;
x=0;
y=r;
while(x<y)
{
if(p<0)
{
x=x+1;
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*(x-y)+1;
}
plot_point(x_c,y_c,x,y);
}
}
void plot_point(int x_c,int y_c,int x,int y)
{
putpixel((x_c+x),(y_c+y),5);
putpixel((x_c-x),(y_c+y),5);
putpixel((x_c+x),(y_c-y),5);
putpixel((x_c-x),(y_c-y),5);
putpixel((x_c+y),(y_c+x),5);
putpixel((x_c-y),(y_c+x),5);
putpixel((x_c+y),(y_c-x),5);
putpixel((x_c-y),(y_c-x),5);
}
BRESENHAM CIRCLE ALGORITHM
CIRCLE_BRES(int x, int y, int r)
{
float x1, y1, p;
x1 = 0;
y1 = r;
p = 3 – r * r;
while(x1 <= y1)
{
x1 = x1 + 0.01;
if(p < 0)
{
p = p + (4 * x1) + 6;
}
else
{
y1 = y1 – 0.01;
p = p + (4 * (x1 – y1)) + 10;
}
putpixel((x_c + x1), (y_c + y1), 10);
putpixel((x_c - x1), (y_c - y1), 10);
putpixel((x_c + x1), (y_c - y1), 10);
putpixel((x_c - x1), (y_c + y1), 10);
putpixel((x_c + y1), (y_c + x1), 10);
putpixel((x_c - y1), (y_c - x1), 10);
putpixel((x_c + y1), (y_c - x1), 10);
putpixel((x_c - y1), (y_c + x1), 10);
}
}
BRESENHAM CIRCLE ALGORITHM’S
C-IMPLEMENTATION
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circle_bres(int,int,int);
void main()
{
int x_c,y_c,r;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n Enter the coordinates of center of the circle : ");
scanf("%d%d",&x_c,&y_c);
printf("\n Enter the radius of circle : ");
scanf("%d",&r);
circle_bres(x_c,y_c,r);
getch();
}
void circle_bres(int x_c,int y_c,int r)
{
float x1,y1,p;
x1=0;
y1=r;
p=3-r*r;
while(x1 <= y1)
{
x1=x1+0.01;
if(p<0)
p=p+(4*x1)+6;
else
{
y1=y1-0.01;
p=p+(4*(x1-y1))+10;
}
putpixel((x_c+x1),(y_c+y1),4);
putpixel((x_c-x1),(y_c-y1),4);
putpixel((x_c+x1),(y_c-y1),4);
putpixel((x_c-x1),(y_c+y1),4);
putpixel((x_c+y1),(y_c+x1),4);
putpixel((x_c-y1),(y_c-x1),4);
putpixel((x_c+y1),(y_c-x1),4);
putpixel((x_c-y1),(y_c+x1),4);
}
}
POLYGON GENERATING ALGORITHM
POLYGON_ABS-2(Ax,Ay,N) Entry of polygon in
display file.
Args: Ax,Ay  Array containing the vertices of
polygon.
Global : DF_PEN_X,DF_PEN_Y
Local : I  Stepping through polygon sides.

BEGIN :
IF N<3 THEN RETURN ERROR “Polygon Size Error.”
DF_PEN_X  Ax[N];
DF_PEN_Y  Ay[N];
DISPLAY_FILE_ENTER(N);

FOR I=1 to N DO
LINE_ABS-2(Ax[I],Ay[I]);
RETURN;
END;
POLYGON GENERATING ALGORITHM’S
C- IMPLEMENTATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define MAX 20
void polygon(int,int,int,int);
int ROUND(float);
void main()
{
int x[MAX],y[MAX],n,i;
int gd=DETECT,gm;
clrscr();
printf("\n Enter the number of sides of the polygon : ");
scanf("%d",&n);
if(n<3)
printf("\n Please enter valid coordinates....");
else
{
printf("\n Enter the coordinates of the vertices : ");
for(i=0;i<n;i++)
{
printf("\n x%d, y%d : “,i+1,i+1);
scanf("%d%d",&x[i],&y[i]);
}
}
initgraph(&gd,&gm,"C:\\TC\\BGI");
for(i=0;i<n;i++)
{
if(i==(n-1))
polygon(x[i],y[i],x[0],y[0]);
else
polygon(x[i],y[i],x[i+1],y[i+1]);
}
getch();
}
void polygon(int x1,int y1,int x2,int y2)
{
int dx = x2-x1;
int dy = y2-y1;
int step,k;
float x_inc,y_inc,x,y;
x=x1;
y=y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
x_inc=dx/(float)step;
y_inc=dy/(float)step;
putpixel(ROUND(x),ROUND(y),6);
for(k=0;k<step;k++)
{
x+=x_inc;
y+=y_inc;
putpixel(ROUND(x),ROUND(y),6);
}
}
int ROUND(float x)
{
int y;
if(x>((int)x+0.5))
y=floor(x);
if(x<((int)x+0.5))
y=ceil(x);
return(y);
}

OUTPUT :
Enter the number of sides of the polygon : 4

Enter the coordinates of the vertices :

x1, y1 : 0 0

x2, y2 : 0 100

x3, y3 : 100 0

x4, y4 : 100 100


POLYGON FILLING ALGORITHM

FILLPOLYGON(x,y,fill,old) Fill the polygon


Args: x,y  Seed points
fill New color to be filled in polygon
old  Old color/Boundary color
Begin:

Current  getpixel(x,y);
if(current==old)
Begin:
setcolor(fill);
putpixel(x,y,fill);
fillpolygon(x+1,y,fill,old);
fillpolygon(x-1,y,fill,old);
fillpolygon(x,y+1,fill,old);
fillpolygon(x,y-1,fill,old);
End;
Return;
End;
POLYGON FILLING ALGORITHM’S
C- IMPLEMENTATION
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void fillpolygon(int,int,int,int);
void main()
{
int gm,gd=DETECT,a,b;
int x[30],y[30],i,n;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n\n Enter the number of sides of polygon : ");
scanf("%d",&n);
printf("\n\n Enter the coordinates : \n");
for(i=1;i<=n;i++)
{
printf("\n x%d,y%d : ",i,i);
scanf("%d %d",&x[i],&y[i]);
}
cleardevice();
for(i=1;i<=n;i++)
{
if(i==n)
line(x[n],y[n],x[1],y[1]);
else
line(x[i],y[i],x[i+1],y[i+1]);
}
printf("\n Enter the seed point coordinate : ");
scanf("%d %d",&a,&b);
fillpolygon(a,b,4,0);
getch();
}
void fillpolygon(int x,int y,int fill,int old)
{
int current=getpixel(x,y);
if(current==old)
{
setcolor(fill);
putpixel(x,y,fill);
fillpolygon(x+1,y,fill,old);
fillpolygon(x-1,y,fill,old);
fillpolygon(x,y+1,fill,old);
fillpolygon(x,y-1,fill,old);
}
}
OUTPUT :
Enter the number of sides of the polygon : 4

Enter the coordinates of the vertices :

x1, y1 : 0 0

x2, y2 : 0 100

x3, y3 : 100 0

x4, y4 : 100 100

Enter the seed point coordinate : 50 50


SCALING ALGORITHM

SCALING(Ax,Ay,dx,dy,N) Resize the line or polygon


Args: Ax,Ay  Array containing the vertices of
line or polygon.
dx,dy  Scaling factors.
Global : DF_PEN_X,DF_PEN_Y
Local : I  Stepping through polygon sides.

BEGIN :
IF N<2 THEN RETURN ERROR “Scaling Size Error.”
DF_PEN_X  Ax[N];
DF_PEN_Y  Ay[N];
DISPLAY_FILE_ENTER(N);

FOR I=1 to N DO
LINE_ABS-2(Ax[I],Ay[I]);
FOR I=1 to N DO
LINE_ABS-2(Ax[I]*dx,Ay[I]*dy);
RETURN;
END;
SCALING ALGORITHM’S
C- IMPLEMENTATION
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define MAX 10
void main()
{
int i,j,x[MAX],y[MAX],n,dx,dy,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n\n Enter the number of points you want to insert : ");
scanf("%d",&n);
if(n<2)
{
printf("\n\n Sorry please enter minimum two coordinates...");
getch();
exit(0);
}
printf("\n\n Enter the points for scaling : \n");
for(i=0;i<n;i++)
{
printf("\n x%d,y%d : ",i+1,i+1);
scanf("%d%d",&x[i],&y[i]);
}
setcolor(6);
for(i=0;i<n;i++)
{
if(i<n-1)
line(x[i],y[i],x[i+1],y[i+1]);
else
line(x[n-1],y[n-1],x[0],y[0]);
}
printf("\n\n Enter the scaling factors dx,dy : ");
scanf("%d%d",&dx,&dy);
for(i=1;i<n;i++)
{
x[i]=x[i]*dx;
y[i]=y[i]*dy;
}
setcolor(5);
for(i=0;i<n;i++)
{
if(i<n-1)
line(x[i],y[i],x[i+1],y[i+1]);

else
line(x[n-1],y[n-1],x[0],y[0]);
}
getch();
}

OUTPUT :
Enter the number of points you want to insert : 4

Enter the coordinates of the vertices :

x1, y1 : 0 0

x2, y2 : 0 100

x3, y3 : 100 0

x4, y4 : 100 100

Enter the scaling factors dx,dy : 2 2


TRANSLATION ALGORITHM

TRANSLATION(Ax,Ay,tx,ty,N) Translate the line or


polygon
Args: Ax,Ay  Array containing the vertices of
line or polygon.
tx,ty  Translating factors.
Global : DF_PEN_X,DF_PEN_Y
Local : I  Stepping through polygon sides.

BEGIN :
DF_PEN_X  Ax[N];
DF_PEN_Y  Ay[N];
DISPLAY_FILE_ENTER(N);
FOR I=1 to N DO
LINE_ABS-2(Ax[I],Ay[I]);
FOR I=1 to N DO
LINE_ABS-2(Ax[I]+tx,Ay[I]+ty);
RETURN;
END;
TRANSLATION ALGORITHM’S
C- IMPLEMENTATION
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define MAX 10
void main()
{
int i,j,x[MAX],y[MAX],n,tx,ty,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n\n Enter the number of points you want to insert : ");
scanf("%d",&n);
printf("\n\n Enter the points for translating : \n");
for(i=0;i<n;i++)
{
printf("\n x%d,y%d : ",i+1,i+1);
scanf("%d%d",&x[i],&y[i]);
}
setcolor(6);
for(i=0;i<n;i++)
{
if(i<n-1)
line(x[i],y[i],x[i+1],y[i+1]);
else
line(x[n-1],y[n-1],x[0],y[0]);
}
printf("\n\n Enter the translating factors tx,ty : ");
scanf("%d%d",&tx,&ty);
for(i=0;i<n;i++)
{
x[i]=x[i]+dx;
y[i]=y[i]+dy;
}
setcolor(5);
for(i=0;i<n;i++)
{
if(i<n-1)
line(x[i],y[i],x[i+1],y[i+1]);
else
line(x[n-1],y[n-1],x[0],y[0]);
}
getch();
}
OUTPUT :
Enter the number of points you want to insert : 4

Enter the coordinates of the vertices :

x1, y1 : 0 0

x2, y2 : 0 100

x3, y3 : 100 0

x4, y4 : 100 100

Enter the translating factors tx,ty : 100 0


ROTATION ALGORITHM

ROTATION(Ax,Ay,A,N) Translate the line or


polygon
Args: Ax,Ay  Array containing the vertices of
line or polygon.
A  Angle of rotation.
Global : DF_PEN_X,DF_PEN_Y
Local : I  Stepping through polygon sides.

BEGIN :
DF_PEN_X  Ax[N];
DF_PEN_Y  Ay[N];
DISPLAY_FILE_ENTER(N);
FOR I=1 to N DO
LINE_ABS-2(Ax[I],Ay[I]);
FOR I=1 to N DO
BEGIN:
Ax’[I]  Ax[I]*cosA-Ay[I]*sinA; // For anti-clockwise
Ay’[I]  Ax[I]*sinA+Ay[I]*cosA; // For anti-clockwise
FOR I=1 to N DO
LINE_ABS-2(Ax’[I],Ay’[I]);
RETURN;
END;
ROTATION ALGORITHM’S
C- IMPLEMENTATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define MAX 10
int ROUND(float);
void main()
{
int i,j,f,x[MAX],y[MAX],xr[MAX],yr[MAX],n,ad,gd=DETECT,gm;
float ar,ca,sa;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n\n Enter the number of points you want to insert : ");
scanf("%d",&n);
printf("\n\n Enter the coordinates : ");
for(i=0;i<n;i++)
{
printf("\n x%d,y%d : ",i+1,i+1);
scanf("%d%d",&x[i],&y[i]);
}
setcolor(6);
for(i=0;i<n;i++)
{
if(i<n-1)
line(x[i],y[i],x[i+1],y[i+1]);
else
line(x[n-1],y[n-1],x[0],y[0]);
}
printf("\n\n Enter the rotation angle : ");
scanf("%d",&ad);
ar=((3.142159*ad)/180);
ca=cos(ar);
sa=sin(ar);
printf("\n\n Press '1' to rotate anticlocklwise & '2' for clockwise :");
scanf("%d",&f);
if(f==1)
{
for(i=1;i<n;i++)
{
xr[i]=abs((x[i]*ca)-(y[i]*sa));
yr[i]=abs((x[i]*sa)+(y[i]*ca));
}
}
else
{
for(i=1;i<n;i++)
{
xr[i]=ROUND(abs((x[i]*ca)+(y[i]*sa)));
yr[i]=ROUND(abs((y[i]*ca)-(x[i]*sa)));
}
}
setcolor(5);
for(i=0;i<n;i++)
{
if(i<n-1)
line(x[i],y[i],x[i+1],y[i+1]);
else
line(x[n-1],y[n-1],x[0],y[0]);
}
getch();
}
int ROUND(float x)
{
int y;
if(x>((int)x+0.5))
y=floor(x);
if(x<((int)x+0.5))
y=ceil(x);
return(y);
}

OUTPUT :
Enter the number of points you want to insert : 4
Enter the coordinates of the vertices :
x1, y1 : 0 0
x2, y2 : 0 100
x3, y3 : 100 0
x4, y4 : 100 100
Enter the rotation angle : 45
Press '1' to rotate anticlocklwise & '2' for clockwise : 1

You might also like