CGA Practical File 2024
CGA Practical File 2024
Objective(s):
The objective of the program is to demonstrate basic graphics construction in C using the
graphics.h library. The program draws various shapes, including a line, rectangle, arc, circle,
and ellipse, on the screen.
❖ Program to draw basic graphics construction like line, circle, arc, ellipse
and rectangle.
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
initgraph (&gd,&gm,"c:\\tc\\bgi");
setbkcolor(GREEN);
printf("\t\t\t\n\nLINE");
line(50,40,190,40);
printf("\t\t\n\n\n\nRECTANGE;
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC";
arc(120,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30);
printf("\t\n\n\n\nECLIPSE");
ellipse(120,350,0,360,30,20);
getch();
}
Output:
LAB EXERCISE #2
Objective(s):
The objective of the program seems to be drawing a simple house-like structure using
rectangles, lines, and flood fill to color different parts of the structure.
#include<graphics.h>
int main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
closegraph();
return 0;
}
Output:
LAB EXERCISE #3
Objective(s):
The objective of this program seems to be to create a graphical pattern using circles with
different fill styles and colours.
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT, gm, i, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x=getmaxx()/3;
y=getmaxx()/3;
setbkcolor(WHITE);
setcolor(BLUE);
for(i=1;i<=8;i++)
{
setfillstyle(i,i);
delay(20);
circle(x, y, i*20);
floodfill(x-2+i*20,y,BLUE);
}
getch();
closegraph();
}
Output:
LAB EXERCISE #4
Objective(s):
This program aims to draw a simple pie chart representing different expenses or categories.
#include<graphics.h>
int main()
{
int gd = DETECT, gm, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(220,10,"PIE CHART");
x = getmaxx()/2;
y = getmaxy()/2;
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);
setfillstyle(SOLID_FILL, RED);
pieslice(x, y, 0, 60, 120);
outtextxy(x + 140, y - 70, "FOOD");
setfillstyle(SOLID_FILL, YELLOW);
pieslice(x, y, 60, 160, 120);
outtextxy(x - 30, y - 170, "RENT");
setfillstyle(SOLID_FILL, GREEN);
pieslice(x, y, 160, 220, 120);
outtextxy(x - 250, y, "ELECTRICITY");
setfillstyle(SOLID_FILL, BROWN);
pieslice(x, y, 220, 360, 120);
outtextxy(x, y + 150, "SAVINGS");
closegraph();
return 0;
}
Output:
LAB EXERCISE #5
Objective(s):
The objective of this program appears to be to draw concentric circles of different colours at
the centre of the screen using graphics in C.
#include<stdio.h>
#include<graphics.h>
int main()
{
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(240, 50, "Concentric Circles");
setcolor(RED);
circle(x, y, 30);
setcolor(GREEN);
circle(x, y, 50);
setcolor(YELLOW);
circle(x, y, 70);
setcolor(BLUE);
circle(x, y, 90);
closegraph();
return 0;
}
Output:
LAB EXERCISE #6
Objective(s):
This program is to create a visually appealing 3D bar graph using the graphics library in C. The
graph is meant to represent a dataset or some statistical information in a graphical format.
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0,"3D BAR GRAPH");
setlinestyle(SOLID_LINE,0,2);
line(90,410,90,50);
line(90,410,590,410);
line(85,60,90,50);
line(95,60,90,50);
line(585,405,590,410);
line(585,415,590,410);
outtextxy(65,60,"Y");
outtextxy(570,420,"X");
outtextxy(70,415,"O");
setfillstyle(XHATCH_FILL, RED);
bar3d(150,80,200,410, 15, 1);
bar3d(225,100,275,410, 15, 1);
bar3d(300,120,350,410, 15, 1);
bar3d(375,170,425,410, 15, 1);
bar3d(450,135,500,410, 15, 1);
closegraph();
return 0;
}
Output:
LAB EXERCISE #7
Objective(s):
The objective of the provided C code is to create a simple animation demonstrating a sine wave
using the graphics capabilities of the Turbo C compiler.
#include <math.h>
#include <graphics.h>
#include <dos.h>
int main()
{
int gd = DETECT, gm;
int angle = 0;
double x, y;
putpixel(x, y, 15);
delay(100);
angle+=5;
}
closegraph();
return 0;
}
Output:
LAB EXERCISE #8
Objective(s):
The objective of the provided C code is to create a simple animation that generates random
pixels on the screen and clears the screen repeatedly until a keyboard key is pressed.
❖ C Program to Draw Stars in Night Sky Using C Graphics
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>
int main()
{
int gd = DETECT, gm;
int i, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
while (!kbhit()) {
for(i=0; i<=500; i++) {
x=rand()%getmaxx();
y=rand()%getmaxy();
putpixel(x,y,15);
}
delay(500);
cleardevice();
}
closegraph();
return 0;
}
Output:
LAB EXERCISE #9
Objective(s):
The objective of the provided C program is to create a simple graphical animation that displays
a counter incrementing from 0 up to a specified number entered by the user.
#include <stdio.h>
#include <graphics.h>
#include <string.h>
#include <dos.h>
int main()
{
int gd = DETECT, gm;
int i, midx, midy, count;
char string[100];
printf("Enter a Number\n");
scanf("%d", &count);
initgraph(&gd, &gm, "X:\\TC\\BGI");
midx = getmaxx()/2;
midy = getmaxy()/2;
setcolor(WHITE);
setfillstyle(SOLID_FILL, WHITE);
rectangle(midx - 50, midy - 50, midx + 50, midy + 50);
floodfill(midx, midy, WHITE);
setcolor(BLUE);
sprintf(string, "%s", "Counter");
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 5);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy - 100, "Counter");
closegraph();
return 0;
}
Output:
Objective(s):
The objective of the provided C program is to create a simple graphical animation that
continuously displays the current system time in a graphical window.
#include <graphics.h>
#include <time.h>
#include <dos.h>
#include <string.h>
int main()
{
int gd = DETECT, gm;
int midx, midy;
long current_time;
char timeStr[256];
midx = getmaxx() / 2;
midy = getmaxy() / 2;
while (!kbhit()) {
cleardevice();
setcolor(WHITE);
setfillstyle(SOLID_FILL, WHITE);
rectangle(midx - 250, midy - 40, midx + 250, midy + 40);
floodfill(midx, midy, WHITE);
current_time = time(NULL);
strcpy(timeStr, ctime(¤t_time));
setcolor(RED);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 4);
moveto(midx, midy);
outtext(timeStr);
delay(1000);
}
closegraph();
return 0;
}
Output:
LAB EXERCISE #11
Objective(s):
The objective of the provided C program is to create a simple graphical animation that displays
a bouncing red ball within a graphical window.
#include <stdio.h>
#include <graphics.h>
#include <dos.h>
int main()
{
int gd = DETECT, gm;
int i, x, y, flag=0;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x = getmaxx()/2;
y = 30;
while (!kbhit())
{
if(y >= getmaxy()-30 || y <= 30)
flag = !flag;
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
circle(x, y, 30);
floodfill(x, y, RED);
delay(50);
cleardevice();
if(flag){
y = y + 5;
} else
{
y = y - 5;
}
}
closegraph();
return 0;
}
Output:
Objective(s):
The objective of the provided C program is to create a simple animation of a moving car using
basic graphics functions in Turbo C.
#include <stdio.h>
#include <graphics.h>
#include <dos.h>
int main()
{
int gd = DETECT, gm;
int i, maxx, midy;
initgraph(&gd, &gm, "X:\\TC\\BGI");
maxx = getmaxx();
midy = getmaxy()/2;
closegraph();
return 0;
}
Output:
LAB EXERCISE #13
Objective(s):
The objective of the provided C program is to demonstrate the basic usage of graphics functions
in Turbo C by drawing a rectangle and a bar on the screen.
#include<stdio.h>
#include<graphics.h>
int main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
closegraph();
return 0;
}
Output:
LAB EXERCISE #14
Objective(s):
The objective of the provided C program is to showcase basic graphical drawing capabilities
using the Turbo C graphics library.
❖ Program to make screen saver in that display different size circles filled
with different colours and at random places.
#include<stdio.h>
#include<conio.h>
#include"graphics.h"
#include"stdlib.h"
void main()
{
intgd=DETECT,gm,i=0,x,xx,y,yy,r;
//Initializes the graphics system
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=getmaxx();
y=getmaxy();
{ i++;
//setfillstyle(random(i),random(30));
circle(xx=random(x),yy=random(y),random(30));
setfillstyle(random(i),random(30));
floodfill(xx,yy,getmaxcolor());
delay(200);
}
getch();
}
Output:
LAB EXERCISE #15
Objective(s):
The objective of the provided C program is to create a simple animation of a bouncing ball
within a graphical window using the Turbo C graphics library.
#include<stdio.h>
#include<graphics.h>
#define HEIGHT getmaxy()
#define WIDTH getmaxx()
#define GROUND 450
#define MAXHEIGHT 420
void main()
{
int x,y=0,t=MAXHEIGHT,c=1;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\T urboC3\\BGI");
for(x=40;x<=getmaxx();x=x+2)
{
rectangle (0,MAXHEIGHT,getmaxx(),MAXHEIGHT+5);
floodfill (5,MAXHEIGHT+3,WHITE);
//Draw Ball
pieslice(x,y,0,360,20);
//floodfill(x,y,RED);
delay(100);
if(y>MAXHEIGHT-20)
{
c=0; t=t-40;
}
if(y<=(MAXHEIGHT-t))
{ c=1;
}
if(t>=40)
y=y+(c? 15:-15);
cleardevice();
//Exit upon keypress
if(kbhit())
break;
}
getch();
}
Output:
Objective(s):
This C program seems to be aimed at drawing a triangle using the graphics.h library and then
rotating it based on user input.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,a,b;
void draw();
void rotate();
int main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter first co-ordinate value for triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter second co-ordinatevalues for triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter third co-ordinate valuesfor triangle:");
scanf("%d%d",&x3,&y3);
draw(); getch(); rotate();
getch();
return 0;
}
void draw()
{
line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1);
}
void rotate()
{
int a1,a2,a3,b1,b2,b3;
float angle;
printf("Enter the rotation angle co-ordinates:");
scanf("%f",&angle);
cleardevice();
angle=(angle*3.14)/180;
a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle);
b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle);
a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle);
b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle);
a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle);
b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle);
printf("ROTATION");
printf("\n Changed coordinates\n");
printf("%d %d\n%d %d\n%d %d",a1,b1,a2,b2,a3,b3);
zline(a1,b1,a2,b2); line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}
Output: