11 Introduction To Graphics

C Programming · Unit 11 · 3 hrs

Introduction to Graphics

Exam-focused notes for Introduction to Graphics (C Programming, CSC115): what the TU syllabus asks and how it has actually been tested, with 4 solved past questions from this unit.

Introduction to Graphics in short

This chapter is a brief introduction to drawing on the screen in C using the classic graphics library, from the idea of a pixel and screen resolution to the functions used to draw simple shapes.

Introduction to Graphics: notes

Concepts of Graphics

A screen image is built from a grid of tiny picture elements called pixels, and the resolution of a display is the number of pixels it can show from top to bottom and from left to right; the more pixels packed into the same screen area, the sharper and clearer the resulting picture appears, since resolution is directly proportional to visual clarity. A display can operate in one of two modes: text mode, in which the screen shows only characters, and graphics mode, in which individual pixels can be addressed and coloured. In graphics mode, the screen is driven by a graphics adapter, classic examples being CGA (Color Graphics Adapter), EGA and VGA, and each adapter differs in the resolution and the number of simultaneous colours it supports.

What this unit covers

  • Concepts of Graphics
  • Graphics Initialization and Modes
  • Graphics Function

Graphics Function

2081 · 5 marks

Write a program to draw two shapes of your choice using graphics function. [5]

Show solved answer

Program to Draw Two Shapes Using Graphics Functions

Answer

The two shapes chosen are a Circle and a Rectangle, both drawn using standard graphics functions from graphics.h.


Program

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

void main()
{
    int gd = DETECT, gm;

    /* Initialize the graphics mode */
    initgraph(&gd, &gm, "c:\\tc\\bgi");

    /* Shape 1: Draw a Circle
       center at (200, 150) with radius 80 */
    setcolor(WHITE);
    circle(200, 150, 80);

    /* Shape 2: Draw a Rectangle
       top-left corner at (350, 100)
       bottom-right corner at (550, 250) */
    setcolor(WHITE);
    rectangle(350, 100, 550, 250);

    getch();

    /* Close the graphics mode */
    closegraph();
}

Explanation of Each Step

StepDescription
int gd = DETECT, gm;Declares graphics driver and mode variables. DETECT auto-detects the driver.
initgraph(&gd, &gm, "c:\\tc\\bgi")Initializes the graphics system and opens a graphics window. The path points to the BGI driver files.
setcolor(WHITE)Sets the current drawing color to white.
circle(200, 150, 80)Draws a circle with center at pixel (200, 150) and radius 80 pixels.
rectangle(350, 100, 550, 250)Draws a rectangle with top-left corner at (350, 100) and bottom-right corner at (550, 250).
getch()Waits for a key press so the output window stays visible.
closegraph()Closes the graphics mode and returns to text mode.

Output

+------------------------------------------+
|                                          |
|      ( Circle )      [ Rectangle ]       |
|       drawn at        drawn at           |
|      (200,150)       (350,100) to        |
|      r = 80          (550,250)           |
|                                          |
+------------------------------------------+

Note: The pixel origin (0, 0) is at the top-left corner of the screen. All coordinates are measured in pixels from this origin.

Open this answer on its own page →

2078 · 5 marks

Write a program to draw a line using graphics function. [5]

In C graphics, the line(x1, y1, x2, y2) function is used to draw a line from the starting point (x1, y1) to the ending point (x2, y2). The coordinates are measured in pixels, with the origin (0, 0) at the top-left corner of the screen. Before using any graphics function, the graphics mode must be...

Open this answer on its own page →

2077 · 5 marks

Write a program to draw a circle using graphics function. [5]

To draw a circle in C using graphics, we use the built-in circle() function from the graphics.h library. The program follows these steps: 1. Initialize the graphics driver and mode using initgraph() 2. Draw the circle using the circle() function 3. Set the color using setcolor() 4. Close the grap...

Open this answer on its own page →

2074 · 5 marks

Why do we need graphics functions? Write a program to draw a circle. [2+3]

--- (a) Why Do We Need Graphics Functions? Graphics functions are needed for the following reasons: 1. Visual Representation: Graphics functions allow us to draw shapes, lines, circles, and other visual elements on the screen, making output more meaningful and visually appealing compared to plain...

Open this answer on its own page →