FYBCA Sem 2 C Lang Unit 4 - Graphics
FYBCA Sem 2 C Lang Unit 4 - Graphics
Introduction to Graphics in C:
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.
1. First graphics program (Draw a line)
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main(void) {
intgdriver = DETECT, gmode;
int x1 = 200, y1 = 200;
int x2 = 300, y2 = 300;
clrscr();
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
line(x1, y1, x2, y2);
getch();
closegraph();
}
2. Explanation of Code :
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.
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 *gdriver is set to DETECT, then initgraph sets *gmode 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 search for
We have declared variables so that we can keep track of starting and ending
point.
int x1=200, y1=200;
int x2=300, y2=300;
DARKGRAY 8 NO Yes
LIGHTGREEN 10 NO Yes
LIGHTCYAN 11 NO Yes
LIGHTRED 12 NO Yes
LIGHTMAGENTA 13 NO Yes
YELLOW 14 NO Yes
WHITE 15 NO Yes
BLINK 128 NO *
putpixe()&closegraph() function in C
The header file graphics.h contains putpixel() function which plots a pixel at
location (x, y) of specified color.
Syntax :
voidputpixel(int x, int y, intcolor);
where,
}
Output :
Write a Program to draw basic graphics construction like line, circle, arc, ellipse
and rectangle.
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
initgraph (&gd,&gm,"c:\\tc\\bgi");
setbkcolor(GREEN);
printf("\t\t\t\n\nLINE");
Output
where,
left specifies the X-coordinate of top left corner,
top specifies the Y-coordinate of top left corner,
right specifies the X-coordinate of right bottom corner,
bottom specifies the Y-coordinate of right bottom corner.
Examples :
Input : left = 150, top = 150,
right = 190, bottom = 350
left = 220, top = 250,
right = 260, bottom = 350