C Draw Box Around Text Write to File
C++ GRAPHICS Introduction:
C++ GRAPHICS Functions relating to graphics are used to create different shapes in different colors. The graphics functions require a graphics monitor (nowadays almost all computers have graphics monitors) and a graphics card such as VGA, SVGA or EGA. Colour monitor is recommended for viewing graphics in colors.
The graphics include:
- Lines in unlike colors and styles.
- Unlike shapes like circles, rectangles in different styles.
- Text in different fonts, sizes, and styles.
The following files are needed with the C++ compiler to work in graphics:
- The Header File "graphics.h" that contains built-in graphic functions. This header file is included in the program.
- Borland Graphics Interface (BGI) files. These files contain graphics driver programs that initialize the figurer monitor into graphics fashion. These files take BGI extension.
- Character font style files having extension "chr". These files are used to procedure text or characters in graphics way.
Display Mode:
The output of a program can be displayed on the screen in two modes. These modes are:
- Text Mode
- Graphics Mode
C++ Text Mode:
In a text manner, the screen is normally divided into 80 columns and 25 rows. The topmost row is 0 and the bottom-most row is 24. Similarly. the leftmost column is 0 and the rightmost column is 79. In-text mode, only text can be displayed. The images and one graphics object cannot be displayed.
C++ Graphics Mode:
Images and other graphic objects are displayed on the SC graphics mode. In this style, the output is displayed on the calculator screen in points or pixels.
In graphics mode, the screen is divided into small dots called For example, in the VGA monitor, the screen is divided into 480 row 640 columns of dots. Thus the VGA monitor screen is carve up 640×480 pixels. The number of dots per inch is called resolution screen. The dots are very close to each other. The more than the number pixels, the more clearer the graphics is.
The monitor types (display adapter) and their respective resolution are given below.
Monitor type | Resolution | Color |
CGA VGA SVGA | 640×200 640×480 800×600 | 16 16 256 |
Initializing C++ Graphics Mode:
The computer display system must be initialized into graphics mode before calling the graphics function.
The "initgraph" function is used to initialize the display into graphics style. This function is a role of the "graphics.h" header file. So this file is included in the program earlier executing "initgraph" role.
The syntax of initgraph" function is:
initgraph(&driver, &mode, "path");
Where:
Commuter:
Represents the graphics driver installed on the computer. It may be an integer variable or an integer abiding identifier, e.g. CGA, EGA, SVGA, etc.
The-graphics driver tin as well be automatically detected by using the keyword "Observe". Letting compiler discover the graphic driver is known every bit auto-detect.
If the driver is to be automatically detected, the variable driver is declared as of integer type and DETECT value is assigned to it as shown below.
int driver, way;
driver = DETECT;
This argument must exist placed earlier "initgraph" part. When the above statement is executed. he computer automatically detects the graphic driver and the graphics mode.
Style
Represents output resolution on the calculator screen. The normal fashion for VGA is VGAHI. It gives the highest resolution
If the driver is on auto-detected, then its use is optional. The reckoner automatically detects the commuter as well as the manner.
&
represents the addresses of constant numerical identifiers of driver and manner. If constants (e.g., VGA, VGAHI) are used, then "&" operator is not used equally shown below:
initgraph (VGA, VGAHI, "path");
path
Represents the path of graphic drivers. It is the directory on the dish where the BGI files are located. Suppose the BGI files are stored in "C:\TC\BGI", so the complete path is written as:
initgraph (VGA, VGAHI, "C:\TC\\BGI");
Use of double backslash "\" is to be noted. One backslash is used as escape character and other for the directory path. If the BGI files are in the current directory, then the path is written as:
initgraph (VGA, VGAHI, " ");
Note:
The variables for driver and way must exist of int type but their names may be different.
Instance how to employ initgraph reckoner brandish into C++ graphics mode:
#include<graphics.h> master ( ) { int d , chiliad ; d = Notice ; Initgraph ( & d , & grand , "c : \ \ tc") ; } |
In the above example, the BGI files must be in the specified directory, ane.due east., in "c:\tclbgi". If the BGI files are in the directory path"c:\tc" and then the higher up argument is written every bit:
initgraph(&d, &chiliad, "C:\\TC");
The "cleardevice" Function:
The "cleardevice" function is used to clear the screen in graphics mode. Information technology is similar to "clrscr" part that is used to articulate the screen text fashion. Its syntax is:
cleardevice();
Closing Graphics Fashion
The "closegraph" function is used to restore the screen to the text fashion.
When graphics manner is initialized, memory is allocated to the graphics system. When "closegraph" function is executed, it de-allocates all retention allocated to the graphics system. This function is commonly used a the end of the plan. Its syntax is:
closegraph();
Text in Graphics Fashion
In graphics node: text can also be written in different fonts, styles. sizes, colors, and directions. The graphic functions commonly used to create and print text are described beneath.
The "outtext" Role
The "outtext" function is used to print text on the computer screen in graphics manner. The text is printed at the current cursor position on the screen. Its syntax is:
outtext(cord);
Where:
string
Represents the characters that are to be printed on the screen. Information technology may be a cord variable or string constant. The string constant is enclosed in double-quotes.
Example how to employ cleardevice, closegraph and outtext function and print Programming digest into C++ graphics mode:
#include<graphics.h> #include<conio.h> main ( ) { int d , m ; d = Discover ; initgraph ( & d , & m , "") ; cleardevice ( ) ; outtext ("Programming assimilate") ; getch ( ) ; closegraph ( ) ; } |
The "moveto" Function
The "moveto" function is used to move the electric current cursor position to a specified location on the screen where the output is to be printed. Information technology is like to "gotoxy" function used in text mode. Its syntax is:
moveto (x, y);
Where:
x
Represents the x-coordinate of the screen. It is the horizontal distance in pixels from the left of the screen. It may be an unsigned int blazon value or variable. For VGA, its value is from 0 to 639.
y
represents the y-coordinate of the scren. It is the vertical distance in pixels from the top of the screen. It may be an unsigned int type value or variable. For VGA, its value is from 0 to 479.
Example how to use moveto office and print Programming digest into C++ graphics mode:
#include<graphics.h> #include<conio.h> main ( ) { int d , m ; d = Find ; initgraph ( & d , & g , "") ; cleardevice ( ) ; moveto ( 400 , 200 ) ; outtext ("Programming assimilate") ; getch ( ) ; closegraph ( ) ; } |
The "outtextxy" Function
The "outtextxy" function is like to the outtext" part merely it is used to print text on the screen at a specified location. This function serves the purpose of both the "moveto" and "outtext" functions. Its syntax is:
outtextxy (x, y, string);
where:
ten
represents the x-coordinate of the screen. Information technology is the horizontal altitude in pixels from the left of the screen. It may be unsigned int type value or variable. For VGA, its value is from 0 to 639.
Y
represents the y-coordinate of the screen. Information technology is the vertical distance in pixels from the top of the screen. It may be unsigned int type value or variable. For VGA, its value is from 0 to 479.
String
represents the cord of characters that is to exist printed on the reckoner screen. Information technology may be a cord variable or a string. abiding. The cord constant is enclosed in double quotes.
Example how to utilize outtextxy part and print Programming digest into C++ graphics style:
#include<graphics.h> #include<conio.h> main ( ) { int d , grand ; d = Detect ; initgraph ( & d , & g , "") ; cleardevice ( ) ; outtextxy ( 100 , 200 , "Programming digest") ; getch ( ) ; closegraph ( ) ; } |
The "settextstyle" Function
The "settextstyle" function is used to ascertain the text style in graphics mode. The text style includes the font type, font size and text direction. The syntax of this role is given equally: settextstyle (mode, dir, size);
All the three parameters are of int type. These may be int type values or variables.
Where:
Style:
specifies the font manner. Its value range is from 0 to ten.
Dir:
specifies the direction of the text in which it is to be displayed. Its value is from 0 to i. It may be a numerical constant identifier. It is HORIZ DIR (for horizontal management) or VERT_DIR (for vertical direction).
Size:
specifies the font size of the text. Its value is from 0 to 72.
Example how to use settextstyle function and print Programming digest into C++ graphics mode:
#include<graphics.h> #include<conio.h> chief ( ) { int d , m , c ; d = Notice ; initgraph ( & d , & 1000 , "") ; cleardevice ( ) ; for ( c = 0 ; c <= 10 ; c ++ ) { setextstyle ( c , 0 , 0 ) ; outtextxy ( 100 , twenty + c* xx , "Programming digest") } getch ( ) ; closegraph ( ) ; } |
The "setcolor" Part
The setcolor" function is used to define color of the objects and the text in graphics style. Its syntax is: setcolor (co);
where:
со
Represents the color. Information technology may exist an int type value or variable. For VGA, its value is from 0 to 15. It may as well be a numerical constant identifier, e.chiliad. Bluish, Green, RED etc.
The "setbkcolor" Office
The "setbkcolor" office is used in graphics mode to ascertain the background color of the screen. Its syntax is: setbkcolor(co);
Where:
co
Specifies the color. It may be an information technology type value or variable. For VGA. Its value is from 0 to fifteen. It may also exist numerical constant identifier eg blue, greenish and blood-red etc.
Example how to use setcolor and setbkcolor office and print Programming digest into C++ graphics mode:
1 2 3 iv 5 6 vii viii 9 10 11 12 xiii 14 15 16 17 18 19 | #include<graphics.h> #include<conio.h> main ( ) { int d , m , co ; d = DETECT ; initgraph ( & d , & m , "") ; cleardevice ( ) ; for ( co = 0 ; co <= fifteen ; co ++ ) { setbkcolor ( co ) ; setcolor ( co + 1 ) ; settextstyle ( 0 , 0 , ii ) ; outtextxy ( 100 , 10 + co* 20 , "Programming digest") ; outtextxy ( 200 , 200 ,"printing any key….") ; getch ( ) ; } closegraph ( ) ; } |
Creating Objects in C++ Graphics Mode
Different objects, east.g. lines, circles, rectangles and many other shapes are created in graphics manner using various built-in functions. Following are the functions that are unremarkably used to create graphics objects:
The "circle" Function
The "circle" function is used to draw a circle on the screen. Its syntax is:
circle(ten, y, radius);
All the three parameters are of int blazon. These may exist int type values or variables.
Where
ten & y
Specifies the center signal of the circumvolve. These are the x- coordinate and y-coordinate of the center of the circumvolve on the screen.
Radius
Specifies the radius of the circle.
Example how to brand a circle using circle office in C++ graphics manner:
#include<graphics.h> #include<conio.h> main ( ) { int d , thou , r , c ; d = DETECT ; initgraph ( & d , & yard , "") ; cleardevice ( ) ; for ( c = i ; c <= 15 ; c ++ ) { setcolor ( c ) ; circle ( 300 , 200 , c* 10 ) ; } getch ( ) ; closegraph ( ) ; } |
The "arc" Function
The arc function is used to draw a circular arc starting from a specified angle and up to another specified angle. Its syntax is:
arc (x, y, stangle, endangle, radius);
All the five parameters are of int types. These may be constants or variables.
Where:
x & y
specify the center signal of the circle. These are the x- coordinate and y-coordinate of the center of the arc on the screen.
Stangle:
specifies the starting bending of the arc in caste.
Endangle:
specifies the ending bending of the arc in degree.
radius:
specifies the radius of the arc.
Note:
The arc function tin likewise be used to draw a circumvolve past giving the starting bending 0 and ending angle 360. Similarly, it can also be used to draw line by giving the same values for starting and catastrophe angles.
Instance how to make arc in c++ using arc function in graphics fashion:
1 2 three iv 5 6 7 8 nine ten eleven 12 13 14 15 16 17 | #include<graphics.h> #include<conio.h> main ( ) { int d , one thousand , c ; d = DETECT ; initgraph ( & d , & chiliad , "") ; cleardevice ( ) ; for ( c = 1 ; c <= 15 ; c ++ ) { setcolor ( c ) ; arc ( 300 , 200 , 45 , 145 , c* 10 ) ; } getch ( ) ; closegraph ( ) ; } |
The "line" Function
The "line" function is used to draw a line betwixt ii points on the screen. Its syntax is:
line (x1, y1, x2, y2);
All the four parameters are of int type. These may be integer blazon values or variables.
Where:
x1 & y1
specify the x & y co-ordinates of outset point.
x2 & y2
specify the 10 & y co-ordinates of second betoken.
For case, to draw a line between left upper corner & right bottom corner of the screen for VGA monitor, the statement is written as:
line(0, 0, 639, 479);
or
line (639, 479,0, 0);
The bespeak (0, 0) represents the upper left corner of the screen and signal (639, 479) represents the lesser right corner of the screen.
Similarly, to draw a line between lesser left corner & upper correct corner of the screen (for VGA monitor), the statement is written as;
line (0, 479, 639, 0);
or
line (639, 0, 0, 479);
The point (0,479) represents the bottom left comer of the screen and bespeak (639, 0) represents the upper right corner of the screen.
Example how to make lines in c++ using line part in graphic mode:
i 2 three 4 5 vi 7 8 9 x 11 12 xiii 14 xv 16 17 eighteen 19 | #include<graphics.h> #include<conio.h> chief ( ) { int d , 1000 , c , x1 ; d = Observe ; initgraph ( & d , & k , "") ; cleardevice ( ) ; for ( c = 1 , x1 = 1 ; x1 <= 640 ; c ++ , x1 += v ) { setcolor ( c ) ; line ( 320 , 240 , x1 , 0 ) ; if ( c >= fifteen ) c = 1 ; } getch ( ) ; closegraph ( ) ; } |
The "rectangle" Role
The "rectangle function is used to draw a rectangle betwixt two points on the screen. Its syntax is:
rectangle (x1, y1, x2, y2);
All four parameters are of int types. These may be integer blazon values or variables.
where
x1 & y1:
specify the x & y co-ordinates of the commencement point.
x2 & y2:
specify the x & y co-ordinates of the 2d | oint.
For example, to draw a rectangle between upper left comer & bottom right corner of the screen (for VGA monitor), the argument is written as:
rectangle(0, 0, 639, 479);
or
rectangle (639, 479,0, 0);
The point (0, 0) represent the upper left corner of the screen and point (639, 479) represents the lesser right comer of the screen.
Example how to brand a rectangle in c++ using the rectangle function in graphics mode:
#include<graphics.h> #include<conio.h> primary ( ) { int d , thousand ; d = detect ; initgraph ( & d , & k , "") ; cleardevice ( ) ; rectangle ( 100 , 300 , 500 , 200 ) ; getch ( ) ; closegraph ( ) ; } |
The "setlinestyle" Role
The "setlinestyle" office is used to set the line style of different objects that are fatigued. The lines of these objects tin be fatigued in different styles, patterns and thickness. These attributes of the line are defined through this function. Its syntax is:
setlinestyle (manner, pattern, thickness);
It has three parameters all of int types. These may be int type values or variables.
Where
style:
specifies the line fashion. Its value may exist from 0 to four.
pattern:
specifies the line pattern. Its value may be from 0 to 12.
thickness:
specifies the thickness of line. Its value may be from 0 to 3.
Case how to make stylish lines in c++ using setlinestyle part in graphic mode:
i 2 3 iv 5 6 vii eight 9 10 11 12 13 14 xv 16 17 eighteen 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include<graphics.h> #include<conio.h> chief ( ) { int d , g , y = 100 , i ; d = Observe ; initgraph ( & d , & m , "") ; cleardevice ( ) ; outtextxy ( 100 , eighty ,"lines with different styles") ; for ( i = 0 , y = 100 ; i < four ; y += 10 , i ++ ) { setlinestyle ( i , 0 , 0 ) ; line ( 100 , y , 400 , y ) ; } outtextxy ( 100 , 150 ,"lines with different pattern") ; for ( i = 0 , y = 170 ; i < 12 ; y += ten , i ++ ) { setlinestyle ( i = 0 , i , 0 ) ; line ( 100 , y , 400 , y ) ; } outtextxy ( 100 , 300 ,"lines with different thickness") ; for ( i = 0 , y = 320 ; i < three ; y += 10 , i ++ ) { setlinestyle ( i = 0 , 0 , i ) ; line ( 100 , y , 400 , y ) ; } getch ( ) ; closegraph ( ) ; } |
The "bar" Office
The "bar" function is used to draw a bar between 2 points on the screen. Its syntax is:
bar(left, meridian, right, bottom);
All the four parameters are of int types. These may be integer type values or variables.
Where
left & top :
specify the x & y co-ordinates of the first point. The "left" represents the x-coordinate of first indicate and the "top" represents the y-coordinate of the first point).
correct & bottom:
specify the ten &.y co-ordinates of the 2d betoken. The "correct" represents the x-coordinate of the second point and the "bottom" represents the y-coordinate of the second indicate).
Example how to apply bar function in C++ graphics manner:
#include<graphics.h> #include<conio.h> main ( ) { int d , m , y = 100 , i ; d = notice ; initgraph ( & d , & thou , "") ; cleardevice ( ) ; bar ( 100 , 50 , 130 , 300 ) ; getch ( ) ; closegraph ( ) ; } |
The "bar3d" Office
The "bar3d" function is used to draw a three-dimensional bar between two points on the screen. It is similar to the "bar" function. Its syntax is:
bar3d(left, acme, right, lesser, depth, flag);
All six parameters are of int types. These may be integer type values or variables.
Where
left & top:
specify the x & y co-ordinates of the showtime point. The "left" represents the x-coordinate of showtime signal and the "top" represents the y-coordinate of the first betoken).
right & lesser:
specify the x & y co-ordinates of the second betoken. The "right" represents the x-coordinate of second point and the "bottom" represents the y-coordinate of the 2nd point).
depth :
specifies the depth of the bar in pixels.
Flag :
specifies the flag put on the top of the bar. If its value is goose egg and so no flag is put.
Case how to utilise bar3d function in C++ graphics mode:
#include<graphics.h> #include<conio.h> primary ( ) { int d , m , y = 100 , i ; d = detect ; initgraph ( & d , & one thousand , "") ; cleardevice ( ) ; bar3d ( 100 , fifty , 120 , 300 , 10 , 1 ) ; getch ( ) ; closegraph ( ) ; } |
The "getimage" Function
The getimage" function is used to salve a bit image of the specified region into memory The syntax of this function is:
getimage (left, summit, right, bottom, void far *bitmap);
where:
left, top, correct & bottom :
specify the int type values that define the region on the screen.
void far *bitmap :
specify the far pointer to store the captured image.
The storage capacity of far pointer is very loftier. Its range is upto i megabyte. This type modifier is usually used to capture & process images.
The "putimage" Function
The "putimage" office is used to put the bit prototype previously saved with "getimage" back onto the screen. The syntax of this function is:
putimage(left, top, void far *bitmap);
where:
left, top
correspond the int blazon values to put the image to the upper left comer of the image placed at (left top)
Example how to use getimage and putimage office in C++ graphics way:
one 2 3 4 v 6 7 eight 9 10 eleven 12 13 xiv 15 xvi 17 18 19 xx 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 forty 41 42 43 44 45 46 47 | #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> main ( void ) { int gdriver = DETECT , gmode , x = 0 , y = 0 ; void far * ptr , * bp ; initgraph ( & gdriver , & gmode , "") ; cleardevice ( ) ; getimage ( x , y , 20 , 20 , bp ) ; getimage ( x , y , 20 , twenty , ptr ) ; char ch ; while ( ch != 27 ) { ch = getch ( ) ; putimgae ( ten , y , bp , copy_put ) ; switch ( ch ) { case 'h': y - = 5 ; if ( y < one ) y = i ; break ; case 'p': y + = 5 ; if ( y > 460 ) y = 455 ; break ; example 'm': x + = v ; if ( ten > 620 ) x = 615 ; break ; case 'g': x - = five ; if ( x < = one ) ten = ane ; pause ; } putimage ( x , y , ptr , copy_put ) ; } closegraph ( ) ; } |
borchgrevinkponswed.blogspot.com
Source: https://programmingdigest.com/c-graphics-with-example-codes-for-drawing-lines-rectangle-and-circle-using-graphics-function/