Study of Basic Graphics Functions Defined In
Study of Basic Graphics Functions Defined In
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
#include <conio.h>
int main() {
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() {
getch();
closegraph();
return 0;
}
4. Write a Program to make a moving car.
Ans: #include <graphics.h>
#include <conio.h>
#include <dos.h>
int main() {
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");
getch();
closegraph();
return 0;
}
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();
}
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;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
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");
getch();
closegraph();
return 0;
}
void displayTime() {
// Get current time
time_t rawtime;
struct tm *timeinfo;
char buffer[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
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;
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");
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) {
int main() {
setcolor(RED);
getch();
closegraph();
return 0;
Before Rotation
After Rotation
14. Write a program to perform Translation.
#include <conio.h>
x += tx;
y += ty;
int main() {
setcolor(RED);
getch();
closegraph();
return 0;
}
15. Write a program to perform Scaling.
Ans: #include <graphics.h>
#include <conio.h>
x *= sx;
y *= sy;
int main() {
int sx = 2, sy = 2;
getch();
closegraph();
return 0;
}
16. Write a program to implement polygon filling using
boundary fill algorithm.
Ans: #include <graphics.h>
#include <conio.h>
putpixel(x, y, fill_color);
int main() {
int poly[] = {150, 150, 200, 100, 250, 150, 200, 200, 150, 150};
drawpoly(5, poly);
boundaryFill(200, 150, RED, WHITE);
getch();
closegraph();
return 0;
#include <conio.h>
if (getpixel(x, y) == old_color) {
putpixel(x, y, fill_color);
int main() {
int poly[] = {150, 150, 200, 100, 250, 150, 200, 200, 150, 150};
drawpoly(5, poly);
getch();
closegraph();
return 0;
#include <conio.h>
else x = -x;
void reflectLine(int &x1, int &y1, int &x2, int &y2, int axis) {
int main() {
setcolor(RED);
closegraph();
return 0;
#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() {
setcolor(RED);
getch();
closegraph();
return 0;
}
20. Write a program to implement transformations.
Ans: #include <graphics.h>
#include <conio.h>
x += tx;
y += ty;
void rotatePoint(int x, int y, int xc, int yc, float angle, int &xr, int &yr) {
x *= sx;
y *= sy;
int main() {
int gd = DETECT, gm;
int sx = 2, sy = 2;
// Original line
// Translation
setcolor(RED);
// Rotation
setcolor(GREEN);
// Scaling
setcolor(BLUE);
line(x1, y1, x2, y2);
getch();
closegraph();
return 0;