graphics.h Header File in C: Full Explanation & Shapes | Unit 3 | BKNMU BCA



BCA Sem 1  |  Unit 3  |  Computer Concepts & C Programming

Overview of graphics.h Header File in C — BKNMU Junagadh

Introduction to Computer Graphics, Initialization, Drawing Shapes (Line, Circle, Rectangle), Colors, and Legacy GCC Issues

BKNMU Junagadh NEP 2020 Unit 3 graphics.h Header Computer Graphics Legacy Borland
In simple words: Welcome to Unit 3! While the previous headers handled text, numbers, and system utilities, the graphics.h header file is used to introduce **Computer Graphics** in C programming. It is a legacy header, primarily part of older Borland C++ compilers (like Turbo C), containing declarations for functions required to draw shapes, lines, and manage colors on a graphical window. It allows transforming the console's text-only display into a simple graphical output screen. This post provides a clear overview for **BKNMU BCA Semester 1 (Unit 3)**.
📖 Core Functionalities Defined in graphics.h

The graphics.h library is widely recognized for four main areas of visual interaction routines:

initgraph() / closegraph()

1. System Management

Functions required to switch the terminal from text mode to graphics mode (`initgraph`) and properly shut it down (`closegraph`).

line() / putpixel()

2. Drawing Primitive

The building blocks: putpixel draws a single colored dot; line connects two (X, Y) coordinates with a straight line.

circle() / rectangle() / ellipse()

3. Closed Shapes

Functions that accept center points and radii/coordinates to instantly draw predefined geometric structures.

setcolor() / setbkcolor()

4. Color & Styling

Used to set the current drawing color (foreground) or the background color of the graphics window using standard color codes.

1️⃣ Master Table: Commonly Used graphics.h Functions

Here is a detailed breakdown of the primary functions supplied by the non-standard graphics.h library, focusing on their input requirements.

Function PrototypeFunctional DescriptionStandard Parameters / Coordinate System
void initgraph(int*, int*, char*)Initializes the graphics system and loads graphics drivers (often set to 'DETECT').GD driver, GM mode, Path to drivers. (X increases Right, Y increases Down from 0,0).
void line(int, int, int, int)Draws a straight line from the starting coordinate to the ending coordinate.(X1, Y1, X2, Y2) Integers.
void circle(int, int, int)Draws a circle with the specified center and radius.(CenterX, CenterY, Radius) Integers.
void rectangle(int, int, int, int)Draws a rectangle defined by its top-left and bottom-right corner coordinates.(TopLeftX, TopLeftY, BotRightX, BotRightY) Integers.
void putpixel(int, int, int)Plots a single pixel at the specified coordinate in the designated color.(X, Y, ColorCode) Integers.
void setcolor(int)Sets the current foreground color for all subsequent drawing functions.Color Code (0-15 or Constants). Example: RED, BLUE, WHITE.
ℹ️ The Graphics Coordinate System: Standard graphics environments begin with (0,0) at the *top-left corner* of the window. X-coordinates increase as you move right; Y-coordinates increase as you move *down* the screen. Maximum X and Y depend on the initialized resolution (e.g., VGAHI is typically 640x480).
💻 Code Examples & Practical Applications

1. Initializing and Drawing a Basic Circle and Line:

#include <stdio.h>
#include <graphics.h> // Required for graphics functions
#include <conio.h>    // Required for getch() pause

int main() {
    int gd = DETECT, gm; // Autodetect driver and mode

    // 1. Initialize the system
    initgraph(&gd, &gm, "C:\\TurboC3\\BGI"); // Path often required in TC

    // 2. Set current color to YELLOW
    setcolor(YELLOW);

    // 3. Draw a circle: center (200, 200), radius 50
    circle(200, 200, 50);

    // 4. Draw a line: from (100, 100) to (300, 300)
    line(100, 100, 300, 300);

    // 5. Pause output
    getch();

    // 6. Close system
    closegraph();

    return 0;
}
🔄 Step-by-Step Flow: Graphics Program Sequence
1

Step 1: Driver Detection & Initialization

The preprocessor loads declarations. `initgraph` loads BGI (Borland Graphics Interface) drivers to physically switch the console monitor from character mode to pixel addressing.

2

Step 2: Attribute Setup & Primitive Calls

Attribute functions (`setcolor`) determine styling. Drawing functions (`line`, `circle`) provide mathematical instructions for specific pixel plots.

3

Step 3: Rendering & Closure

The screen instantly renders based on pixel instructions. You *must* call `closegraph()` to release graphics drivers and return the monitor to standard text console mode when finished.

💡 Special BKNMU Exam Tip on TC Path: Pay close attention to the `initgraph` path in Turbo C! Forgetting the BGI path (e.g., "C:\\TurboC3\\BGI") or using incorrect backslashes often results in the error "BGI Error: Graphics not initialized." during BCA lab exams.
⚠️ Important Portability Warning: The standard graphics.h header file discussed here is specific to ancient Borland Turbo C compilers and is NOT supported by modern compilers like GCC (VS Code, Code::Blocks) on Windows/Linux. Students wishing to learn modern graphics must use advanced libraries like SDL, OpenGL, or special legacy GCC emulation headers (WinBGIm).
📝 Quick Revision — Key Exam Points
  • Full Header Name: Computer Graphics Header (graphics.h).
  • Header Type: Non-standard / Legacy header file (specific to Borland Turbo C).
  • Core Function Sequence: Mandatory `initgraph()` start, mandatory `closegraph()` end.
  • Key Primitive Functions: `putpixel()`, `line()`, `circle()`, `rectangle()`.
  • Coordinate System Origin (0,0): Strictly at the Top-Left corner of the screen.
  • Primary Parameters: Integers (Coordinates X, Coordinates Y, Radius, ColorCode).
  • Portability fallback: Modern GCC requires emulation via WinBGIm to run these legacy examples.

Post a Comment

Thanks for comment.

Previous Post Next Post