0% found this document useful (0 votes)
100 views48 pages

CG Practical File

This document contains the practical file submission of a student named Gunjan Kumari for the subject of Computer Graphics. It consists of 22 programs addressing various computer graphics concepts like plotting pixels and lines, drawing shapes like triangles and rectangles, and performing transformations like translation, rotation, and scaling on lines, triangles, and rectangles. The programs are written in C language and utilize graphics libraries. Each program is numbered, contains the code, input/output and is signed by the student and guide.

Uploaded by

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

CG Practical File

This document contains the practical file submission of a student named Gunjan Kumari for the subject of Computer Graphics. It consists of 22 programs addressing various computer graphics concepts like plotting pixels and lines, drawing shapes like triangles and rectangles, and performing transformations like translation, rotation, and scaling on lines, triangles, and rectangles. The programs are written in C language and utilize graphics libraries. Each program is numbered, contains the code, input/output and is signed by the student and guide.

Uploaded by

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

KAMAL INSTITUTE OF HIGHER EDUCATION

AND ADVANCE TECHNOLOGY

(Affiliated to Guru Gobind Singh Indraprastha University)

K-1 Extension, Mohan Garden, New Delhi-110059)

PRACTICAL FILE

“COMPUTER GRAPHICS”
Submitted by

GUNJAN KUMARI M
BCA 5th SEM.
35196702020
(Batch 2020-2023)

Under the guidance of

Mr. Davinder Singh


Index
S.no Particular Signature
01. WAP to plot a pixel.
02. WAP to plot 3 pixels with delay.
03.
WAP to draw a line through DDA algorithm.
04. WAP to draw a line through BREHESEM line
algorithm.
05. WAP to make a triangle using 3 lines.
06. WAP to perform translation of a line.
07.
WAP to perform a translation of triangle.
08.
WAP to perform a translation of a rectangle.
09. WAP to perform a rotation of Line.
10. WAP to perform a rotation of triangle.
11.
WAP to perform a rotation of a rectangle.
12. WAP to perform a scalene of triangle.
13. WAP to perform a scalene of rectangle.
14. WAP to perform a shearing of triangle.
15.
WAP to perform a shearing of rectangle.
16. WAP to make a fish.
17. WAP to make a hut.
18. WAP to make a kite.
19. WAP to draw a circle using Mid-Point
Algorithm.
20. WAP to draw a circle using
BRESENHEM’S Algorithm.
21. WAP to draw a FLAG.
22. WAP to draw a SMILEY.

Program No. 1:- Write a program to plot a pixel.

graphics Driver : It is a pointer to an integer specifying the graphics driver to be used. It


tells the compiler that what graphics driver to use or to automatically detect the drive. In
all our programs we will use DETECT macro of graphics.h library that instruct compiler
for auto detection of graphics driver.
graphics Mode : It is a pointer to an integer that specifies the graphics mode to
be used. If *gdriver is set to DETECT, then initgraph sets *gmode to
the highest resolution available for the detected driver.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h> void

main()

int gdriver=DETECT,gmode,x,y; initgraph(&gdriver,&gmode,"c:\\bgi");

printf("Enter the coordinates of x"); scanf("%d",&x); printf("Enter the

coordinates of y"); scanf("%d",&y); putpixel(x,y,10); //putpixel(int x, int

y, int color); //putpixel(x, y, RED); getch(); closegraph();

}
Output:-
Program No. 2:- Write a program 3 pixels with delay.
#include<conio.h>

#include<stdio.h>

#include<graphics.h>

#include<dos.h> void

main()

int g=DETECT,d,x,y,x1,y1,x2,y2;

initgraph(&g,&d,"c:\\bgi");

printf("Enter the coordinates of x");

scanf("%d",&x); printf("Enter the

coordinates of y"); scanf("%d",&y);

putpixel(x,y,10); delay(200);

printf("Enter the coordinates of x1");

scanf("%d",&x1); printf("Enter the

coordinates of y1"); scanf("%d",&y1);

putpixel(x1,y1,20); delay(400);

printf("Enter the coordinates of x2");

scanf("%d",&x2); printf("Enter the

coordinates of y2"); scanf("%d",&y2);

putpixel(x2,y2,30); delay(400);

getch(); closegraph();

}
Output:-
Program No. 3:- Write a program to draw a line through DDA algorithm.
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<math.h> void

main()

int x1,y1,x2,y2,len; int

gdriver=DETECT,gmode,i;

float incx,incy,x,y; clrscr();

initgraph(&gdriver,&gmode,"c:\\bgi");

printf("enter the coordinates of x1");

scanf("%d",&x1); printf("enter the

coordinates of y1"); scanf("%d",&y1);

printf("enter the coordinates of x2");

scanf("%d",&x2); printf("enter the

coordinates of y2"); scanf("%d",&y2);

len=abs(x2-x1); if(abs(y2-y1)>len)

{
len=abs(y2-y1);

incx=(x2-x1)/len;

incy=(y2-y1)/len;

for(i=1;i<=len;i++)

{
putpixel(x,y,9);

x=x+incx; y=y+incy;

delay(100);

getch(); closegraph();

Output:-
Program No. 4:- Write a program to draw a line through BREHESEM line
algorithm.

#include<conio.h>

#include<stdio.h>

#include<graphics.h>

#include<dos.h>

#include<math.h> void

main()

int gd=DETECT,gm;

int x1,x2,y1,y2,dx,dy,i,flag,d,x,y,t,s1,s2;

clrscr(); initgraph(&gd,&gm,"c:\\bgi");

printf("enter the values of x1");

scanf("%d",&x1); printf("enter the values

of y1"); scanf("%d",&y1); printf("enter

the values of x2"); scanf("%d",&x2);

printf("enter the values of y2");

scanf("%d",&y2); dx=abs(x2-x1);

dy=abs(y2-y1); x=x1,y=y1; if((x2-x1)>0)

s1=1; else

s1=-1; if((y2-

y1)>0) s2=1;

else s2=-1;

if(dy>dx)

{
t=dx;

dx=dy; dy=t;

flag=1;

else flag=0; d=(2*dy)-dx;

outtextxy(x1,y1,"(x1,y1)");

outtextxy(x2,y2,"(x2,y2)");

i=1; a:

putpixel(x,y,9);

delay(40);

while(d>=0)

{
if(flag==1)

x=x+s1; else

y=y+s1; d=d-

2*dx;

if(flag==1)

y=y+s2; else

x=x+s2;

d=d+2*dy;

i++;

if(i<=dx) goto

a; getch();

closegraph();

}
Output:-
Program No. 5:- Write a program to make a triangle using 3 lines.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h> void

main()

int gdriver=DETECT,gmode,x,y,x1,y1,x2,y2;

initgraph(&gdriver,&gmode,"c:\\bgi");

line(100,100,160,160); line(100,100,150,90);

line(160,160,150,90); getch(); closegraph();

Output:-

s
Program No. 6:- Write a program to perform stranslation of a line.

#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdio.h> void
main()
{
int x1,y1,x2,y2,tx,ty; int g=DETECT,d;
initgraph(&g,&d,"c:\\bgi"); printf("enter
the value of x1:"); scanf("%d",&x1);
printf("enter the value of y1:");
scanf("%d",&y1); printf("enter the value
of x2:"); scanf("%d",&x2); printf("enter
the value of y2:"); scanf("%d",&y2);
line(x1,y1,x2,y2); getch(); cleardevice();
printf("enter the value of translation\n");
printf("enter tha value of tx");
scanf("%d",&tx); printf("enter tha value of
ty"); scanf("%d",&ty); printf("\n The
translated object is:\n");
line(x1+tx,y1+ty,x2+tx,y2+ty); getch();
closegraph();
}
Output:-
Program No. 7:- Write a program to perform a translation of triangle.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<math.h> void

main()

int x1,x2,y1,y2,x3,y3,x4,tx,ty; int

gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"c:\\bgi");

printf("Enter the coordinates of x1:");

scanf("%d",&x1); printf("Enter the

coordinates of y1:"); scanf("%d",&y1);

printf("Enter the coordinates of x2:");

scanf("%d",&x2); printf("Enter the

coordinates of y2:"); scanf("%d",&y2);

printf("Enter the coordinates of x3");

scanf("%d",&x3); printf("Enter the

coordinates of y3"); scanf("%d",&y3);

line(x1,y1,x2,y2);

x3=x2-100; line(x2,y2,x3,y2);

line(x3,y2,x1,y1); getch(); cleardevice();

printf("\nEnter the value for Translation:");

printf("\nEnter the value of tx:");


scanf("%d",&tx); printf("\nEnter the value of

ty:"); scanf("%d",&ty); printf("\nThe

Translated object is:");

line(x1+tx,y1+ty,x2+tx,y2+ty);

line(x2+tx,y2+ty,x3+ty,y2+ty);

line(x3+tx,y2+ty,x1+tx,y1+ty); getch();

closegraph();

}
Output:-
Program No. 8:- Write a program to perform a translation of a rectangle.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h> void
main()
{
int x1,y1,x2,y2,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1); printf("Enter the
coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of x2:");
scanf("%d",&x2); printf("Enter the
coordinates of y2:"); scanf("%d",&y2);
rectangle(x1,y1,x2,y2); getch();
cleardevice(); printf("Enter the value for
Translation:\n"); printf("Enter the value of
tx:"); scanf("%d",&tx); printf("Enter the
value of ty:"); scanf("%d",&ty);
printf("Translated object is:");
rectangle(x1,y1+ty,x2+tx,y2+ty); getch();
closegraph();
}
Output:-
Program No. 9:- Write a program to perform a rotation of Line.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h> void
main()
{
int x1,y1,x2,y2,rx,ry; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1); printf("Enter the
coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of x2:");
scanf("%d",&x2); printf("Enter the
coordinates of y2:"); scanf("%d",&y2);
line(x1,y1,x2,y2); getch(); cleardevice();
printf("Enter the value for Translation\n");
printf("Enter the value of rx:\n");
scanf("%d",&rx);
printf("Enter the value of ry:\n");
scanf("%d",&ry); printf("Rotated
object is:"); line(x1,y1,rx,ry);
getch(); closegraph();
}

Output:-
Program No. 10:- Write a program to perform a rotation of triangle.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<math.h> void

main()

{
int x1,x2,y1,y2,x3,a; int

gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"c:\\bgi");

printf("Enter the coordinates for x1:");

scanf("%d",&x1); printf("Enter the

coordinates for y1:"); scanf("%d",&y1);

printf("Enter the coordinates for x2:");

scanf("%d",&x2); printf("Enter the

coordinates for y2:"); scanf("%d",&y2);

line(x1,y1,x2,y2); x3=x2-100;

line(x2,y2,x3,y2); line(x3,y2,x1,y1);

getch();

cleardevice(); printf("Enter the angle for

rotation\n"); scanf("%d",&a);

a=a*(3.14/180); x1=((x1*cos(a))-

(y1*sin(a))); y1=((x1*sin(a))+

(y1*cos(a))); x1=((x2*cos(a))-

(y2*sin(a))); x1=((x2*sin(a))+

(y2*cos(a))); line(x1,y1,x2,y2);

line(x2,y2,x3,y2); line(x3,y2,x1,y1);

getch(); closegraph();

}
Output:-
Program No. 11:- Write a program to perform a rotation of a rectangle.

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int x1,x2,y1,y2,x3,a; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates for x1:");
scanf("%d",&x1); printf("Enter the
coordinates for y1:"); scanf("%d",&y1);
printf("Enter the coordinates for x2:");
scanf("%d",&x2); printf("Enter the
coordinates for y2:"); scanf("%d",&y2);
rectangle(x1,y1,x2,y2); getch();
cleardevice(); printf("Enter the angle for
rotation\n"); scanf("%d",&a);
a=a*(3.14/180); x1=((x1*cos(a))-
(y1*sin(a))); y1=((x1*sin(a))+
(y1*cos(a))); x1=((x2*cos(a))-
(y2*sin(a))); x1=((x2*sin(a))+
(y2*cos(a))); rectangle(x1,y1,x2,y2);
getch(); closegraph();
}

Output:-
s
Program No. 12:- Write a program to perform a scalene of triangle.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int x1,x2,y1,y2,x3,y3,x4,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1); printf("Enter the
coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of x2:");
scanf("%d",&x2); printf("Enter the
coordinates of y2:"); scanf("%d",&y2);
line(x1,y1,x2,y2); x3=x2-100;
line(x1,y1,x3,y2); line(x3,y2,x2,y2); getch();
cleardevice(); printf("\nEnter the value for
SCALLING:"); printf("\nEnter the value of
tx:"); scanf("%d",&tx); printf("\nEnter the
value of ty:"); scanf("%d",&ty);
printf("\nThe Scaled object is:");
line(x1,y1,x2+tx,y2);
line(x1,y1,x3-ty,y2); line(x3-
tx,y2,x2+tx,y2); getch();
closegraph();
}

Output:-
Program No. 13:- Write a program to perform a scalene of rectangle.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int x1,x2,y1,y2,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1); printf("Enter the
coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of x2:");
scanf("%d",&x2); printf("Enter the
coordinates of y2:"); scanf("%d",&y2);
rectangle(x1,y1,x2,y2); getch();
cleardevice(); printf("Enter the values for
SCALING:\n") printf("Enter the value of
tx:") scanf("%d",&tx); printf("Enter the
vaue of ty:"); scanf("%d",&ty);
printf("Scaled object is:");
rectangle(x1,y1+ty,x2+tx,y2+ty); getch();
closegraph();
}
Output:-
Program No. 14:- Write a program to perform a shearing of triangle.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2,x3,x4,y3,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1);
printf("Enter the coordinates of x2:"); scanf("%d",&x2);
printf("Enter the coordinates of y1:"); scanf("%d",&y1);
printf("Enter the coordinates of y2:");
scanf("%d",&y2);
line(x1,y1,x2,y2);
x3=x2-100;
line(x2,y2,x3,y2);
line(x3,y2,x1,y1);
getch(); cleardevice();
printf("Enter the value for shearing:\n");
printf("Enter the value of tx:");
scanf("%d",&tx); printf("Enter the value
of ty:"); scanf("%d",&ty); printf("Sheared
object is:"); line(x1+tx,y1+ty,x2,y2);
line(x2,y2,x3,y2);
line(x3,y2,x1+tx,y1+ty); getch();
closegraph();
}

Output:-
s
Program No. 15:- Write a program to perform a shearing of rectangle.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2,tx,ty; int
gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
printf("Enter the coordinates of x1:");
scanf("%d",&x1);
printf("Enter the coordinates of y1:"); scanf("%d",&x2);
printf("Enter the coordinates of x2:"); scanf("%d",&y1);
printf("Enter the coordinates of y2:");
scanf("%d",&y2); rectangle(x1,y1,x2,y2);
getch(); cleardevice();
printf("Enter the values or shearing:\n");
printf("enter value of tx:");
scanf("%d",&tx); printf("enter
value of ty:"); scanf("%d",&ty);
printf("sheared object is:");
rectangle(x1,y1,x2+tx,y2+ty);
getch();
closegraph();
}

Output:-
s

Program No. 16:- Write a program to make a fish.


#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
int g=DETECT,d;
initgraph(&g,&d,"c:\\bgi");
ellipse(200,200,0,360,50,30);
line(250,200,280,170);
line(280,170,280,230);
line(280,230,250,200);
circle(160,190,3); getch();
closegraph();
}

Output:-

Program No. 17:- Write a program to make a hut.


#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int g=DETECT,d;
initgraph(&g,&d,"c:\\bgi");
setcolor(6);
rectangle(50,180,150,300);
rectangle(150,180,320,300);
rectangle(80,250,120,300);
line(100,100,50,180);
line(100,100,150,180);
line(100,100,300,100);
line(300,100,320,180); getch();
closegraph();
}

Output:-
s
Program No. 18:- Write a program to make a kite.

#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\bgi");
line(100,100,50,180);
line(100,100,150,180);
line(50,180,100,250);
line(150,180,100,250);
line(100,100,100,250);
line(50,180,150,180);
line(100,250,70,300);
line(100,250,130,300);
line(70,300,130,300);
line(100,300,120,320);
line(120,320,80,340);
line(80,340,120,360);
line(120,360,80,380); setcolor(4);
getch();
closegraph();
}

Output:-

s
Program No. 19:- Write a program to draw a circle using MidPoint
Algorithm.
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdio.h>
void plotpixel (int x,int y,int x1,int y1);
void main()
{
int x,y,x1,y1,p,r; int
g=DETECT,d;
initgraph(&g,&d,"c:\\bgi");
printf("enter the coordinates of x1,y1,&radius");
scanf("%d%d%d%",&x1,&y1,&r);
x=0;
y=5;
plotpixel(x,y,x1,y1);
p=1-r;
while(x<y)
{
if(p<0)
{
p=p+(2*x)+3; x+
+;
plotpixel(x,y,x1,y1);
}
else
{
p=p+2*(x-y)+5;
x++;
y--;
plotpixel(x,y,x1,y1);
}
}
getch();
closegraph();
}
void plotpixel(int x,int y, int x1, int y1)
{
putpixel(x1+x,y+y1,2); putpixel(x1-
x,y1+y,2); putpixel(x1+x,y1-y,2);
putpixel(x1-x,y1-y,2); putpixel(x1+y,y1+x,2);
putpixel(x1-y,y1+x,2); putpixel(x1+y,y1-x,2);
putpixel(x1-y,y1-x,2);
}
Output:-
Program No. 20:- Write a program to draw a circle using BRESENHEM’S
Algorithm.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<math.h>

int h,k; void plotpixel(int

x,int y)

putpixel(x+h,y+k,7);

putpixel(y+h,x+k,7);

putpixel(-y+h,x+k,7);

putpixel(-x+h,y+k,7);

putpixel(-x+h,-y+k,7);

putpixel(-y+h,-x+k,7);

putpixel(y+h,-x+k,7);

putpixel(x+h,-y+k,7);

void bren_circle(int r)

int x=0,y=r,a=3-(2*r);

while(x<=y)

delay(100);
if(a<0) a=a+(4*x)

+6; else

a=a+(4*(x-y))+10;

y--;

x++; plotpixel(x,y);

void main()

int gd=DETECT,gm,r;

initgraph(&gd,&gm,"c:\\bgi");

printf("enter the radious of circle");

scanf("%d",&r); h=r+100; k=r+100;

bren_circle(r); getch();

}
Output:-
Program No. 21:- Write a program to draw a FLAG.
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h> void
main()
{
int g=DETECT,d;
initgraph(&g,&d,"c:\\bgi");
setcolor(5);
line(20,50,20,180);
line(20,50,100,50);
line(20,70,100,70);
line(20,90,100,90);
line(20,110,100,110);
line(100,50,100,110);
circle(60,80,10);
line(52,80,68,80);
line(60,72,60,88);
setfillstyle(SOLID_FILL,22);
floodfill(21,51,5);
setfillstyle(SOLID_FILL,7);
floodfill(21,75,5);
setfillstyle(SOLID_FILL,2);
floodfill(21,95,5);
setfillstyle(SOLID_FILL,7);
floodfill(81,75,5);
setfillstyle(SOLID_FILL,7);
floodfill(61,81,5); getch();
closegraph();
}
Output:-
Program No. 22:- Write a program to draw a SMILEY.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h> void

main()

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\bgi");

circle(100,100,50);

circle(80,80,10);

circle(120,80,10);

line(100,90,100,115);

arc(100,115,180,360,15);

getch(); closegraph();

}
Output:-

You might also like