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.
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 answerHide 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
| Step | Description |
|---|---|
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.
2078 · 5 marks
Write a program to draw a line using graphics function. [5]
2077 · 5 marks
Write a program to draw a circle using graphics function. [5]
2074 · 5 marks
Why do we need graphics functions? Write a program to draw a circle. [2+3]
Make Unit 11 stick
Practice CSC115 with flashcards & quizzes