9.
a) outtext()
Definition:
outtext() displays a string at the current position of the text cursor
on the graphics screen.
Code :
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
outtext("Hello using outtext!");
getch();
closegraph();
return 0;
}
b) outtextxy(int x, int y, char *text)
Definition:
outtextxy() displays a string at the (x, y) coordinates on the
graphics screen.
Code :
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
outtextxy(150, 100, "Hello at (150, 100)");
getch();
closegraph();
return 0;
}
c) settextstyle(int font, int direction, int charsize)
Definition:
settextstyle() sets the font, direction (horizontal/vertical), and size
of the text.
Code :
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
settextstyle(3, HORIZ_DIR, 5); // Font 3, horizontal direction,
size 5
outtextxy(100, 150, "Styled Text");
getch();
closegraph();
return 0;
}
10. a) setbkcolor()
Definition:
setbkcolor() sets the background color of the screen.
Code :
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setbkcolor(BLUE); // Set background to blue
outtextxy(100, 100, (char*)"Background is blue");
getch();
closegraph();
return 0;
b) setcolor()
Definition:
setcolor() sets the color for drawing text or shapes.
Code :
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setbkcolor(WHITE); // Background white
setcolor(RED); // Drawing color red
outtextxy(100, 100, (char*)"Text in red color");
getch();
closegraph();
return 0;
}
c) getbkcolor()
Definition:
getbkcolor() returns the current background color as an integer value.
Code :
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setbkcolor(GREEN);
int bg = getbkcolor();
char msg[50];
sprintf(msg, "Background Color Code: %d", bg);
outtextxy(100, 100, msg);
getch();
closegraph();
return 0;}
d) getcolor()
Definition:
getcolor() returns the current drawing color as an integer value.
💻 Code :
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setcolor(CYAN);
int drawColor = getcolor();
char msg[50];
sprintf(msg, "Drawing Color Code: %d", drawColor);
outtextxy(100, 100, msg);
getch();
closegraph();
return 0;
}
12. #include <graphics.h>
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
int xmin = 100, ymin = 100, xmax = 300, ymax = 300;
int getCode(int x, int y) {
int code = 0;
if (x < xmin) code |= LEFT;
if (x > xmax) code |= RIGHT;
if (y < ymin) code |= BOTTOM;
if (y > ymax) code |= TOP;
return code;
void cohenClip(int x1, int y1, int x2, int y2) {
int code1 = getCode(x1, y1), code2 = getCode(x2, y2);
while (code1 | code2) {
if (code1 & code2) return;
int code = code1 ? code1 : code2, x, y;
if (code & TOP) { x = x1 + (x2 - x1)*(ymax - y1)/(y2 - y1); y = ymax; }
else if (code & BOTTOM) { x = x1 + (x2 - x1)*(ymin - y1)/(y2 - y1); y = ymin; }
else if (code & RIGHT) { y = y1 + (y2 - y1)*(xmax - x1)/(x2 - x1); x = xmax; }
else { y = y1 + (y2 - y1)*(xmin - x1)/(x2 - x1); x = xmin; }
if (code == code1) { x1 = x; y1 = y; code1 = getCode(x1, y1); }
else { x2 = x; y2 = y; code2 = getCode(x2, y2); }
setcolor(RED);
rectangle(xmin, ymin, xmax, ymax);
line(x1, y1, x2, y2);
outtextxy(50, 50, (char*)"Clipped Line");
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1 = 50, y1 = 120, x2 = 350, y2 = 250;
rectangle(xmin, ymin, xmax, ymax);
setcolor(BLUE); line(x1, y1, x2, y2); outtextxy(50, 30, (char*)"Original Line");
delay(2000); cleardevice();
cohenClip(x1, y1, x2, y2);
getch(); closegraph();
return 0;
}
11. #include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
int main() {
int gd = DETECT, gm;
int ch;
initgraph(&gd, &gm, (char*)"");
int x1 = 100, y1 = 100, x2 = 200, y2 = 200; // coordinates of a rectangle
printf("1. Translation\n2. Rotation\n3. Scaling\n");
printf("Enter your choice: ");
scanf("%d", &ch);
if (ch == 1) {
// Translation
int tx, ty;
printf("Enter translation factors tx and ty: ");
scanf("%d%d", &tx, &ty);
// Original rectangle
rectangle(x1, y1, x2, y2);
// Translated rectangle
rectangle(x1 + tx, y1 + ty, x2 + tx, y2 + ty);
} else if (ch == 2) {
// Rotation (about origin for simplicity)
int angle;
float theta;
printf("Enter angle of rotation (in degrees): ");
scanf("%d", &angle);
theta = angle * 3.14159 / 180; // converting to radians
int x = 100, y = 100; // original point
int x_rot = x * cos(theta) - y * sin(theta);
int y_rot = x * sin(theta) + y * cos(theta);
// Original point
putpixel(x, y, WHITE);
// Rotated point
putpixel(x_rot, y_rot, RED);
outtextxy(x + 10, y + 10, "Original");
outtextxy(x_rot + 10, y_rot + 10, "Rotated");
} else if (ch == 3) {
// Scaling
float sx, sy;
printf("Enter scaling factors sx and sy: ");
scanf("%f%f", &sx, &sy);
// Original rectangle
rectangle(x1, y1, x2, y2);
// Scaled rectangle (w.r.t origin)
rectangle(x1 * sx, y1 * sy, x2 * sx, y2 * sy);
} else {
printf("Invalid choice.");
getch();
closegraph();
return 0;
}
13.
#include <graphics.h>
int xmin = 100, ymin = 100, xmax = 300, ymax = 300;
bool inside(int x, int y) {
return (x >= xmin && x <= xmax && y >= ymin && y <= ymax);
bool reject(int x1, int y1, int x2, int y2) {
return (x1 < xmin && x2 < xmin) || (x1 > xmax && x2 > xmax) ||
(y1 < ymin && y2 < ymin) || (y1 > ymax && y2 > ymax);
void midpointClip(int x1, int y1, int x2, int y2) {
if (inside(x1, y1) && inside(x2, y2)) {
setcolor(RED);
line(x1, y1, x2, y2);
return;
if (reject(x1, y1, x2, y2)) return;
int mx = (x1 + x2) / 2;
int my = (y1 + y2) / 2;
midpointClip(x1, y1, mx, my);
midpointClip(mx, my, x2, y2);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1 = 50, y1 = 50, x2 = 350, y2 = 350;
rectangle(xmin, ymin, xmax, ymax);
setcolor(BLUE);
line(x1, y1, x2, y2);
outtextxy(20, 30, (char*)"Original Line (BLUE)");
delay(2000);
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
midpointClip(x1, y1, x2, y2);
outtextxy(20, 30, (char*)"Clipped Line (RED)");
getch();
closegraph();
return 0;
}
14. car:
#include <graphics.h>
#include <conio.h>
void drawCar(int x, int y) {
// Body of the car
rectangle(x, y, x + 100, y + 30);
// Upper body
rectangle(x + 20, y - 20, x + 80, y);
// Wheels
circle(x + 25, y + 30, 10);
circle(x + 75, y + 30, 10);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawCar(150, 200);
getch();
closegraph();
return 0;
}
Man :
#include <graphics.h>
#include <conio.h>
void drawMan(int x, int y) {
// Head
circle(x, y, 10);
// Body
line(x, y + 10, x, y + 40);
// Arms
line(x, y + 20, x - 15, y + 30);
line(x, y + 20, x + 15, y + 30);
// Legs
line(x, y + 40, x - 10, y + 60);
line(x, y + 40, x + 10, y + 60);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawMan(200, 150);
getch();
closegraph();
return 0;
}
15. Moton car :
#include <graphics.h>
#include <conio.h>
#include <dos.h> // For delay()
void drawCar(int x, int y) {
// Body
rectangle(x, y, x + 200, y + 50); // Main base
rectangle(x + 50, y - 50, x + 150, y); // Roof
line(x, y, x + 50, y - 50); // Left roof slope
line(x + 150, y - 50, x + 200, y); // Right roof slope
// Wheels
circle(x + 40, y + 60, 20); // Left wheel
circle(x + 160, y + 60, 20); // Right wheel
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
int x = 0, y = 300;
while (!kbhit()) { // Move until a key is pressed
cleardevice(); // Clear previous frame
drawCar(x, y); // Draw car at new position
delay(50); // Slow down the motion
x += 5; // Move car to the right
if (x > getmaxx()) // Reset when car goes off screen
x = -200;
getch();
closegraph();
return 0;