Professional Documents
Culture Documents
"Computer Graphics & Animation": Practical File ON
"Computer Graphics & Animation": Practical File ON
ON
“COMPUTER GRAPHICS & ANIMATION”
Practical No. 1
Aim: Write a program to implement DDA line algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gd = DETECT,gm,x1,y1,x2,y2,i,dx,dy,length;
float xinc,yinc,x,y;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
cleardevice();
printf("Enter the values of x1 and y1\n");
scanf("%d%d",&x1,&y1);
printf("Enter the values of x2 and y2\n");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
length = (abs(dy)>abs(dx))?abs(dy):abs(dx);
xinc = (float)dx/length;
yinc = (float)dy/length;
x = x1;
y = y1;
for(i=0;i<=length;i++)
{
putpixel(x,y,2);
x = x + xinc;
y = y + yinc;
}
getch();
closegraph();
}
Output:
Practical No. 2
#include<stdio.h>
#include<graphics.h>
#include <stdio.h>
#include <graphics.h>
void drawCircle(int xc, int yc, int x, int y) {
putpixel(xc+x, yc+y, GREEN);
putpixel(xc-x, yc+y, GREEN);
putpixel(xc+x, yc-y, GREEN);
putpixel(xc-x, yc-y, GREEN);
putpixel(xc+y, yc+x, GREEN);
putpixel(xc-y, yc+x, GREEN);
putpixel(xc+y, yc-x, GREEN);
putpixel(xc-y, yc-x, GREEN);
}
void circleBres(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x) {
x++;
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
}
}
int main()
{
int xc, yc, r ;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
printf("Enter x and y of center:\n");
scanf("%d %d",&xc,&yc);
printf("Enter radius of circle:\n");
scanf("%d",&r);
circleBres(xc, yc, r);
return 0;
}
Output:
Practical No. 4
#include <stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,x_cen=360,y_cen=180,rad,i,j,x[10],y[10];
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the radius");
scanf("%d",&rad);
for(i=0;i<10;i++)
{
x[i]=x_cen+rad*cos(36*i*(3.14/180));
y[i]=y_cen+rad*sin(36*i*(3.14/180));
}
for(i=0;i<10;i++)
{
line(x[i],y[i],x[(i+1)%10],y[(i+1)%10]);
}
getch();
}
Output:
Practical No. 5
Aim: Write a program to draw a Decagon whose all vertices are connected
with every other vertex using lines.
#include <stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,x_cen=360,y_cen=180,rad,i,j,x[10],y[10];
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the radius:\n");
scanf("%d",&rad);
for(i=0;i<10;i++)
{
x[i]=x_cen+rad*cos(36*i*(3.14/180));
y[i]=y_cen+rad*sin(36*i*(3.14/180));
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
line(x[i],y[i],x[j],y[j]);
}
}
getch();
}
Output:
Practical No. 6
Aim: Write a program to show a ship moving using the concept of 2-D
transformations.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{ int gd=DETECT,gm,x1=20,y1=20,x2=60,y2=20,x3=50,y3=40,x4=30,y4=40;
int dir,xd=1,yd=0,a,b,c,d,xf,yf,sign=1,xmax,ymax;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
cleardevice();
xmax=getmaxx();
ymax=getmaxy();
dir=1;
while(!kbhit())
{
delay(3);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
line(x4,y4,x1,y1);
x1=x1+xd;
x2=x2+xd;
x3=x3+xd;
x4=x4+xd;
y1+=yd;
y2+=yd;
y3+=yd;
y4+=yd;
xf=x4;
yf=y4;
if(x2==(xmax-40))
{
xd=0;
yd=1;
dir=2;
sign=1;
}
if(y2==(ymax-20))
{
xd=-1;
yd=0;
dir=2;
sign=-1;
}
if(x1==15)
{
yd=-1;
xd=0;
dir=2;
sign=1;
}
if(y1==15)
{
xd=1;
yd=0;
dir=2;sign=-1;
}
if(dir!=1)
{
a=x1;
b=x2;
c=x3;
d=x4;
x1=xf-sign*(y1-yf);
y1=yf+sign*(a-xf);
x2=xf-sign*(y2-yf);
y2=yf+sign*(b-xf);
x3=xf-sign*(y3-yf);
y3=yf+sign*(c-xf);
x4=xf-sign*(y4-yf);
y4=yf+sign*(d-xf);
dir=1;
}
}
getch();
}
Output:
Practical No. 7
Aim: Write a program to show a Ball moving on the screen according to the
given requirements.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y,r,xd=1,yd=1,xc,yc;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
cleardevice();
printf("Enter the center of the circle:\n");
scanf("%d%d",&x,&y);
printf("Enter radius of the circle:\n");
scanf("%d",&r);
circle(x,y,r);
xc=x;
yc=y;
while(!kbhit()){
xc=xc+xd;
yc=yc+yd;
if(x>xc){
xd=1;
}
else{
if((xc+r)>=getmaxx()||(xc-r)<0)
xd=xd*-1;
}
if(x>yc){
yd=1;
}
else{
if((yc+r)>=getmaxy()||(yc-r)<0)
yd=yd*-1;
}
circle(xc,yc,r);
delay(3);
cleardevice();
}
getch();
}
Output:
Practical No. 8
#include<graphics.h>
#include<conio.h>
int xc,yc,r,p,x,y;
void main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TurboC3\\BGI");
cleardevice();
printf("Enter the center of circle:\n");
scanf("%d%d",&xc,&yc);
printf("Enter the radius of circle:\n");
scanf("%d",&r);
x=0;
y=r;
plotpoint();
p=1-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;
}
plotpoint();
}
getch();
}
plotpoint(){
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
return 0;
}
Output:
Practical No. 9
#include<stdio.h>
#include<conio.h>
#include <graphics.h>
Aim: Write a program to implement Boundry fill algorithm with two figures.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void boundryFill(int x,int y,int border,int fill)
{
int xc=getpixel(x,y);
if(xc!=border && xc!=fill){
xc=x;
while(getpixel(x,y)!=border)
putpixel(x++,y,fill);
x=xc-1;
while(getpixel(x,y)!=border)
putpixel(x--,y,fill);
boundryFill(xc,y+1,border,fill);
boundryFill(xc,y-1,border,fill);
}
}
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,cx,cy,r;
initgraph( &gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
printf("Enter the center of Circle (x,y) & radius : ");
scanf("%d %d %d",&cx,&cy,&r);
circle(cx,cy,r);
printf("Enter the co-ordinates of rectangle : ");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
rectangle(x1,y1,x2,y2);
boundryFill(cx,cy,WHITE,LIGHTGREEN);
boundryFill(x1+2,y1+2,WHITE,RED);
getch();
closegraph();
}
Output:
Practical No. 11
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x,y,minx=200,miny=150,maxx=400,maxy=300;
initgraph( &gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
rectangle(minx,miny,maxx,maxy);
printf("Enter the co-ordinates of point : ");
scanf("%d %d",&x,&y);
if(x<maxx&&x>minx)
{
if(y<maxy&&y>miny)
{
printf("Point is inside the clipping window");
putpixel(x,y,15);
circle(x,y,2);
}
}
else
printf("Point is outside the clipping window");
getch();
closegraph();
}
Output:
Practical No. 12
#include<graphics.h>
#include<stdio.h>
#define LEFT 1
#define RIGHT 2
#define BELOW 4
#define ABOVE 8
int left, top, right, bottom,x1,y1,x2,y2,flag=0;
void printtheline(int region1,int region2);
void getpoint(int *x1,int *y1);
float m,c;
void main()
{
int gdriver = DETECT, gmode, errorcode;
int region1,region2;
initgraph(&gdriver, &gmode, "C:\\TurboC3\\BGI");
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
printf("Enter Co-ordinates of the window\n");
fflush(stdin);
scanf("%d %d %d %d",&left,&top,&right,&bottom);
printf("\nEnter Cordinates of first end pt \n");
fflush(stdin);
scanf("%d %d",&x1,&y1);
printf("\nEnter Cordinates of first end pt \n");
fflush(stdin);
scanf("%d %d",&x2,&y2);
cleardevice();
rectangle(left,top,right,bottom);
gotoxy(1,1);
m=(y2-y1)/(x2-x1);
c= y1-m*x1;
region1=calculateregion(x1,y1);
gotoxy(50,1);
region2=calculateregion(x2,y2);
setlinestyle(DOTTED_LINE,1, 1);
line(x1,y1,x2,y2);
setlinestyle(CENTER_LINE,1, 1);
printtheline(region1,region2);
getch();
closegraph();
}
int calculateregion(int x,int y)
{
int total=0;
if(x<left)
{
total +=LEFT;
}
else if (x>right)
{
total+=RIGHT;
}
if(y<top)
{
total+=ABOVE;
}
else if (y>bottom)
{
total+=BELOW;
}
return total;
}
void printtheline(int region1,int region2)
{
if (region1==0 && region2==0)
{
outtextxy(1,getmaxy(),"Line Completely inside the window.");
line(x1,y1,x2,y2);
return;
}
else if ((region1 & region2)!=0)
{
outtextxy(1,getmaxy(),"Line Completely Ouside the window.");
line(x1,y1,x2,y2);
return;
}
else
{
putpixel(x1,y1,RED);
circle(x1,y1,4);
if (region1 !=0)
{
getpoint(&x1,&y1);
putpixel(x1,y1,YELLOW);
circle(x1,y1,4);
}
putpixel(x2,y2,RED);
circle(x2,y2,4);
if (region2 !=0)
{
getpoint(&x2,&y2);
putpixel(x2,y2,YELLOW);
circle(x2,y2,4);
}
line(x1,y1,x2,y2);
//for special case
region1=calculateregion(x1,y1);
region2=calculateregion(x2,y2);
if(region1==0&& region2==0)
printf("Line clipped");
else
printf("Line outside window");
}
}
void getpoint(int *x1,int *y1)
{
if(*x1<left)
{
*y1+=m*(left- *x1);
*x1=left;
if(*y1<top)
{
*x1+=(top-*y1)/m;
*y1=top;
}
else if(*y1>bottom)
{
*x1+=(bottom-*y1)/m;
*y1=bottom;
} //y=mx+c
}
else if(*x1>right)
{
*y1+=m*(right- *x1);
*x1=right;
if(*y1<top)
{
*x1+=(top-*y1)/m;
*y1=top;
}
else if(*y1>bottom)
{
*x1+=(bottom-*y1)/m;
*y1=bottom;
} //y=mx+c
}
else
{
if(*y1<top)
{
*x1+=(top-*y1)/m;
*y1=top;
}
else if(*y1>bottom)
{
*x1+=(bottom-*y1)/m;
*y1=bottom;
}
}
}
Output: