Computer Graphics Lab Programs
Computer Graphics Lab Programs
) Algorithm
#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();
}
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
#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();
initgraph(&gd,&gm,"C:\\TC\\BGI");
line_bres(x1,y1,x2,y2);
getch();
}
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);
}
}
}
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();
}
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
x1, y1 : 0 0
x2, y2 : 0 100
x3, y3 : 100 0
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
x1, y1 : 0 0
x2, y2 : 0 100
x3, y3 : 100 0
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
x1, y1 : 0 0
x2, y2 : 0 100
x3, y3 : 100 0
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
x1, y1 : 0 0
x2, y2 : 0 100
x3, y3 : 100 0
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