Graphics in C allows drawing shapes, displaying colors, and creating visual output using graphical functions.
1. Initialization
- Graphics require initializing the graphics driver and mode.
#include <graphics.h>
int gd = DETECT, gm;
initgraph(&gd, &gm, “”); // Initialize graphics mode
- DETECT → Automatically detects the graphics driver.
- ” ” → Path to BGI driver (usually empty if BGI files are configured).
2. Graphical Mode
- Switches program from text mode to graphics mode.
- After initialization, all graphical functions can be used.
- Close graphics mode:
closegraph(); // Return to text mode
3. Graphical Functions
- Drawing Functions:
- line(x1, y1, x2, y2) → Draw line between two points
- rectangle(x1, y1, x2, y2) → Draw rectangle
- circle(x, y, radius) → Draw circle
- ellipse(x, y, start_angle, end_angle, x_radius, y_radius) → Draw ellipse
- Text and Color Functions:
- outtextxy(x, y, “Text”) → Display text at position
- setcolor(color) → Set color for drawing
- setfillstyle(pattern, color) → Set fill pattern and color
- Other Functions:
- bar(x1, y1, x2, y2) → Draw filled rectangle
- floodfill(x, y, border_color) → Fill area with color
Key Takeaways
- Graphics in C requires initializing graphics driver and mode.
- Use initgraph() to start, closegraph() to exit graphics mode.
- Common functions: line, rectangle, circle, ellipse, outtextxy.
- Colors and fill patterns can be set for shapes.
- Graphics provides visual representation for better user interaction and program output.