BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Graphics Functions in C: initgraph, line, circle, arc, ellipse, setcolor | Unit 3 | BKNMU BCA

by BCA School



BKNMU Junagadh  |  BCA Sem 1  |  Unit 3

Graphics Functions in C: Complete Guide to 2D Primitives & Controls

Comprehensive breakdown of initgraph, closegraph, line, circle, arc, ellipse, getx, outtextxy, setcolor, and setbkcolor

BKNMU Junagadh BCA Sem 1 Unit 3 graphics.h Header 2D Primitives initgraph()
In simple words: Standard console programs in C only display characters on a fixed text grid. The graphics.h library switches the screen into pixel-addressable graphics mode, allowing you to draw geometric shapes, manage color palettes, and handle graphical coordinates. This comprehensive guide covers all essential graphics functions tailored specifically for BKNMU BCA Semester 1 (Unit 3).
📖 Core Categories of Graphics Functions

The graphics system in C organizes visual routines into four primary categories:

initgraph() / closegraph()

1. System Control

Initializes the Borland Graphics Interface (BGI) subsystem, switches to graphics mode, and restores standard console mode on completion.

line() / circle() / arc()

2. Geometric Shapes

Draws fundamental 2D shapes on screen using coordinate-based mathematical plotting for lines, circles, arcs, and ellipses.

setcolor() / setbkcolor()

3. Color Attributes

Controls drawing and background colors using predefined constants or numeric values (0–15) to style shapes and viewports.

getx() / outtextxy()

4. Cursor & Text

Retrieves the current graphical pen positions (X, Y) and outputs formatted text directly at exact pixel coordinates.

1️⃣ Master Table: Graphics Functions Specifications

A detailed reference of function prototypes, parameters, and behaviors for your exam preparation:

Function Prototype Functional Purpose Parameters / Return Type
void initgraph(int*, int*, char*) Initializes the graphics hardware driver and switches the monitor into graphics mode. Takes Driver address, Mode address, and BGI directory string path.
void closegraph(void) Deallocates all graphics system memory and restores the screen to standard text mode. Takes no parameters. Returns nothing (void).
void line(int, int, int, int) Draws a straight line segment between two specified coordinate points on the screen. Coordinates: (x1, y1, x2, y2).
void circle(int, int, int) Draws a circle with a given center point and radius using the current drawing color. Center coordinate (x, y) and radius integer r.
void arc(int, int, int, int, int) Draws a circular arc from a start angle to an end angle along a given radius. Center (x, y), Start Angle, End Angle, and Radius.
void ellipse(int, int, int, int, int, int) Draws an elliptical arc or complete ellipse using horizontal and vertical radii. Center (x, y), Start/End angles, X-radius, and Y-radius.
int getx(void) Returns the current X-coordinate position of the graphical drawing cursor. Takes no parameters. Returns integer X-coordinate.
int gety(void) Returns the current Y-coordinate position of the graphical drawing cursor. Takes no parameters. Returns integer Y-coordinate.
void setcolor(int) Sets the current drawing color for all subsequent line, circle, and shape operations. Color code integer (0–15) or constants like WHITE, RED.
void setbkcolor(int) Sets the background color of the active graphics display screen window. Color code integer (0–15) or standard color constants.
ℹ️ Understanding Graphics Coordinate System:
  • The origin coordinate (0, 0) is located at the top-left corner of the screen.
  • The X-axis values increase horizontally from left to right.
  • The Y-axis values increase vertically from top to bottom.
  • Angles in arc() and ellipse() are measured in degrees counter-clockwise, where 0° is at 3 o'clock.
💻 Practical Code Example: Complete Graphics Program

The following program demonstrates initialization, drawing shapes, setting colors, and proper system closure:

#include <stdio.h>
#include <conio.h>
#include <graphics.h> // Required for all graphics functions

int main() {
    // 1. Request auto-detection of graphics driver
    int gd = DETECT, gm;

    // 2. Initialize graphics mode (specify BGI folder path in Turbo C)
    initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

    // 3. Set Background & Drawing Colors
    setbkcolor(BLACK);
    setcolor(WHITE);

    // 4. Draw a Straight Line: from (50, 50) to (250, 50)
    line(50, 50, 250, 50);

    // 5. Draw a Circle: Center (150, 150), Radius 50
    setcolor(YELLOW);
    circle(150, 150, 50);

    // 6. Draw an Arc: Center (350, 150), Angles 0 to 180, Radius 50
    setcolor(GREEN);
    arc(350, 150, 0, 180, 50);

    // 7. Draw an Ellipse: Center (250, 300), Angles 0 to 360, X-rad 80, Y-rad 40
    setcolor(CYAN);
    ellipse(250, 300, 0, 360, 80, 40);

    // 8. Display Current Cursor Position
    outtextxy(50, 380, "Graphics Primitives Demonstration Successful!");

    // 9. Pause execution to view drawing
    getch();

    // 10. Close graphics mode and restore text console
    closegraph();

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

Step 1: Driver Detection & Mode Switch

Calling initgraph() loads the hardware display driver (e.g., EGAVGA.BGI), configures screen resolution, and allocates video display memory.

2

Step 2: Attribute Setup & Primitive Rendering

Drawing routines render pixels onto the display buffer based on color parameters and the top-left oriented Cartesian pixel matrix.

3

Step 3: Cleanup & Memory Deallocation

Calling closegraph() resets the video display controller back to standard alphanumeric character mode and frees system RAM.

💡 Critical Exam Tip on BGI Path: When writing programs in Turbo C++ for university exams, always check the third parameter of initgraph(). Providing an invalid BGI path (such as "" when the files are in C:\Turboc3\BGI) causes a runtime initialization failure.
⚠️ Common Exam Pitfall: Missing closegraph(): Omitting closegraph() leaves the video controller in high-resolution graphics mode. When the program terminates, the standard text terminal may become unresponsive or display garbled characters. Always pair initgraph() with closegraph().
📝 Quick Revision — Key Exam Points
  • Header: #include <graphics.h>
  • initgraph(&gd, &gm, path): Loads BGI driver and initializes graphics mode.
  • closegraph(): Shuts down the graphics system and frees memory.
  • line(x1, y1, x2, y2): Plots a straight line between two points.
  • circle(x, y, r): Draws a circle around center (x, y) with radius r.
  • arc(x, y, st, end, r): Draws an angular arc segment.
  • ellipse(x, y, st, end, xrad, yrad): Draws elliptical curves or full ellipses.
  • setcolor() / setbkcolor(): Controls foreground drawing and screen background colors.