0% found this document useful (0 votes)
68 views40 pages

Lab - Manual Computer Graphics by Sarika

This document is a lab manual for interactive computer graphics programs using C/C++. It was written by Ms. Sarika Arora, Assistant Professor. The manual includes the basic structure for a graphics program and several example programs demonstrating graphics concepts like drawing lines, shapes, circles and more using different algorithms.

Uploaded by

sarika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views40 pages

Lab - Manual Computer Graphics by Sarika

This document is a lab manual for interactive computer graphics programs using C/C++. It was written by Ms. Sarika Arora, Assistant Professor. The manual includes the basic structure for a graphics program and several example programs demonstrating graphics concepts like drawing lines, shapes, circles and more using different algorithms.

Uploaded by

sarika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

LAB MANUAL

INTERACTIVE COMPUTER GRAPHICS

PROGRAMS USING C/C++

MANUAL BY:
Ms. SARIKA ARORA
ASSISTANT PROFESSOR
E-mail: [email protected]

Computer Graphics

By: Ms. Sarika Arora

Declaration

Declaration

Computer Graphics

By: Ms. Sarika Arora

Declaration

Declaration

Computer Graphics

By: Ms. Sarika Arora

Declaration

Declaration

Computer Graphics

By: Ms. Sarika Arora

Declaration

Computer Graphics

By: Ms. Sarika Arora

Declaration

Computer Graphics

By: Ms. Sarika Arora

Declaration

Basic Structure of a graphics program:


#include<stdio.h>
#include<graphics.h>//must be included for every graphics program
#include<conio.h>
#include<dos.h> //for including delay function.
void main()
{
int gd=DETECT,gm; //gd=detects best available graphics driver, gm =graphics mode.
initgraph(&gd,&gm,C:\\TC\\BGI);// for initializing graph mode
// above 2 steps are must for every graphics program.
//declaration of any variables must be done before calling initgraph() function.
// next write code for producing requiring design or drawing object
line(100,100,200,200);//draws a line segment.
getch();
}

Computer Graphics

By: Ms. Sarika Arora

1. Program to show GRAPHIC DRIVER AND MODE NAME.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int a,b;
initgraph(&gd,&gm," ");
setbkcolor(BLUE);
setcolor(GREEN);
a=getmaxx();
b=getmaxy();
line(15,15,(a-30),15);
line(15,15,15,(b-30));
printf("\n\n\tThe Graphic driver name is %s",getdrivername());
printf("\tThe Graphic mode name is %d",getgraphmode());
outtextxy(0,0,"(0,0)");
outtextxy((a-70),5,"(x-axis)");
outtextxy(10,(b-30),"(y-axis)");
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

2. Program to make SMILEY AND HALF MOON


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
clrscr();
setcolor(RED);
outtextxy(250,20,"Smily_Face");
line(250,29,330,29);
setcolor(WHITE);
ellipse(55,93,90,305,50,45);
ellipse(58,92,95,270,40,45);
setcolor(MAGENTA);
circle(400,300,60);
setfillstyle(SOLID_FILL,LIGHTRED);
floodfill(401,301,MAGENTA);
setcolor(1);
ellipse(378,270,0,360,10,15);
setcolor(1);
ellipse(418,270,0,360,10,15);
line(400,290,400,320);
setcolor(4);
arc(400,330,180,360,20);
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

3. Program to print a shape of OLYMPIC RING.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
circle(250,200,50);
setfillstyle(SOLID_FILL,RED);
floodfill(250,200,RED);
setcolor(GREEN);
circle(250,200,30);
setfillstyle(SOLID_FILL,GREEN);
floodfill(250,200,GREEN);
setcolor(BLUE);
circle(250,200,15);
setfillstyle(SOLID_FILL,BLUE);
floodfill(250,200,BLUE);
setcolor(RED);
circle(370,200,50);
setfillstyle(SOLID_FILL,RED);
floodfill(370,200,RED);
setcolor(GREEN);
circle(370,200,30);
setfillstyle(SOLID_FILL,GREEN);
floodfill(370,200,GREEN);
setcolor(BLUE);
circle(370,200,15);
setfillstyle(SOLID_FILL,BLUE);
floodfill(370,200,BLUE);
setcolor(RED);
circle(310,250,50);
setfillstyle(SOLID_FILL,RED);
floodfill(310,250,RED);
setcolor(GREEN);

Computer Graphics

By: Ms. Sarika Arora

circle(310,250,30);
setfillstyle(SOLID_FILL,GREEN);
floodfill(310,250,GREEN);
setcolor(BLUE);
circle(310,250,15);
setfillstyle(SOLID_FILL,BLUE);
floodfill(310,250,BLUE);
setcolor(YELLOW);
outtextxy(260,320,"OLYMPIC RING");
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

4. Program to print grapes on the screen.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gm,gd;
gd=DETECT;
initgraph(&gd,&gm," ");
rectangle(265,140,365,150);
setfillstyle(SOLID_FILL,BROWN);
floodfill(266,141,WHITE);
rectangle(310,150,315,158);
setfillstyle(SOLID_FILL,BROWN);
floodfill(311,151,WHITE);
ellipse(312,179,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(313,180,WHITE);
ellipse(290,179,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(291,180,WHITE);
ellipse(270,179,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(271,180,WHITE);
ellipse(335,179,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(336,180,WHITE);
ellipse(356,179,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(357,180,WHITE);
ellipse(346,210,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(347,211,WHITE);
ellipse(326,210,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(327,211,WHITE);

Computer Graphics

By: Ms. Sarika Arora

ellipse(305,210,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(306,211,WHITE);
ellipse(285,210,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(286,211,WHITE);
ellipse(336,241,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(337,242,WHITE);
ellipse(316,241,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(317,242,WHITE);
ellipse(296,241,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(297,242,WHITE);
ellipse(306,272,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(307,273,WHITE);
ellipse(326,272,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(327,273,WHITE);
ellipse(316,303,0,360,13,20);
setfillstyle(SOLID_FILL,GREEN);
floodfill(317,304,WHITE);
outtextxy(290,100,"GRAPES");
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

5. Program to draw a cycle.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"EGAVGA.BGI");
setbkcolor(YELLOW);
setcolor(DARKGRAY);
circle(150,280,70);
setfillstyle(SOLID_FILL,DARKGRAY);
floodfill(150,280,DARKGRAY);
circle(150,280,50);
circle(500,280,70);
setfillstyle(SOLID_FILL,DARKGRAY);
floodfill(500,280,DARKGRAY);
circle(500,280,50);
//seat line
setcolor(DARKGRAY);
line(150,210,500,210);
line(310,189,310,210);
line(320,189,320,210);
setcolor(RED);
setfillstyle(SOLID_FILL,BROWN);
ellipse(315,189,0
,360,25,10);
floodfill(315,189,RED);
//end seat line
setcolor(DARKGRAY);
line(500,195,500,210);
line(485,195,485,210);
setcolor(DARKGRAY);
setfillstyle(LINE_FILL,BROWN);
pieslice(492,195,0,180,30);
floodfill(492,195,DARKGRAY);
setcolor(GREEN);
setfillstyle(HATCH_FILL,BROWN);
bar3d(132,165,155,188,5,1);
floodfill(132,165,GREEN);
setcolor(DARKGRAY);
//front vertical line
line(150,150,150,210);
//handle line
line(132,122,180,190);
setcolor(BROWN);
ellipse(132,112,0,360,2,15);
setfillstyle(SOLID_FILL,BROWN);

Computer Graphics

By: Ms. Sarika Arora

floodfill(132,112,BROWN);
ellipse(180,180,0,360,2,15);
setfillstyle(SOLID_FILL,BROWN);
floodfill(180,180,BROWN);
//dia line
setcolor(DARKGRAY);
line(445,210,417,322);
setfillstyle(SOLID_FILL,BLACK);
pieslice(150,280,0,45,50);
pieslice(150,280,45,90,50);
pieslice(150,280,90,135,50);
pieslice(150,280,135,180,50);
pieslice(150,280,180,225,50);
pieslice(150,280,225,270,50);
pieslice(150,280,270,315,50);
pieslice(150,280,315,360,50);
pieslice(500,280,0,45,50);
pieslice(500,280,45,90,50);
pieslice(500,280,90,135,50);
pieslice(500,280,135,180,50);
pieslice(500,280,180,225,50);
pieslice(500,280,225,270,50);
pieslice(500,280,270,315,50);
pieslice(500,280,315,360,50);
floodfill(132,165,GREEN);
setlinestyle(DOTTED_LINE,2,8);
line(165,210,483,349);
//PADDLES
setcolor(DARKGRAY);
line(300,270,270,300);
line(320,276,348,248);
rectangle(338,235,355,248);
setfillstyle(LINE_FILL,BROWN);
floodfill(349,240,DARKGRAY);
rectangle(279,313,261,300);
setfillstyle(LINE_FILL,BROWN);
floodfill(270,310,DARKGRAY);
setcolor(RED);
rectangle(50,40,590,440);
setfillstyle(SOLID_FILL,BROWN);
floodfill(10,10,RED);
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

6. Program to draw a line using DIRECT METHOD.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,m,dx,dy,b,x1,y1,x2,y2;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
clrscr();
printf("Enter coordinates of a line");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
dy=y2-y1;
dx=x2-x1;
m=dy/dx;
b=y-(m*x);
printf("m=%d\tb=%d",m,b);
for(x=x1;x<x2;x++)
{
if(m<1)
{
y=m*x+b;
putpixel(x,y,1);
}
}
for(y=y1;y<y2;y++)
{
if(m>=1)
{
x=(y-b)/m;
putpixel(x,y,1);
}
}
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

7. Program to show working of DDA LINE ALGORITHM.


#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int x1,x2,y1,y2,m,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc\\bgi");
cleardevice();
printf("enter the values of x1 and y1\n");
scanf("%d%d",&x1,&y1);
printf("enter the value of x2 and y2\n");
scanf("%d%d",&x2,&y2);
m=(y2-y1)/(x2-x1);
printf("%d",m);
if(m<1)
{
y=y1;
for(x=x1;x<=x2;x++,y+=m)
putpixel(x,y,2);
}
else
{
x=x1;
for(y=y1;y<=y2;y++,x+=1/m)
putpixel(x,y,3);
}
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

8. Program to show working of BRESENHAM LINE DRAWING


ALGORITHM.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
int x1,x2,y1,y2,m,p,dy,dx,y,x;
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("enter the value of x1,y1\n");
scanf("%d%d",&x1,&y1);
printf("enterthe value of x2 and y2\n");
scanf("%d%d",&x2,&y2);
dy=y2-y1;
dx=x2-x1;
p=2*dy-dx;
if(p<0)
{
y=y1;
for(x=x1;x<=x2;x++,y=y)
putpixel(x,y,2);
p=p+2*dy;
}
else
{
x=x1;
for(y=y1;y<=y2;y+=1,x+=1)
putpixel(x,y,4);
p=p+2*dy-2*dx;
}
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

9. Program to show working of BRESENHAM CIRCLE DRAWING


ALGORITHM.
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void plotpixel(int,int);
int xc,yc;
void main()
{
int r,x,y,p;
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("enter the values of xc and yc\n");
scanf("%d%d",&xc,&yc);
printf("enterthe values of r\n");
scanf("%d",&r);
x=0;
y=r;
p=3-2*r;
while(x<y)
{
if(p<0)
{
x=x+1;
y=y;
plotpixel(x,y);
p=p+4*x+6;
}
else
{
x=x+1;
y=y-1;
plotpixel(x,y);
p=p+4*(x-y)+10;
}
}
getch();
closegraph();
}
void plotpixel(x,y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc+y,yc+x,2);
putpixel(xc-x,yc+y,3);
putpixel(xc-y,yc+x,4);

Computer Graphics

By: Ms. Sarika Arora

putpixel(xc-x,yc-y,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+x,yc-y,7);
putpixel(xc+y,yc-x,1);
}

Computer Graphics

By: Ms. Sarika Arora

10.Program to TRANSLATE AN OBJECT from one place to another.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,tx,ty,x1,y1,x2,y2,xt,yt,xt1,yt1;
initgraph(&gd,&gm," ");
clrscr();
printf("Enter coordinates:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
rectangle(x1,y1,x2,y2);
printf("Enter translation factor towards x-axis:");
scanf("%d",&tx);
printf("Enter translation factor towards y-axis:");
scanf("%d",&ty);
xt=x1+tx;
yt=y1+ty;
xt1=x2+tx;
yt1=y2+ty;
rectangle(xt,yt,xt1,yt1);
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

11. Program to translate a circle.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x1,y1,tx,ty,r;
initgraph(&gd,&gm," ");
printf("enter center points and radius");
scanf("%d%d%d",&x1,&y1,&r);
circle(x1,y1,r);
printf("enter translation factor");
scanf("%d%d",&tx,&ty);
x1=x1+tx;
y1=y1+ty;
printf("translated circle:");
setcolor(RED);
circle(x1,y1,r);
outtextxy(x1,y1,"translated circle");
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

12.Program to SCALE AN OBJECT.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,sx,sy,x1,y1,x2,y2,xs,ys,xs1,ys1;
initgraph(&gd,&gm," ");
setbkcolor(GREEN);
printf("Enter coordinates:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
rectangle(x1,y1,x2,y2);
printf("Enter scaling factor towards x-axis:");
scanf("%d",&sx);
printf("ENTER SCALING FACOR TOWARDS Y-AXIS:");
scanf("%d",&sy);
xs=x1*sx;
ys=y1*sy;
xs1=x2*sx;
ys1=y2*sy;
printf("\n AFTER SCALING");
rectangle(xs,ys,xs1,ys1);
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

13.Program to ROTATE AN OBJECT.


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int r,x1=150,y1=100,x2=100,y2=200,x3=200,y3=200;
float xr,xn1,yn1,xn2,yn2,xn3,yn3;
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc\\bgi");
cleardevice();
printf("enter the angle of rotation\n");
scanf("%d",&r);
cleardevice();
printf("after rotation by %d degrees",r);
outtextxy(500,200,"rotation");
xr=(r*3.14)/180;
xn1=x1*cos(xr)-y1*sin(xr);
yn1=y1*cos(xr)+x1*sin(xr);
xn2=x2*cos(xr)-y2*sin(xr);
yn2=y2*cos(xr)+x2*sin(xr);
xn3=x3*cos(xr)-y3*sin(xr);
yn3=y3*cos(xr)+x3*sin(xr);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
setcolor(3);
line(xn1,yn1,xn2,yn2);
line(xn2,yn2,xn3,yn3);
line(xn3,yn3,xn1,yn1);
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

14.Program to translate an object USING MATRIX method.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],sx,sy,b[3][3],k,sum=0,c[3][3],e,f,g,h,m,l;
clrscr();
printf("enter the value of tx,ty\n");
scanf("%d%d",&sx,&sy);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
a[i][j]=1;
}
else
{
a[i][j]=0;
}
}
}
a[0][2]=sx;
a[1][2]=sy;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("enter the values of x1,x2,x3 as vertices of triangle\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
if(i==0)
printf("enter the value of y1,y2,y3\n");
if(i==1)
printf("please enter the 1 for identical matrix\n");
}

Computer Graphics

By: Ms. Sarika Arora

e=b[0][0];
f=b[1][0];
g=b[0][1];
h=b[1][1];
m=b[0][2];
l=b[1][2];
line(e,f,g,h);
line(g,h,m,l);
line(m,l,e,f);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("after multiplication\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
e=c[0][0];
f=c[1][0];
g=c[0][1];
h=c[1][1];
m=c[0][2];
l=c[1][2];
line(e,f,g,h);
line(g,h,m,l);

Computer Graphics

By: Ms. Sarika Arora

line(m,l,e,f);
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

15.Program to scale an object USING MATRIX.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int i,j,a[3][3],sx,sy,b[3][3],k,sum=0,c[3][3],e=0,f=0,g=0,h=0,m=0,l=0;
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
clrscr();
printf("enter the value ofsx,sy\n");
scanf("%d%d",&sx,&sy);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
a[i][j]=1;
}
else
{
a[i][j]=0;
}
}
}
a[0][0]=sx;
a[1][1]=sy;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("enter the values of x1,x2,x3 as vertices of triangle\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
if(i==0)
printf("enter the value of y1,y2,y3\n");

Computer Graphics

By: Ms. Sarika Arora

if(i==1)
printf("please enter the 1 for identical matrix\n");
}
e=b[0][0];
f=b[1][0];
g=b[0][1];
h=b[1][1];
m=b[0][2];
l=b[1][2];
printf("\nbefore the scaling\n");
line(e,f,g,h);
line(g,h,m,l);
line(m,l,e,f);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("after multiplication\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
e=c[0][0];
f=c[1][0];
g=c[0][1];
h=c[1][1];

Computer Graphics

By: Ms. Sarika Arora

m=c[0][2];
l=c[1][2];
//printf("\n showing values of e:%d,f:%d,g:%d,h:%d,m:%d,l:%d\n",e,f,g,h,m,l);
printf("\nafter the scaling\n");
line(e,f,g,h);
line(g,h,m,l);
line(m,l,e,f);
getch();
closegraph();
}

Computer Graphics

By: Ms. Sarika Arora

Computer Graphics

By: Ms. Sarika Arora

You might also like