CSC115 C Programming

C ProgrammingUnit 113 min read

Introduction to Graphics: Graphics Library, Initialization, and Drawing Primitives

Unit 11 of C Programming: This unit covers the fundamental concepts of computer graphics in C, focusing on the BGI (Borland Graphics Interface) library, the initialization process, coordinate systems, and the implementation of various geometric shapes.

Key points

  • Graphics programming allows for visual representation of data, making applications more intuitive and user-friendly.
  • The `graphics.h` header file provides the necessary functions to interface with the system's display hardware.
  • Proper initialization using `initgraph()` is mandatory before executing any graphics-related commands.
  • The coordinate system in C graphics starts with (0,0) at the top-left corner of the screen.
  • Closing the graphics mode using `closegraph()` is essential to restore the system to its original state and free memory.

Overview of Computer Graphics in C

Computer graphics in C refers to the ability to draw lines, shapes, and images on the screen using standard library functions. While standard C is text-based, the Borland Graphics Interface (BGI) provides a set of functions to manipulate pixels and create visual outputs.

Why do we need Graphics Functions?

  1. Visual Representation: To display data in charts, graphs, or diagrams.
  2. User Interface: To create interactive menus, buttons, and windows.
  3. Game Development: To render characters, backgrounds, and animations.
  4. Simulation: To model physical systems or architectural designs visually.

The Graphics Environment

To use graphics in C, the compiler must support the graphics.h library. The process involves three main phases:

  1. Initialization: Setting up the graphics driver and mode.
  2. Drawing: Using primitive functions to render shapes.
  3. Termination: Closing the graphics mode to return to text mode.

Initialization and Termination

The initgraph() function initializes the graphics system.

int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
  • gd (Graphics Driver): DETECT automatically selects the best driver.
  • gm (Graphics Mode): Specifies the resolution.
  • path: The directory where BGI files are located.

After drawing, closegraph() is called to unload the graphics driver and restore the screen.

Drawing Primitives

The following table summarizes common graphics functions:

Function Description
line(x1, y1, x2, y2) Draws a line from (x1, y1) to (x2, y2).
circle(x, y, radius) Draws a circle with center (x, y) and given radius.
rectangle(left, top, right, bottom) Draws a rectangle.
ellipse(x, y, start, end, xrad, yrad) Draws an elliptical arc.
setcolor(color) Sets the current drawing color.

Worked Examples

Program to Draw a Circle

#include <graphics.h>
#include <conio.h>

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

    // Draw a circle at center (200, 200) with radius 50
    circle(200, 200, 50);

    getch(); // Wait for user input
    closegraph();
    return 0;
}

Program to Draw Multiple Shapes

#include <graphics.h>
#include <conio.h>

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

    // Draw a line
    line(50, 50, 200, 50);

    // Draw a rectangle
    rectangle(50, 100, 150, 200);

    getch();
    closegraph();
    return 0;
}

Coordinate System

In the graphics mode, the screen is treated as a grid of pixels.

  • The origin (0, 0) is at the top-left corner.
  • The x-axis increases from left to right.
  • The y-axis increases from top to bottom.

Advantages and Disadvantages

  • Advantages: Enhances user experience, simplifies complex data interpretation, and provides a platform for basic game development.
  • Disadvantages: Graphics functions are highly dependent on specific compilers (like Turbo C++) and are not part of the standard C library (ANSI C), making them non-portable to modern IDEs like VS Code or GCC.

Exam Tip

  • Focus on Syntax: When writing programs in the exam, always include the initgraph and closegraph functions. Forgetting these is a common reason for losing marks.
  • Path Specification: If asked for the path in initgraph, use the standard "C:\\TURBOC3\\BGI" as it is the expected answer in TU/PU exams.
  • Conceptual Clarity: If asked "Why do we need graphics?", emphasize the transition from text-based interfaces to visual interfaces.
  • Diagrams: If asked to draw a shape, clearly label the coordinate parameters (e.g., circle(x, y, r) where x, y is the center).

Based on the TU BSc CSIT syllabus for C Programming (CSC115), unit 11.

Discussion

Loading…