0% found this document useful (0 votes)
19 views32 pages

Aashish CG

Uploaded by

zssdzwqpd9
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)
19 views32 pages

Aashish CG

Uploaded by

zssdzwqpd9
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/ 32

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 Application 1st Semester


Session 2024-2026

Computer Graphics & Multimedia


(20MCA21CL1 based on paper 20MCA21C3)

Submitted To: Submitted by: Aashish


Mr. Aadi Student I’d no: 146070
(Assistant Professor)
1

Certificate

This is to certify that Aashish S/o Anil Kumar has submitted a practical file for
fulfilment of MCA 1st semester lab course for Computer Graphics & Multimedia.

Submitted To: Submitted By: Aashish

Mr. Aadi Student I'd no: 146070

(Assistant Professor)
3

SR Programs Remarks
No
1 Write a Program to draw basic graphics construction like line,
circle, arc, ellipse, and rectangle.
2 Write a program to draw animation using increasing circles
filled with different colors and patterns.
3 Program to make screen saver that display different size
circles filled with different colors and at random places.
4 Write a function to make a moving car using inbuilt
functions.
5 Write a program to implement digital clock.

6 Write a program to implement bouncing ball in vertical


direction.
7 Write a program of translation, rotation and scaling using
composite transformations.
8 Write a program to draw Bezier curve.

9 Program to rotate a rectangle about its midpoint.

10 Program to clip a line using Liang Barsky method.


6

Program 1
Write a program to draw basic graphics construction 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. printf("\t\t\t\n\nLINE");
11. line(50,40,190,40);
12. printf("\t\t\n\n\n\nRECTANGLE");
13. rectangle(125,115,215,165);
14. printf("\t\t\t\n\n\n\n\n\n\nARC");
15. arc(120,200,180,0,30);
16. printf("\t\n\n\n\nCIRCLE");
17. circle(120,270,30);
18. printf("\t\n\n\n\nECLIPSE");
19. ellipse(120,350,0,360,30,20);
20. getch();
21.}
7
8

Program 2
Write a program to draw animation using increasing circles filled with
different colors and patterns.
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. setfillstyle(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.}
9
1
0

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. //Initializes the graphics system
9. initgraph(&gd,&gm,"c:\\tc\\bgi");
10. x=getmaxx();
11. y=getmaxy();
12. while(!kbhit())
13. {
14. i++;
15. // setfillstyle(random(i),random(30));
16. circle(xx=random(x),yy=random(y),random(30));
17. setfillstyle(random(i),random(30));
18. floodfill(xx,yy,getmaxcolor());
19. delay(200);
20. }
21. getch();
22.}
1
1
1
2

Program 4
Write a program to make a moving-coloured car using inbuilt functions.
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. setfillstyle(1,BLUE);
30. floodfill(5+i, cy-15, RED);
31. setfillstyle(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 time 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. settextstyle(4,0,7);
16.
17. while(!kbhit())
18. {
19. gettime(&t);
20. hr=t.ti_hour;
21. min=t.ti_min;
22. sec=t.ti_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.}
36.void display(int x,int y,int num)
37.{
38.
39. char str[3];
40. itoa(num,str,10);
41.
42. settextstyle(4,0,7);
43.
44. outtextxy(180,100,":");
45. outtextxy(280,100,":");
46. outtextxy(x,y,str);
47.
48. rectangle(90,90,380,200);
49. rectangle(70,70,400,220);
13

50.
51. outtextxy(90,250,"Digital Clock");
52.}
14

Program 6
Write a program to implement bouncing ball in vertical direction.
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. setfillstyle(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 translation,rotation and scaling using composite
transformation.
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. {
11. int gd=DETECT,gm;
12.initgraph(&gd,&gm,"C:\\TC\\BGI");
13.printf("Enter first co-ordinate value for triangle:");
14.scanf("%d%d",&x1,&y1);
15.printf("Enter second co-ordinatevalues for triangle:");
16.scanf("%d%d",&x2,&y2);
17.printf("Enter third co-ordinate valuesfor triangle:");
18.scanf("%d%d",&x3,&y3);
19. draw();

20. getch();

21. rotate();
17

22. getch();

23.
24. return 0;

25. }

26.

27. void 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. printf("Enter the rotation 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. printf("ROTATION");
18

48. printf("\n Changed coordinates\n");


49. printf("%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.
32. void main()
33.{
34. int x[4], y[4];
35. int i;
36. printf ("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. }
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) {
printf("\nENTER ANGLE OF ROTATION: ");
scanf("%f", &a->ang);

a->n = 4;
printf("\nENTER\n");
for (int i = 0; i < a->n; i++) {
printf("\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);
printf("\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];
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) {
printf("Graphics error: %s \n", grapherrormsg(errorcode));
printf("Press any key to halt: ");
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;
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;
}
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) {
printf("Graphics error: %s \n", grapherrormsg(errorcode));
printf("Press any key to halt: ");
getch();
exit(1);
}
}
void graph(Liang* l) {
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) {
printf("\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);
printf("\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;
for (l->k = 0; l->k < 4; l->k++) {
if (l->p[l->k] == 0 && l->q[l->k] < 0) {
printf("\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 initialized properly
l->u2 = y; // Assume u2 is initialized properly
if (l->u1 > l->u2) {
printf("\nCOMPLETE OUTSIDE WINDOW");
getch();
exit(0);
}
}
int main() {
Liang l;
clrscr();
map(&l);
graph(&l);
31

getCoordinates(&l);
clipLiang(&l);
getch();
return 0;
}

You might also like