1. Study of basic graphics functions defined in “graphics.h “.
Ans: #include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
line(100, 100, 300, 100);
rectangle(100, 150, 300, 250);
circle(400, 200, 50);
ellipse(550, 200, 0, 360, 100, 50);
arc(200, 400, 0, 180, 50);
outtextxy(100, 300, "Basic Graphics Functions Demo");
delay(5000); // Delay for 5 seconds
closegraph();
return 0;
}
2. Write a program to draw a Hut or other geometrical figures.
Ans: #include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Drawing the base of the hut
rectangle(100, 200, 300, 400);
rectangle(160, 300, 240, 400);
// Drawing the roof of the hut
line(100, 200, 200, 100);
line(200, 100, 300, 200);
getch();
closegraph();
return 0;
}
3. Write a Program to draw basic graphics construction like line, circle, arc, ellipse and
rectangle
Ans: #include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
line(100, 100, 200, 100);
circle(300, 100, 50);
arc(400, 100, 0, 180, 50);
ellipse(500, 100, 0, 360, 50, 25);
rectangle(600, 50, 700, 150);
getch();
closegraph();
return 0;
}
4. Write a Program to make a moving car.
Ans: #include <graphics.h>
#include <conio.h>
#include <dos.h>
void drawCar(int x, int y) {
rectangle(x, y, x + 100, y + 50);
circle(x + 20, y + 60, 10);
circle(x + 80, y + 60, 10);
int main() {
int gd = DETECT, gm, x = 50, y = 200;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
while (!kbhit()) {
cleardevice();
drawCar(x, y);
delay(100);
x += 10;
if (x > getmaxx()) x = 0;
getch();
closegraph();
return 0;
}
5. Write a Program to make a kite.
Ans: #include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
line(200, 100, 300, 200);
line(300, 200, 200, 300);
line(200, 300, 100, 200);
line(100, 200, 200, 100);
line(200, 100, 200, 300);
line(100, 200, 300, 200);
getch();
closegraph();
return 0;
}
6. Write a Program to implement Digital Clock
Ans: #include <graphics.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
void drawClock(int h, int m, int s) {
char timeStr[9];
sprintf(timeStr, "%02d:%02d:%02d", h, m, s);
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 4);
outtextxy(200, 200, timeStr);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
while (!kbhit()) {
time_t now = time(NULL);
struct tm *t = localtime(&now);
cleardevice();
drawClock(t->tm_hour, t->tm_min, t->tm_sec);
delay(1000);
}
getch();
closegraph();
return 0;
}
7. Write a Program to draw the fish.
Ans: #include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
ellipse(200,200,0,360,130,50);
setfillstyle(LINE_FILL,CYAN);
floodfill(201,201,15);
line(325,185,390,155);
line(390,155,360,200);
line(360,235,325,215);
line(395,235,325,215);
setfillstyle(LINE_FILL,LIGHTRED);
floodfill(330,190,15);
ellipse(100,200,315,45,50,60);
circle(120,180,3);
setfillstyle(SOLID_FILL,YELLOW):
floodfill(120,180,3);
line(170,150,260,90);
line(260,90,220,150);
setfillstyle(LINE_FILL,LIGHTRED);
floodfill(200,140,15);
line(170,250,260,290);
line(260,290,230,250);
setfillstyle(LINE_FILL,LIGHTRED);
floodfill(200,255,15);
arc(220,185,270,90,6);
arc(200,185,270,90,6);
arc(180,185,270,90,6);
arc(200,215,270,90,6);
arc(220,215,270,90,6);
arc(180,215,270,90,6);
arc(240,200,270,90,6);
getch();
closegraph();
}
8. Write a program to draw a line using DDA Algo.
Ans: #include <graphics.h>
#include <conio.h>
#include <math.h>
void drawLineDDA(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float Xinc = dx / (float) steps;
float Yinc = dy / (float) steps;
float x = x1;
float y = y1;
for (int i = 0; i <= steps; i++) {
putpixel(x, y, WHITE);
x += Xinc;
y += Yinc;
delay(10);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
drawLineDDA(100, 100, 200, 200);
getch();
closegraph();
return 0;
}
9. Write a program to draw a line using Bresenhem’s Algo.
Ans: #include <graphics.h>
#include <conio.h>
void drawLineBresenham(int x1, int y1, int x2, int y2) {
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int p = 2 * dy - dx;
int twoDy = 2 * dy;
int twoDyDx = 2 * (dy - dx);
int x, y, xEnd;
if (x1 > x2) {
x = x2;
y = y2;
xEnd = x1;
} else {
x = x1;
y = y1;
xEnd = x2;
}
putpixel(x, y, WHITE);
while (x < xEnd) {
x++;
if (p < 0) {
p += twoDy;
} else {
y++;
p += twoDyDx;
}
putpixel(x, y, WHITE);
delay(10);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
drawLineBresenham(100, 100, 200, 200);
getch();
closegraph();
return 0;
}
10. Write a program to draw a circle using mid-point algorithm.
Ans: #include <graphics.h>
#include <conio.h>
void drawCircleMidPoint(int xc, int yc, int r) {
int x = 0;
int y = r;
int p = 1 - r;
while (x <= y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
if (p < 0) {
p += 2 * x + 1;
} else {
p += 2 * (x - y) + 1;
y--;
}
x++;
delay(10);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
drawCircleMidPoint(200, 200, 100);
getch();
closegraph();
return 0;
}
11. Write a Program to implement Digital Clock.
Ans: #include <graphics.h>
#include <ctime>
#include <cstdio>
void displayTime() {
// Get current time
time_t rawtime;
struct tm *timeinfo;
char buffer[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, sizeof(buffer), "%I:%M:%S %p", timeinfo);
// Clear previous display
setcolor(BLACK);
outtextxy(100, 100, buffer);
// Display current time
setcolor(WHITE);
outtextxy(100, 100, buffer);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
while (1) {
displayTime();
delay(1000); // Delay for 1 second (1000 milliseconds)
}
getch();
closegraph();
return 0;
}
12. Write a program to perform line clipping.
Ans: #include <graphics.h>
#include <conio.h>
void lineClipping(int x1, int y1, int x2, int y2, int xmin, int ymin, int xmax,
int ymax) {
// Cohen-Sutherland algorithm for line clipping
int code1, code2, codeOut;
bool accept = false;
bool done = false;
auto computeCode = [&](int x, int y) {
int code = 0;
if (x < xmin) code |= 1;
else if (x > xmax) code |= 2;
if (y < ymin) code |= 4;
else if (y > ymax) code |= 8;
return code;
};
code1 = computeCode(x1, y1);
code2 = computeCode(x2, y2);
do {
if (!(code1 | code2)) {
accept = true;
done = true;
} else if (code1 & code2) {
done = true;
} else {
int x, y;
codeOut = code1 ? code1 : code2;
if (codeOut & 8) {
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
} else if (codeOut & 4) {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
} else if (codeOut & 2) {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
} else {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (codeOut == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
} while (!done);
if (accept) {
setcolor(WHITE);
rectangle(xmin, ymin, xmax, ymax);
setcolor(GREEN);
line(x1, y1, x2, y2);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
lineClipping(50, 50, 400, 400, 100, 100, 300, 300);
getch();
closegraph();
return 0;
}
13. .
Ans: #include <graphics.h>
#include <conio.h>
#include <math.h>
void rotatePoint(int x, int y, int xc, int yc, float angle, int &xr, int &yr) {
float radian = angle * M_PI / 180;
xr = xc + (x - xc) * cos(radian) - (y - yc) * sin(radian);
yr = yc + (x - xc) * sin(radian) + (y - yc) * cos(radian);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int x1 = 100, y1 = 100, x2 = 200, y2 = 100;
int xc = 150, yc = 150;
float angle = 45;
int xr1, yr1, xr2, yr2;
rotatePoint(x1, y1, xc, yc, angle, xr1, yr1);
rotatePoint(x2, y2, xc, yc, angle, xr2, yr2);
line(x1, y1, x2, y2);
setcolor(RED);
line(xr1, yr1, xr2, yr2);
getch();
closegraph();
return 0;
Before Rotation
After Rotation
14. Write a program to perform Translation.
Ans: #include <graphics.h>
#include <conio.h>
void translate(int &x, int &y, int tx, int ty) {
x += tx;
y += ty;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int x1 = 100, y1 = 100, x2 = 200, y2 = 100;
int tx = 50, ty = 50;
line(x1, y1, x2, y2);
translate(x1, y1, tx, ty);
translate(x2, y2, tx, ty);
setcolor(RED);
line(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}
15. Write a program to perform Scaling.
Ans: #include <graphics.h>
#include <conio.h>
void scale(int &x, int &y, int sx, int sy) {
x *= sx;
y *= sy;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int x1 = 100, y1 = 100, x2 = 200, y2 = 100;
int sx = 2, sy = 2;
line(x1, y1, x2, y2);
scale(x1, y1, sx, sy);
scale(x2, y2, sx, sy);
setcolor(RED);
line(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}
16. Write a program to implement polygon filling using
boundary fill algorithm.
Ans: #include <graphics.h>
#include <conio.h>
void boundaryFill(int x, int y, int fill_color, int boundary_color) {
if (getpixel(x, y) != boundary_color && getpixel(x, y) != fill_color) {
putpixel(x, y, fill_color);
boundaryFill(x + 1, y, fill_color, boundary_color);
boundaryFill(x - 1, y, fill_color, boundary_color);
boundaryFill(x, y + 1, fill_color, boundary_color);
boundaryFill(x, y - 1, fill_color, boundary_color);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int poly[] = {150, 150, 200, 100, 250, 150, 200, 200, 150, 150};
drawpoly(5, poly);
boundaryFill(200, 150, RED, WHITE);
getch();
closegraph();
return 0;
17. Write a program to implement polygon filling using flood fill
algorithm.
Ans: #include <graphics.h>
#include <conio.h>
void floodFill(int x, int y, int fill_color, int old_color) {
if (getpixel(x, y) == old_color) {
putpixel(x, y, fill_color);
floodFill(x + 1, y, fill_color, old_color);
floodFill(x - 1, y, fill_color, old_color);
floodFill(x, y + 1, fill_color, old_color);
floodFill(x, y - 1, fill_color, old_color);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int poly[] = {150, 150, 200, 100, 250, 150, 200, 200, 150, 150};
drawpoly(5, poly);
floodFill(200, 150, RED, BLACK);
getch();
closegraph();
return 0;
18. Write a program to implement reflection of a point, line.
Ans: #include <graphics.h>
#include <conio.h>
void reflectPoint(int &x, int &y, int axis) {
if (axis == 'x') y = -y;
else x = -x;
void reflectLine(int &x1, int &y1, int &x2, int &y2, int axis) {
reflectPoint(x1, y1, axis);
reflectPoint(x2, y2, axis);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int x1 = 100, y1 = 100, x2 = 200, y2 = 200;
char axis = 'x';
line(x1, y1, x2, y2);
reflectLine(x1, y1, x2, y2, axis);
setcolor(RED);
line(x1, y1, x2, y2);
getch();
closegraph();
return 0;
19. Write a program to perform shearing on a line.
Ans: #include <graphics.h>
#include <conio.h>
void shearLine(int &x1, int &y1, int &x2, int &y2, int shx, int shy) {
x1 = x1 + shx * y1;
x2 = x2 + shx * y2;
y1 = y1 + shy * x1;
y2 = y2 + shy * x2;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int x1 = 100, y1 = 100, x2 = 200, y2 = 100;
int shx = 1, shy = 0;
line(x1, y1, x2, y2);
shearLine(x1, y1, x2, y2, shx, shy);
setcolor(RED);
line(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}
20. Write a program to implement transformations.
Ans: #include <graphics.h>
#include <conio.h>
void translate(int &x, int &y, int tx, int ty) {
x += tx;
y += ty;
void rotatePoint(int x, int y, int xc, int yc, float angle, int &xr, int &yr) {
float radian = angle * M_PI / 180;
xr = xc + (x - xc) * cos(radian) - (y - yc) * sin(radian);
yr = yc + (x - xc) * sin(radian) + (y - yc) * cos(radian);
void scale(int &x, int &y, int sx, int sy) {
x *= sx;
y *= sy;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
int x1 = 100, y1 = 100, x2 = 200, y2 = 100;
int tx = 50, ty = 50;
float angle = 45;
int sx = 2, sy = 2;
int xr1, yr1, xr2, yr2;
// Original line
line(x1, y1, x2, y2);
// Translation
translate(x1, y1, tx, ty);
translate(x2, y2, tx, ty);
setcolor(RED);
line(x1, y1, x2, y2);
// Rotation
rotatePoint(x1, y1, tx, ty, angle, xr1, yr1);
rotatePoint(x2, y2, tx, ty, angle, xr2, yr2);
setcolor(GREEN);
line(xr1, yr1, xr2, yr2);
// Scaling
scale(x1, y1, sx, sy);
scale(x2, y2, sx, sy);
setcolor(BLUE);
line(x1, y1, x2, y2);
getch();
closegraph();
return 0;