graphics
graphics
Graphics in C
In C, graphics are typically implemented using libraries like Turbo C's graphics.h.
Although this library is outdated, it remains a useful tool for learning.
Setup
1. Turbo C Installation: Download and install the Turbo C++ IDE, which
includes the graphics.h library.
A pixel is the smallest unit of a graphic—a single point of color on the screen.
Use putpixel(x, y, color) to draw a pixel at coordinates (x, y).
putpixel(100, 100, WHITE); // Draws a white pixel at (100, 100)
Line
A line connects two points.
Circle
Rectangle
Ellipse
Text
Colors
Colors are predefined constants like BLACK, WHITE, RED, etc. These can be
used as parameters in functions.
#include <graphics.h>
#include <conio.h>
int main() {
// Draw shapes
closegraph();
return 0;
Steps:
Code:
putpixel(x, y, WHITE);
x += Xinc;
y += Yinc;
Steps:
Code:
int p = 2 * dy - dx;
putpixel(x, y, WHITE);
x++;
if (p < 0) {
p += 2 * dy;
} else {
p += 2 * (dy - dx);
y++;
}
Steps:
2. Recursively fill neighboring pixels with the fill color until the boundary
color is reached.
Code:
void floodFill(int x, int y, int fillColor, int boundaryColor) {
if (getpixel(x, y) != boundaryColor && getpixel(x, y) != fillColor) {
putpixel(x, y, fillColor);
}
Advanced Topics
Common Errors
Moving Forward