DPG School of Technology & Management
(A unit of DPG Degree College,Sec-34,Gurugram)
(Affiliated to MDU. Rohtak)
Recognized 2(f) by UGC & Accredited with 'A' Grade by NAAC
Master of Computer Applica on 1st Semester
Session 2024-2026
Computer Graphics & Mul media
(20MCA21CL1 based on paper 20MCA21C3)
Submitted To: Submitted by: KUSHAL
Mr. Aadi Student I’d no: 145793
(Assistant Professor)
1
Cer ficate
This is to certify that KUSHAL S/o Mr. Rajesh kumar has
submi ed a prac cal file for fulfilment of MCA 1st semester lab course for
Computer Graphics & Mul media.
Submitted To: Submitted By: kushal
Mr. Aadi Student I'd no: 145793
(Assistant Professor)
2
INDEX
[Link] Programs [Link] REMARKS
Write a Program to draw basic
1 graphics construc on like line, circle, 1-4
arc, ellipse, and rectangle.
Write a program to draw anima on
2 using increasing circles filled with 5-8
different colors and pa erns.
Program to make sccreen saver that
display different size circles filled with
3 9-11
different colors and at random
places.
Write a func on to make a moving 12-
4
car using inbuilt func ons. 15
Write a program to implement digital 16-
5
clock. 17
Write a program to implement 18-
6
bouncing ball in ver cal direc on. 20
Write a program of
21-
7 transla on,rota on and scaling using
24
composite transforma ons.
Write a program to draw Bezier 25-
8
curve. 27
Program to rotate a rectangle about 28-
9
its midpoint. 31
Program to clip a line using Liang 32-
10
Barsky method. 36
3
Program 1
Write a program to draw basic graphics construc on like line,circle,arc
,eclipse and rectangle.
Source code:-
1. #include<graphics.h>
2. #include<stdio.h>
3. #include<stdlib.h>
4. #include<conio.h>
5. void main()
6. {
7. intgd=DETECT,gm;
8. initgraph (&gd,&gm,"c:\\tc\\bgi");
9. setbkcolor(GREEN);
10. prin ("\t\t\t\n\nLINE");
11. line(50,40,190,40);
12. prin ("\t\t\n\n\n\nRECTANGLE");
13. rectangle(125,115,215,165);
14. prin ("\t\t\t\n\n\n\n\n\n\nARC");
15. arc(120,200,180,0,30);
16. prin ("\t\n\n\n\nCIRCLE");
17. circle(120,270,30);
18. prin ("\t\n\n\n\nECLIPSE");
19. ellipse(120,350,0,360,30,20);
20. getch();
21.}
4
5
Program 2
Write a program to draw anima on using increasing circles filled with
different colors and pa erns.
Source code:-
1. #include<graphics.h>
2. #include<stdlib.h>
3. #include<conio.h>
4. void main()
5. {
6. intgd=DETECT, gm, i, x, y;
7. initgraph(&gd, &gm, "C:\\TC\\BGI");
8. x=getmaxx()/3;
9. y=getmaxx()/3;
10. setbkcolor(WHITE);
11. setcolor(BLUE);
12. for(i=1;i<=8;i++)
13. {
14. se illstyle(i,i);
15. delay(20);
16. circle(x, y, i*20);
17. floodfill(x-2+i*20,y,BLUE);
18. }
19. getch();
20. closegraph();
21.}
6
7
Program 3
Write a program to make screen saver that display different size circles filled
With different colors and at random places.
Source code:-
1. #include<stdio.h>
2. #include<conio.h>
3. #include"graphics.h"
4. #include"stdlib.h"
5. void main()
6. {
7. intgd=DETECT,gm,i=0,x,xx,y,yy,r;
8. //Ini alizes the graphics system
9. initgraph(&gd,&gm,"c:\\tc\\bgi");
10. x=getmaxx();
11. y=getmaxy();
12. while(!kbhit())
13. {
14. i++;
15. // se illstyle(random(i),random(30));
16. circle(xx=random(x),yy=random(y),random(30));
17. se illstyle(random(i),random(30));
18. floodfill(xx,yy,getmaxcolor());
19. delay(200);
20. }
21. getch();
22.}
8
9
Program 4
Write a program to make a moving-coloured car using inbuilt func ons.
Source code:-
1. #include<graphics.h>
2. #include<stdio.h>
3. #include<stdlib.h>
4. #include<conio.h>
5. int main()
6. {
7. intgd=DETECT,gm, i, maxx, cy;
8. initgraph(&gd, &gm, "C:\\TC\\BGI");
9. setbkcolor(WHITE);
10. setcolor(RED);
11. maxx = getmaxx();
12. cy = getmaxy()/2;
13. for(i=0;i<maxx-140;i++)
14. {
15. cleardevice();
16. line(0+i,cy-20, 0+i, cy+15);
17. line(0+i, cy-20, 25+i, cy-20);
18. line(25+i, cy-20, 40+i, cy-70);
19. line(40+i, cy-70, 100+i, cy-70);
20. line(100+i, cy-70, 115+i, cy-20);
21. line(115+i, cy-20, 140+i, cy-20);
22. line(0+i, cy+15, 18+i, cy+15);
23. circle(28+i, cy+15, 10);
10
24. line(38+i, cy+15, 102+i, cy+15);
25. circle(112+i, cy+15,10);
26. line(122+i, cy+15 ,140+i,cy+15);
27. line(140+i, cy+15, 140+i, cy-20);
28. rectangle(50+i, cy-62, 90+i, cy-30);
29. se illstyle(1,BLUE);
30. floodfill(5+i, cy-15, RED);
31. se illstyle(1, LIGHTBLUE);
32. floodfill(52+i, cy-60, RED);
33. delay(10);
34. }
35. getch();
36. closegraph();
37. return 0;
38.}
11
Program 5
Write a program to implement digital clock.
Source code:-
1. #include<stdio.h>
2. #include<conio.h>
3. #include<stdlib.h>
4. #include<graphics.h>
5. #include<dos.h>
6.
7. struct me t;
8. void display(int,int,int);
9. void main()
10.{
11. int i=0,gd=DETECT,gm,hr,min,sec;
12. clrscr();
13. initgraph(&gd,&gm,"c:\\turboc3\\bgi");
14. setcolor(GREEN);
15. se extstyle(4,0,7);
16.
17. while(!kbhit())
18. {
19. ge me(&t);
20. hr=t. _hour;
21. min=t. _min;
22. sec=t. _sec;
23. i++;
12
24.
25. display(100,100,hr);
26. display(200,100,min);
27. display(300,100,sec);
28. sound(400);
29. delay(30);
30. nosound();
31. delay(930);
32. cleardevice();
33. }
34. getch();
35.}
[Link] display(int x,int y,int num)
37.{
38.
39. char str[3];
40. itoa(num,str,10);
41.
42. se extstyle(4,0,7);
43.
44. ou extxy(180,100,":");
45. ou extxy(280,100,":");
46. ou extxy(x,y,str);
47.
48. rectangle(90,90,380,200);
49. rectangle(70,70,400,220);
13
50.
51. ou extxy(90,250,"Digital Clock");
52.}
14
Program 6
Write a program to implement bouncing ball in ver cal direc on.
Source code:-
1. #include <stdio.h>
2. #include <conio.h>
3. #include <graphics.h>
4. #include <dos.h>
5. int main()
6. {
7. int gd = DETECT, gm;
8. int i, x, y, flag=0;
9. initgraph(&gd, &gm, "C:\\TC\\BGI");
10. x = getmaxx()/2;
11. y = 30;
12. while (!kbhit())
13. {
14. if(y >= getmaxy()-30 || y <= 30)
15. flag = !flag;
16. /* draws the gray board */
17. setcolor(RED);
18. se illstyle(SOLID_FILL, RED);
19. circle(x, y, 30);
20. floodfill(x, y, RED);
21. delay(50);
22. cleardevice();
15
23. if(flag)
24. {
25. y = y + 5; }
26. else
27. {
28. y = y - 5;
29. }
30. }
31. getch();
32. closegraph();
33. return 0;
34.}
16
Program 7
Write a program of transla on,rota on and scaling using composite
transforma on.
Source code:-
1. #include<stdio.h>
2. #include<conio.h>
3. #include<stdlib.h>
4. #include<graphics.h>
5. #include<math.h>
6. int x1,y1,x2,y2,x3,y3,a,b;
7. void draw();
8. void rotate();
9. int main(void)
10.{
[Link] gd=DETECT,gm;
[Link](&gd,&gm,"C:\\TC\\BGI");
[Link] ("Enter first co-ordinate value for triangle:");
[Link]("%d%d",&x1,&y1);
[Link] ("Enter second co-ordinatevalues for triangle:");
[Link]("%d%d",&x2,&y2);
[Link] ("Enter third co-ordinate valuesfor triangle:");
[Link]("%d%d",&x3,&y3);
[Link]();
[Link]();
[Link]();
17
[Link]();
23.
[Link] 0;
25.}
26.
[Link] draw()
28.{
29. line(x1,y1,x2,y2);
30. line(x2,y2,x3,y3);
31. line(x3,y3,x1,y1);
32.}
33. void rotate()
34. {
35. int a1,a2,a3,b1,b2,b3;
36. float angle;
37. prin ("Enter the rota on angle co-ordinates:");
38. scanf("%f",&angle);
39. cleardevice();
40. angle=(angle*3.14)/180;
41. a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle);
42. b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle);
43. a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle);
44. b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle);
45. a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle);
46. b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle);
47. prin ("ROTATION");
18
48. prin ("\n Changed coordinates\n");
49. prin ("%d %d\n%d %d\n%d %d",a1,b1,a2,b2,a3,b3);
50. line(a1,b1,a2,b2);
51. line(a2,b2,a3,b3);
52. line(a3,b3,a1,b1);
53. }
19
Program 8
Write a program to draw a Bezier curve.
Source code:-
1. #include <stdio.h>
2. #include<conio.h>
3. #include <stdlib.h>
4. #include <graphics.h>
5. #include <math.h>
6.
7. void bezier (int x[4], int y[4])
8. {
9. int gd = DETECT, gm;
10. int i;
11. double t;
12. initgraph (&gd, &gm, "C:\\tc\\bgi");
13.
14. for (t = 0.0; t < 1.0; t += 0.0005)
15. {
16. double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] +
17. 3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];
18.
19. double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] +
20. 3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];
21.
22. putpixel (xt, yt, WHITE);
23. }
20
24. for (i=0; i<4; i++)
25. putpixel (x[i], y[i], YELLOW);
26.
27. getch();
28. closegraph();
29. return;
30.}
31.
[Link] main()
33.{
34. int x[4], y[4];
35. int i;
36. prin ("Enter the x- and y-coordinates of the four control points.\n");
37. for (i=0; i<4; i++)
38. scanf ("%d%d", &x[i], &y[i]);
39. bezier (x, y);
40. }
21
22
Program 9
Program to rotate a rectangle about its midpoint.
Source code:-
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define PI 3.14
#define MAX_POINTS 10
typedef struct {
float x[MAX_POINTS], y[MAX_POINTS], theta, h1, k1, r[3][3], ang;
float p[3][MAX_POINTS], p1[3][MAX_POINTS], x1[MAX_POINTS],
y1[MAX_POINTS];
int n;
} Arc;
void getCoordinates(Arc* a) {
prin ("\nENTER ANGLE OF ROTATION: ");
scanf("%f", &a->ang);
a->n = 4;
prin ("\nENTER\n");
23
for (int i = 0; i < a->n; i++) {
prin ("\nx[%d] and y[%d]: ", i, i);
scanf("%f %f", &a->x[i], &a->y[i]);
}
a->h1 = a->x[0] + ((a->x[1] - a->x[0]) / 2);
a->k1 = a->y[0] + ((a->y[3] - a->y[0]) / 2);
prin ("\nMIDPOINT OF RECTANGLE IS-- %f\t%f", a->h1, a->k1);
a->theta = (a->ang * PI) / 180;
a->r[0][0] = cos(a->theta);
a->r[0][1] = -sin(a->theta);
a->r[0][2] = -a->h1 * cos(a->theta) + a->k1 * sin(a->theta) + a->h1;
a->r[1][0] = sin(a->theta);
a->r[1][1] = cos(a->theta);
a->r[1][2] = -a->h1 * sin(a->theta) - a->k1 * cos(a->theta) + a->k1;
a->r[2][0] = 0;
a->r[2][1] = 0;
a->r[2][2] = 1;
}
void calculate(Arc* a) {
for (int i = 0; i < a->n; i++) {
a->p[0][i] = a->x[i];
24
a->p[1][i] = a->y[i];
a->p[2][i] = 1;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < a->n; j++) {
a->p1[i][j] = 0;
for (int k = 0; k < 3; k++) {
a->p1[i][j] += a->r[i][k] * a->p[k][j];
}
}
}
for (int i = 0; i < a->n; i++) {
a->x1[i] = a->p1[0][i];
a->y1[i] = a->p1[1][i];
}
}
void mapGraphics() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int errorcode = graphresult();
if (errorcode != grOK) {
prin ("Graphics error: %s \n", grapherrormsg(errorcode));
prin ("Press any key to halt: ");
25
getch();
exit(1);
}
}
void drawGraph(Arc* a) {
int xm = getmaxx() / 2;
int ym = getmaxy() / 2;
line(xm, 0, xm, 2 * ym);
line(0, ym, 2 * xm, ym);
}
void plotArc(Arc* a) {
int xm = getmaxx() / 2;
int ym = getmaxy() / 2;
for (int i = 0; i < a->n - 1; i++) {
circle(a->x1[i] + xm, (-a->y1[i] + ym), 2);
line(a->x1[i] + xm, (-a->y1[i] + ym), a->x1[i + 1] + xm, (-a->y1[i + 1] + ym));
}
line(a->x1[a->n - 1] + xm, (-a->y1[a->n - 1] + ym), a->x1[0] + xm, (-a->y1[0] +
ym));
getch();
}
void plotOriginal(Arc* a) {
int xm = getmaxx() / 2;
26
int ym = getmaxy() / 2;
for (int i = 0; i < a->n - 1; i++) {
circle(a->x[i] + xm, (-a->y[i] + ym), 2);
line(a->x[i] + xm, (-a->y[i] + ym), a->x[i + 1] + xm, (-a->y[i + 1] + ym));
}
circle(a->x[a->n - 1] + xm, (-a->y[a->n - 1] + ym), 2);
line(a->x[a->n - 1] + xm, (-a->y[a->n - 1] + ym), a->x[0] + xm, (-a->y[0] + ym));
getch();
}
int main() {
Arc a;
clrscr();
mapGraphics();
drawGraph(&a);
getCoordinates(&a);
calculate(&a);
plotOriginal(&a);
plotArc(&a);
getch();
return 0;
27
}
28
Program 10
Program to clip a line using Liang Barsky method.
Source code:-
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>
typedef struct {
float x1, x2, y1, y2, u1, u2, dx, dy, xm, ym;
float xmin, ymin, xmax, ymax, p[4], q[4], r[4];
int gd, gm, k;
} Liang;
void map(Liang* l) {
l->gd = DETECT;
initgraph(&l->gd, &l->gm, "");
int errorcode = graphresult();
if (errorcode != grOK) {
prin ("Graphics error: %s \n", grapherrormsg(errorcode));
prin ("Press any key to halt: ");
getch();
exit(1);
}
}
void graph(Liang* l) {
29
l->xm = getmaxx() / 2;
l->ym = getmaxy() / 2;
line(l->xm, 0, l->xm, 2 * l->ym);
line(0, l->ym, 2 * l->xm, l->ym);
}
void getCoordinates(Liang* l) {
prin ("\nENTER WINDOW COORDINATES xwmin, ywmin, xwmax, ywmax: ");
scanf("%f %f %f %f", &l->xmin, &l->ymin, &l->xmax, &l->ymax);
rectangle(l->xmin + l->xm, -l->ymin + l->ym, l->xmax + l->xm, -l->ymax + l-
>ym);
prin ("\nENTER END POINTS OF LINE (x1, y1)(x2, y2): ");
scanf("%f %f %f %f", &l->x1, &l->y1, &l->x2, &l->y2);
line(l->x1 + l->xm, -l->y1 + l->ym, l->x2 + l->xm, -l->y2 + l->ym);
getch();
}
void clipLiang(Liang* l) {
float x = 0, y = 1;
l->dx = l->x2 - l->x1;
l->dy = l->y2 - l->y1;
l>p[0] = -l->dx;
l->p[1] = l->dx;
l->p[2] = -l->dy;
l->p[3] = l->dy;
l->q[0] = l->x1 - l->xmin;
l->q[1] = l->xmax - l->x1;
l->q[2] = l->y1 - l->ymin;
l->q[3] = l->ymax - l->y1;
30
for (l->k = 0; l->k < 4; l->k++) {
if (l->p[l->k] == 0 && l->q[l->k] < 0) {
prin ("\nOUTSIDE");
getch();
exit(0);
}
if (l->p[l->k] < 0) {
l->r[l->k] = l->q[l->k] / l->p[l->k];
if (l->r[l->k] < y) {
y = l->r[l->k];
}
}
}
l->u1 = y; // Assume u1 is ini alized properly
l->u2 = y; // Assume u2 is ini alized properly
if (l->u1 > l->u2) {
prin ("\nCOMPLETE OUTSIDE WINDOW");
getch();
exit(0);
}
}
int main() {
Liang l;
clrscr();
map(&l);
graph(&l);
31
getCoordinates(&l);
clipLiang(&l);
getch();
return 0;
}