C Graphics Introduction
In this page:
What is Graphics in C?
Computer screens operate in two modes: text mode and graphics mode. Standard console programs use text mode. Graphics mode allows you to draw shapes, lines, and custom colors by controlling individual pixels on the screen. Traditional C graphics uses BGI (Borland Graphics Interface) via the graphics.h library, which is classic for learning computer graphics basics.
Example: What is Graphics in C?
#include <graphics.h>
int main() {
initgraph(0, 0, "");
putpixel(100, 100, WHITE);
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The Graphics Coordinate System
In C graphics, the screen is a grid of pixels. The top-left corner is the origin point (0, 0). The X-coordinate increases to the right. The Y-coordinate increases downwards. This is different from normal cartesian coordinates where Y goes up.
Example: The Graphics Coordinate System
#include <graphics.h>
int main() {
initgraph(0, 0, "");
putpixel(0, 0, WHITE);
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Compilation Requirements
To run graphics.h code on modern systems, you need an emulator like DOSBox or a compatible setup like Turbo C++. Alternatively, you can use modern libraries like winbgim or SDL2 which can compile under modern GCC configurations.
Example: Compilation Requirements
#include <graphics.h>
int main() {
initgraph(0, 0, "");
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Switching Back to Text Mode
When your drawing processes are complete, you must shut down the graphics system. The closegraph() function unloads the graphics drivers and restores the console back to standard text mode.
Example: Switching Back to Text Mode
#include <graphics.h>
int main() {
initgraph(0, 0, "");
putpixel(50, 50, WHITE);
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Basic Graphic Concepts
The fundamental unit of a digital screen is the pixel. In graphics mode, you can change the state of individual pixels or draw complex shapes composed of many pixels. A basic understanding of coordinates and screen buffers helps in building fluid graphics output.
Example: Basic Graphic Concepts
#include <graphics.h>
int main() {
initgraph(0, 0, "");
putpixel(10, 10, WHITE);
getch();
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: