Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

C Graphics Programming

C Graphics programming in C involves drawing shapes, using mathematical functions for curves, and creating simple animations. The process begins by including the graphics.h header file and initializing graphics drivers with the initgraph function. The document also details color usage in graphics programming, providing a list of 16 color macros and their corresponding integer values, along with an example program that demonstrates drawing a rectangle.

Uploaded by

Alekh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Graphics Programming

C Graphics programming in C involves drawing shapes, using mathematical functions for curves, and creating simple animations. The process begins by including the graphics.h header file and initializing graphics drivers with the initgraph function. The document also details color usage in graphics programming, providing a list of 16 color macros and their corresponding integer values, along with an example program that demonstrates drawing a rectangle.

Uploaded by

Alekh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C Graphics Programming:

Graphics programming in C used to drawing various geometrical shapes(rectangle, circle eclipse


etc), use of mathematical function in drawing curves, coloring an object with different colors and
patterns and simple animation programs like jumping ball and moving cars.

The first step in any graphics program is to include graphics.h header file. The graphics.h header
file provides access to a simple graphics library that makes it possible to draw lines, rectangles,
ovals, arcs, polygons, images, and strings on a graphical window.

The second step is initialize the graphics drivers on the computer using initgraph method
of graphics.h library.
void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectoryPath);

It initializes the graphics system by loading the passed graphics driver then changing the system
into graphics mode. It also resets or initializes all graphics settings like color, palette, current
position etc, to their default values. Below is the description of input parameters of initgraph
function.

 graphicsDriver : It is a pointer to an integer specifying the graphics driver to be used. It


tells the compiler that what graphics driver to use or to automatically detect the drive. In
all our programs we will use DETECT macro of graphics.h library that instruct compiler
for auto detection of graphics driver.
 graphicsMode : It is a pointer to an integer that specifies the graphics mode to be used. If
*graphdriver is set to DETECT, then initgraph sets *graphmode to the highest resolution
available for the detected driver.
 driverDirectoryPath : It specifies the directory path where graphics driver files (BGI
files) are located. If directory path is not provided, then it will seach for driver files in
current working directory directory. In all our sample graphics programs, you have to
change path of BGI directory accordingly where you turbo C compiler is installed.

Colors in C Graphics Programming


There are 16 colors declared in C Graphics. We use colors to set the current drawing color,
change the color of background, change the color of text, to color a closed shape etc. To specify
a color, we can either use color constants like setcolor(RED), or their corresponding integer
codes like setcolor(4). Below is the color code in increasing order.

COLOR MACRO INTEGER VALUE


COLOR MACRO INTEGER VALUE

BLACK 0

BLUE 1

GREEN 2

CYAN 3

RED 4

MAGENTA 5

BROWN 6

LIGHTGRAY 7

DARKGRAY 8

LIGHTBLUE 9

LIGHTGREEN 10

LIGHTCYAN 11
COLOR MACRO INTEGER VALUE

LIGHTRED 12

LIGHTMAGENTA 13

YELLOW 14

WHITE 15

At the end of our graphics program, we have to unloads the graphics drivers and sets the screen
back to text mode by calling closegraph function. Here is our first C Graphics program to draw a
straight line on screen.

In this program initgraph function auto detects an appropriate graphics driver and sets graphics
mode maximum possible screen resolution. Then line function draws a straight line from
coordinate (100, 100) to (200, 200). Then we added a call to getch function to avoid instant
termination of program as it waits for user to press any key. At last, we unloads the graphics
drivers and sets the screen back to text mode by calling closegraph function.

Graphics Example Using COLORS:


//Include the graphics header file
#include<graphics.h>
#include<stdio.h>
#include<conio.h>

void main()
{
//Initialize the variables for the graphics driver and mode
int gd = DETECT, gm;
clrscr();
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

//Set the color of the object you want to draw.


setcolor(BLUE);

//Draw an object. For this example,drawing a rectangle using the rectangle function
rectangle(50,50,100,100);

getch();

//unloads the graphics drivers


closegraph();
}

You might also like