[go: up one dir, main page]

0% found this document useful (0 votes)
31 views8 pages

FYBCA Sem 2 C Lang Unit 4 - Graphics

The document discusses graphics programming in C using functions like line(), rectangle(), circle(), initgraph() and closegraph(). It explains how to draw basic shapes and colors. It provides code samples to draw a line, initialize graphics mode, and use functions like putpixel(), outtextxy() and bar() to perform graphics operations. The document also discusses colors in graphics and provides examples to draw using different colors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views8 pages

FYBCA Sem 2 C Lang Unit 4 - Graphics

The document discusses graphics programming in C using functions like line(), rectangle(), circle(), initgraph() and closegraph(). It explains how to draw basic shapes and colors. It provides code samples to draw a line, initialize graphics mode, and use functions like putpixel(), outtextxy() and bar() to perform graphics operations. The document also discusses colors in graphics and provides examples to draw using different colors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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.

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 *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

Prashant M. Savdekar Page 1


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.

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;

No, We need to pass just 4 parameters to the line function.


line(x1,y1,x2,y2);
line Function Draws Line From (x1,y1) to (x2,y2) .
Syntax : line(x1,y1,x2,y2);
Parameter Explanation
• x1 - X Co-ordinate of First Point
• y1 - Y Co-ordinate of First Point
• x2 - X Co-ordinate of Second Point
• y2 - Y Co-ordinate of Second Point
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.

3. Colors in C Graphics Programming


There are 16 colors declared in graphics.h header file. 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 (Foreground and Background Color). 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.
CONSTANT VALUE BACKGROUND? FOREGROUND?

BLACK 0 Yes Yes

BLUE 1 Yes Yes

GREEN 2 Yes Yes

CYAN 3 Yes Yes

RED 4 Yes Yes

MAGENTA 5 Yes Yes

BROWN 6 Yes Yes

LIGHTGRAY 7 Yes Yes

DARKGRAY 8 NO Yes

Prashant M. Savdekar Page 2


LIGHTBLUE 9 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 *

* To display blinking characters in text mode, add BLINK to the


foreground color. (Defined in conio.h)

4. Graphics example using color


//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
intgd = 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();
}

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,

Prashant M. Savdekar Page 3


(x, y) is the location at which pixelis to be put , and color specifies the
color of the pixel.
Explanation : A RED color pixel at (50, 40) can be drawn by using putpixel(50,
40, RED). putpixel() function can be used to draw circles, lines and ellipses
using various algorithms.
Below is the implementation of putpixel() function.
// C Implementation for putpixel()
#include <graphics.h>
#include <stdio.h>
#include<conio.h>
void main()
{
// gm is Graphics mode which is a computer display mode that
generates image using pixels.
intgd = DETECT, gm, color; // DETECT is a macro defined in

// initgraph initializes the graphics system by loading a graphics driver


from disk
initgraph(&gd, &gm, "C:\\TC\\BGI");
putpixel(85, 35, GREEN); // putpixel function
putpixel(30, 40, RED);
putpixel(115, 50, YELLOW);
putpixel(135, 50, CYAN);
putpixel(45, 60, BLUE);
putpixel(20, 100, WHITE);
putpixel(200, 100, LIGHTBLUE);
putpixel(150, 100, LIGHTGREEN);
putpixel(200, 50, YELLOW);
putpixel(120, 70, RED);
getch();
// closegraph function closes the graphics mode and deallocates all
memory allocated by graphics system .
closegraph();

}
Output :

Prashant M. Savdekar Page 4


outtextxy() function in C
The header file graphics.h contains outtextxy() function which displays the
text or string at a specified point (x, y) on the screen.
Syntax :
voidouttextxy(int x, int y, char *string);
where, x, y are coordinates of the point and, third argument contains the
address of string to be displayed.
Examples:
Input : x = 200, y = 150,
string = "Hello Geek, Have a good day !"
Output :

Computer Graphics Programs

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");

Prashant M. Savdekar Page 5


line(50,40,190,40);
printf("\t\t\n\n\n\nRECTANGLE");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(120,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30);
printf("\t\n\n\n\nECLIPSE");
ellipse(120,350,0,360,30,20);
getch();
}

Output

bar() function in C graphics


The header file graphics.h contains bar() function which is used to draw a 2-
dimensional, rectangular filled in bar.
Syntax :
void bar(int left, int top, int right, int bottom);

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

Prashant M. Savdekar Page 6


left = 290, top = 200,
right = 330, bottom = 350
Output :

Below is the implementation of bar() function.


// C implementation of bar() function
#include <graphics.h>
// driver code
void main()
{
// gm is Graphics mode which is a computer display mode that generates
image using pixels.
int gd = DETECT, gm;
// initgraph initializes the graphics system by loading a graphics driver from
disk
initgraph(&gd, &gm, "");
// location of sides
int left, top, right, bottom;
// left, top, right, bottom denotes location of rectangular bar
bar(left = 150, top = 150,right = 190, bottom = 350);
bar(left = 220, top = 250, right = 260, bottom = 350);
bar(left = 290, top = 200, right = 330, bottom = 350);
line(100, 50, 100, 350); // y axis line
line(100, 350, 400, 350); // x axis line
getch();

Prashant M. Savdekar Page 7


// closegraph function closes the graphics mode and deallocates all memory
allocated by graphics system .
closegraph();
}

Prashant M. Savdekar Page 8

You might also like