Graphics in C++
-
Upload
ahsan-mughal -
Category
Education
-
view
364 -
download
0
description
Transcript of Graphics in C++
Group Name Pure onesPresenters Ali Murtaza 11014119-044M. Hamid 11014119-007Qasim Ali 11014119-128M. Tayyab Chema 11014119-045Ahsan Raza 11014119-017
Turbo C has a good collection of graphics libraries. If you know the basics of C, you can easily learn graphics programming.
Why we use graphics ? We use graphics in C language to give a different
look to our program. Function related graphics are use to create different
shapes in different colour.
Graphics in C language
Files for compiler to work in graphics. The header file “graphics.h” i.e built-in function is use in graphics. These files stores in Borland graphics interface (BGI)
files. These files contain graphics drive program.Display modes in C . The output of a program can be displayed on the
screen in two modes. Text mode. Graphics mode.
Graphics in C language
In text mode screen is normally divided into rows and columns. We can move the cursor to specified location by using “gotoxy (x,y);” function.
In text mode there are 80 columns (0-79) and 25 rows (0-24).
In text mode only text can be displayed.
Text mode
In graphics mode the screen is divided into smalldots called pixels.
For example:In VGA monitor the screen is divided into 480
rows and 640 columns of dots. Thus the monitor VGA screen is divided into 640 X 480 pixels.
The resolution of different type of (display adopter) are
VGA 640 X 480 SVGA 800 X 600
Graphics mode
The computer display system must be initialized into graphics mode before calling the graphics function.
Function for initialization of graphics:The “initgraph” function is used to initialized
the graphics mode. This function is already defined in “graphics.h”
header file.
Initialization of graphics mode
SyntaxThe syntax of initgraph is:
initgraph(&driver, &mode,”path”); Driver
Represents the graphics driver installed in computer. It may be an integer variable or an integer constant.
Mode
Represents the output resolution on the computer screen. The normal mode for VGA is VGAHI. It gives the highest resolution.
Syntax of initgraph
&Represents the addresses of constants for driver and
mode. Path
Represents the path of VGA driver.
Which “ C:\\compiler name\\BGI”.
Syntax of initgraph cont…
There are two method of using initgraph function. Manually
In this way you have to tell the computer all about drivers or modes of VGA. In this way you don’t have to use & operator.
For example: initgraph(vga,vgahi,”C:\\compiler name\\BGI ”). Auto-detect
In this method you have only declare two integer type variables name as you wish. Initialize the 1st variable with a key word “DETECT”. In this method we should use & operator.
For example: initgraph(&driver, &mode,”C:\\compiler name\\BGI”);
Way of use initgraph function
The cleardevice function is used to clear screen in graphics mode as clrscr( ) function is used in text mode.
Syntax cleardevice( );
Closegraph function The closegraph function is used to restore ( turn of all the
graphics ) the screen into text mode. Syntax closegraph();
Cleardevice function
The outtext function is used to print text on the screen in graphics mode.
Syntax outtext(Any sentence or character);
Moveto function The moveto function is used to move the cursor on the screen
in specific location. Syntax moveto(x , y);X represents the x-coordinate of the screen.Y represents the y-coordinate of the screen.
Outtext function
#include<conio.h>
#include<graphics.h>
void main()
{
cleardevice();
int d,m;
d= DETECT;
initgraph( &d, &g, “C:\\TC\\BGI”);
outtext(I love programming);
moveto (250,179);
outtext(I love programming);
getch();
closegraph();
}
Example program
The outtextxy function is similar to the outtext function but it is used to print text on the specified location. This function is both to outtext and moveto function.
Syntax outtextxy(x,y,”any string or character”);
Settextstyle function The settextstyle function is used to define text style in graphics mode. Syntax settextstyle(style, direction, size); Style represents the font style and its value is 0-10. Direction represents the direction of text (horizontal or vertical) its
value is 0-1. Size represents the font size of the text its value is 0-72.
Outtextxy function
The setcolor function is used to define the color of the object or text in graphics mode.
Syntax setcolor(color ); There are 16 color in declared in graphics.h .
BLACK: 0 BLUE: 1 GREEN: 2 CYAN: 3 RED: 4MAGENTA: 5 BROWN: 6 LIGHTGRAY: 7DARKGRAY: 8 LIGHTBLUE: 9 LIGHTGREEN: 10LIGHTCYAN: 11LIGHTRED: 12 LIGHTMAGENTA: 13YELLOW: 14 WHITE: 15
Setcolor function
The setbkcolor function is used to define back ground color on the output screen.
Syntax setbkcolor(color);
Setbkcolor function
#include<conio.h>
#include<graphics.h>
void main()
{
cleardevice();
int d=DETECT, m;
initgraph(&d, &m, “C:\\TC\\BGI”);
setbkcolor(5);
setcolor(1);
settextstyle(1,0,5);
outtextxy(75,8,”Pakistan”);
setcolor(15);
settextstyle(1,1,5);
outtextxy(75,40,”Pakistan”);
getch();
closegraph();
}
Example program
The circle function is used to display the circle on the out put screen.
Syntax circle( x, y, radius );
Arc function The arc function is used to draw a circular arc starting from a
specified angle from to another specified angle. Syntax arc ( x, y, starting angle, ending angle, radius); Where all parameters are both circle and arc have integer type.
Circle function
The line function is used to draw a line between two points on the screen.
Syntax line (x1,y1,x2,y2);
Rectangle function The rectangle function is used to draw a rectangle between
two points on the screen. Syntax rectangle (x1,y1,x2,y2); Where x1, y1 specify the x ,y coordinate of the first point. And
x2 , y2 specify the x , y coordinate of the second point. All four parameters are integer type.
Line function
#include<conio.h>
#include<graphics.h>
void main()
{ int d=DETECT, m;
cleardevice();
initgraph( &d, &m, “C:\\TC\\BGI”);
outtextxy(35,65,”Circle”);
circle(50,40,15);
outtextxy(150,60,”Arc”);
arc(155,40,0,180,15);
outtextxy(42,160,”Rectangle”);
rectangle(135,100,120,150);
outtextxy(185,120,”Line”);
line(140,100,230,100);
getch();
closegraph();
}
Example program