Computer graphics using the C programming language involves creating visual content such as images, animations, and interactive applications on a computer screen. This chapter explores the fundamentals of developing computer graphics using C, including the components of a Visual Display Unit (VDU), display adapters, and graphics functions.
A Visual Display Unit (VDU), also known as a monitor, consists of various components that work together to display visual information. These components include the screen, cathode ray tube (CRT), display controller, and input/output ports.
A display adapter, or graphics card, is a hardware component that connects the computer to the monitor and processes graphical data for display. It converts digital signals from the computer into analog signals for the monitor and includes memory and processing units for rendering images.
C provides a set of libraries and functions for performing basic graphics operations, such as drawing lines, circles, and rectangles, on the screen. These functions are part of the graphics.h library and can be used to create simple graphical applications.
Let's explore some common graphics functions in C along with examples:
This example demonstrates how to draw a line on the screen using the line() function.
Syntax :
line ( int x1, int x2, int y1, int y2 );
Example :
#include <graphics.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, NULL); line(100, 100, 300, 200); getch(); closegraph(); return 0; }
This example illustrates how to draw a circle on the screen using the circle() function.
Syntax :
circle ( int x1, int y1, int radius );
Example :
#include <graphics.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, NULL); circle(200, 200, 100); getch(); closegraph(); return 0; }
This example demonstrates how to draw a rectangle on the screen using the rectangle() function.
Syntax :
rect ( int top_left_x, int top_left_y, int bottom_right_x, int bottom_right_y );
Example :
#include <graphics.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, NULL); rectangle(100, 100, 300, 200); getch(); closegraph(); return 0; }
This example shows how to draw an ellipse on the screen using the ellipse() function.
Syntax :
ellipse ( int x1, int y1, int start_angle, int end_angle, int x_radius, int y_radius );
Example :
#include <graphics.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, NULL); ellipse(300, 200, 0, 360, 100, 50); getch(); closegraph(); return 0; }
This example demonstrates how to draw a polygon on the screen using the drawpoly() function.
Syntax :
drawpoly ( int numpoints, int polypoints [] );
Example :
#include <graphics.h> int main() { int gd = DETECT, gm; initgraph(&gd, &gm, NULL); int points[] = {100, 100, 200, 200, 300, 100, 200, 50, 100, 100}; drawpoly(5, points); getch(); closegraph(); return 0; }
Developing computer graphics using C offers a powerful and versatile approach to creating visual content on a computer screen. By understanding the components of a Visual Display Unit, display adapters, and graphics functions provided by the C programming language, developers can create interactive and visually appealing applications for various purposes.