Draw a Circle in C++ Using Graphics
C++ GRAPHICS Introduction:
C++ GRAPHICS Functions relating to graphics are used to create different shapes in unlike colors. The graphics functions require a graphics monitor (nowadays near all computers accept graphics monitors) and a graphics carte such as VGA, SVGA or EGA. Color monitor is recommended for viewing graphics in colors.
The graphics include:
- Lines in different colors and styles.
- Different shapes like circles, rectangles in different styles.
- Text in dissimilar 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 computer monitor into graphics mode. These files have BGI extension.
- Graphic symbol font style files having extension "chr". These files are used to process text or characters in graphics mode.
Display Manner:
The output of a program can be displayed on the screen in ii modes. These modes are:
- Text Mode
- Graphics Manner
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 lesser-most row is 24. Similarly. the leftmost column is 0 and the rightmost column is 79. In-text style, simply 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 way. In this mode, the output is displayed on the figurer screen in points or pixels.
In graphics mode, the screen is divided into pocket-sized 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 divide 640×480 pixels. The number of dots per inch is called resolution screen. The dots are very close to each other. The more the number pixels, the more clearer the graphics is.
The monitor types (display adapter) and their respective resolution are given below.
| Monitor type | Resolution | Colour |
| CGA VGA SVGA | 640×200 640×480 800×600 | 16 sixteen 256 |
Initializing C++ Graphics Mode:
The calculator display system must be initialized into graphics mode before calling the graphics role.
The "initgraph" function is used to initialize the brandish into graphics manner. This function is a part of the "graphics.h" header file. And then this file is included in the program before executing "initgraph" function.
The syntax of initgraph" office is:
initgraph(&driver, &mode, "path");
Where:
Commuter:
Represents the graphics driver installed on the reckoner. It may be an integer variable or an integer constant identifier, due east.chiliad. CGA, EGA, SVGA, etc.
The-graphics driver tin can too be automatically detected by using the keyword "Notice". Letting compiler detect the graphic commuter is known as motorcar-discover.
If the commuter is to be automatically detected, the variable commuter is alleged as of integer type and DETECT value is assigned to it every bit shown below.
int driver, mode;
commuter = DETECT;
This statement must be placed before "initgraph" role. When the above statement is executed. he figurer automatically detects the graphic driver and the graphics mode.
Mode
Represents output resolution on the computer screen. The normal mode for VGA is VGAHI. It gives the highest resolution
If the driver is on motorcar-detected, then its utilize is optional. The computer automatically detects the driver as well as the manner.
&
represents the addresses of abiding numerical identifiers of driver and fashion. If constants (e.g., VGA, VGAHI) are used, then "&" operator is not used every bit shown beneath:
initgraph (VGA, VGAHI, "path");
path
Represents the path of graphic drivers. Information technology is the directory on the dish where the BGI files are located. Suppose the BGI files are stored in "C:\TC\BGI", then the complete path is written equally:
initgraph (VGA, VGAHI, "C:\TC\\BGI");
Use of double backslash "\" is to be noted. 1 backslash is used as escape grapheme and other for the directory path. If the BGI files are in the electric current directory, then the path is written every bit:
initgraph (VGA, VGAHI, " ");
Note:
The variables for driver and mode must be of int type only their names may exist different.
Example how to use initgraph computer display into C++ graphics fashion:
| #include<graphics.h> principal ( ) { int d , 1000 ; d = Observe ; Initgraph ( & d , & chiliad , "c : \ \ tc") ; } |
In the above example, the BGI files must be in the specified directory, 1.east., in "c:\tclbgi". If the BGI files are in the directory path"c:\tc" then the higher up argument is written as:
initgraph(&d, &m, "C:\\TC");
The "cleardevice" Function:
The "cleardevice" function is used to clear the screen in graphics fashion. Information technology is like to "clrscr" function that is used to articulate the screen text mode. Its syntax is:
cleardevice();
Closing Graphics Mode
The "closegraph" part is used to restore the screen to the text mode.
When graphics style is initialized, retention is allocated to the graphics organization. When "closegraph" office is executed, it de-allocates all retention allocated to the graphics arrangement. This office is usually used a the end of the program. Its syntax is:
closegraph();
Text in Graphics Mode
In graphics node: text can besides exist written in different fonts, styles. sizes, colors, and directions. The graphic functions ordinarily used to create and print text are described below.
The "outtext" Function
The "outtext" function is used to impress text on the estimator screen in graphics mode. The text is printed at the electric current cursor position on the screen. Its syntax is:
outtext(cord);
Where:
cord
Represents the characters that are to be printed on the screen. It may exist a string variable or cord abiding. The string constant is enclosed in double-quotes.
Example how to use cleardevice, closegraph and outtext function and impress Programming digest into C++ graphics mode:
| #include<graphics.h> #include<conio.h> main ( ) { int d , yard ; d = DETECT ; initgraph ( & d , & m , "") ; cleardevice ( ) ; outtext ("Programming digest") ; 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 similar to "gotoxy" function used in text fashion. Its syntax is:
moveto (ten, y);
Where:
x
Represents the ten-coordinate of the screen. It is the horizontal altitude in pixels from the left of the screen. It may exist 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 superlative of the screen. Information technology may be an unsigned int type value or variable. For VGA, its value is from 0 to 479.
Instance how to use moveto role and print Programming assimilate into C++ graphics mode:
| #include<graphics.h> #include<conio.h> master ( ) { int d , m ; d = Detect ; initgraph ( & d , & thou , "") ; cleardevice ( ) ; moveto ( 400 , 200 ) ; outtext ("Programming assimilate") ; getch ( ) ; closegraph ( ) ; } |
The "outtextxy" Office
The "outtextxy" function is similar to the outtext" function but it is used to print text on the screen at a specified location. This part serves the purpose of both the "moveto" and "outtext" functions. Its syntax is:
outtextxy (x, y, string);
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 unsigned int type value or variable. For VGA, its value is from 0 to 639.
Y
represents the y-coordinate of the screen. It is the vertical distance in pixels from the top of the screen. Information technology may be unsigned int blazon value or variable. For VGA, its value is from 0 to 479.
Cord
represents the cord of characters that is to exist printed on the computer screen. It may be a string variable or a string. constant. The cord constant is enclosed in double quotes.
Example how to utilise outtextxy function and impress Programming digest into C++ graphics fashion:
| #include<graphics.h> #include<conio.h> main ( ) { int d , one thousand ; d = Detect ; initgraph ( & d , & yard , "") ; cleardevice ( ) ; outtextxy ( 100 , 200 , "Programming digest") ; getch ( ) ; closegraph ( ) ; } |
The "settextstyle" Function
The "settextstyle" function is used to define the text fashion in graphics mode. The text mode includes the font blazon, font size and text direction. The syntax of this function is given as: settextstyle (way, dir, size);
All the three parameters are of int blazon. These may be int type values or variables.
Where:
Mode:
specifies the font fashion. Its value range is from 0 to 10.
Dir:
specifies the management of the text in which it is to be displayed. Its value is from 0 to 1. It may be a numerical constant identifier. It is HORIZ DIR (for horizontal direction) or VERT_DIR (for vertical direction).
Size:
specifies the font size of the text. Its value is from 0 to 72.
Example how to apply settextstyle office and print Programming digest into C++ graphics style:
| #include<graphics.h> #include<conio.h> primary ( ) { int d , thousand , c ; d = DETECT ; initgraph ( & d , & thou , "") ; cleardevice ( ) ; for ( c = 0 ; c <= x ; c ++ ) { setextstyle ( c , 0 , 0 ) ; outtextxy ( 100 , xx + c* 20 , "Programming digest") } getch ( ) ; closegraph ( ) ; } |
The "setcolor" Part
The setcolor" function is used to define color of the objects and the text in graphics mode. Its syntax is: setcolor (co);
where:
со
Represents the color. It may be an int type value or variable. For VGA, its value is from 0 to xv. It may also be a numerical abiding identifier, eastward.g. BLUE, GREEN, RED etc.
The "setbkcolor" Function
The "setbkcolor" part is used in graphics manner to define the groundwork colour 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 15. Information technology may too be numerical constant identifier eg blue, dark-green and red etc.
Example how to utilise setcolor and setbkcolor office and print Programming assimilate into C++ graphics style:
| 1 two 3 4 five half-dozen 7 eight 9 ten 11 12 13 fourteen 15 16 17 eighteen xix | #include<graphics.h> #include<conio.h> main ( ) { int d , m , co ; d = Discover ; initgraph ( & d , & m , "") ; cleardevice ( ) ; for ( co = 0 ; co <= 15 ; co ++ ) { setbkcolor ( co ) ; setcolor ( co + 1 ) ; settextstyle ( 0 , 0 , 2 ) ; outtextxy ( 100 , x + co* 20 , "Programming digest") ; outtextxy ( 200 , 200 ,"press whatever primal….") ; getch ( ) ; } closegraph ( ) ; } |
Creating Objects in C++ Graphics Style
Different objects, e.grand. lines, circles, rectangles and many other shapes are created in graphics fashion using various congenital-in functions. Post-obit are the functions that are ordinarily used to create graphics objects:
The "circumvolve" Function
The "circle" function is used to draw a circle on the screen. Its syntax is:
circle(x, y, radius);
All the three parameters are of int type. These may be int type values or variables.
Where
x & y
Specifies the center point of the circle. These are the 10- coordinate and y-coordinate of the center of the circle on the screen.
Radius
Specifies the radius of the circle.
Example how to make a circle using circle function in C++ graphics mode:
| #include<graphics.h> #include<conio.h> main ( ) { int d , m , r , c ; d = DETECT ; initgraph ( & d , & thou , "") ; cleardevice ( ) ; for ( c = 1 ; 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 heart point of the circumvolve. These are the x- coordinate and y-coordinate of the center of the arc on the screen.
Stangle:
specifies the starting angle of the arc in degree.
Endangle:
specifies the ending bending of the arc in degree.
radius:
specifies the radius of the arc.
Note:
The arc function can also be used to draw a circle by giving the starting angle 0 and ending angle 360. Similarly, it can also be used to describe line past giving the aforementioned values for starting and ending angles.
Example how to make arc in c++ using arc function in graphics fashion:
| 1 2 3 four v half-dozen 7 8 9 10 11 12 13 14 15 16 17 | #include<graphics.h> #include<conio.h> master ( ) { int d , m , c ; d = DETECT ; initgraph ( & d , & m , "") ; cleardevice ( ) ; for ( c = 1 ; c <= 15 ; c ++ ) { setcolor ( c ) ; arc ( 300 , 200 , 45 , 145 , c* ten ) ; } getch ( ) ; closegraph ( ) ; } |
The "line" Function
The "line" office is used to draw a line betwixt 2 points on the screen. Its syntax is:
line (x1, y1, x2, y2);
All the iv parameters are of int type. These may be integer type values or variables.
Where:
x1 & y1
specify the x & y co-ordinates of first point.
x2 & y2
specify the x & y co-ordinates of second point.
For instance, to draw a line between left upper corner & right bottom corner of the screen for VGA monitor, the statement is written every bit:
line(0, 0, 639, 479);
or
line (639, 479,0, 0);
The point (0, 0) represents the upper left corner of the screen and bespeak (639, 479) represents the lesser right corner of the screen.
Similarly, to describe a line between bottom left corner & upper right 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 lesser left comer of the screen and indicate (639, 0) represents the upper right corner of the screen.
Example how to make lines in c++ using line function in graphic mode:
| 1 2 3 4 5 6 seven 8 9 10 11 12 13 fourteen fifteen 16 17 18 19 | #include<graphics.h> #include<conio.h> chief ( ) { int d , m , c , x1 ; d = DETECT ; initgraph ( & d , & m , "") ; cleardevice ( ) ; for ( c = one , x1 = 1 ; x1 <= 640 ; c ++ , x1 += 5 ) { setcolor ( c ) ; line ( 320 , 240 , x1 , 0 ) ; if ( c >= xv ) c = 1 ; } getch ( ) ; closegraph ( ) ; } |
The "rectangle" Role
The "rectangle function is used to depict a rectangle between two points on the screen. Its syntax is:
rectangle (x1, y1, x2, y2);
All four parameters are of int types. These may be integer type values or variables.
where
x1 & y1:
specify the x & y co-ordinates of the beginning signal.
x2 & y2:
specify the 10 & y co-ordinates of the 2d | oint.
For example, to draw a rectangle between upper left comer & lesser right corner of the screen (for VGA monitor), the statement 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 signal (639, 479) represents the bottom right comer of the screen.
Instance how to make a rectangle in c++ using the rectangle function in graphics manner:
| #include<graphics.h> #include<conio.h> chief ( ) { int d , chiliad ; d = notice ; initgraph ( & d , & g , "") ; cleardevice ( ) ; rectangle ( 100 , 300 , 500 , 200 ) ; getch ( ) ; closegraph ( ) ; } |
The "setlinestyle" Function
The "setlinestyle" part is used to set the line mode of dissimilar objects that are drawn. The lines of these objects tin exist drawn in dissimilar styles, patterns and thickness. These attributes of the line are defined through this function. Its syntax is:
setlinestyle (manner, blueprint, thickness);
Information technology has three parameters all of int types. These may be int blazon values or variables.
Where
style:
specifies the line way. Its value may be from 0 to 4.
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 three.
Example how to make stylish lines in c++ using setlinestyle part in graphic mode:
| i ii iii four 5 vi vii 8 nine 10 11 12 thirteen 14 15 xvi 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include<graphics.h> #include<conio.h> principal ( ) { int d , m , y = 100 , i ; d = DETECT ; initgraph ( & d , & chiliad , "") ; cleardevice ( ) ; outtextxy ( 100 , 80 ,"lines with different styles") ; for ( i = 0 , y = 100 ; i < 4 ; y += 10 , i ++ ) { setlinestyle ( i , 0 , 0 ) ; line ( 100 , y , 400 , y ) ; } outtextxy ( 100 , 150 ,"lines with unlike pattern") ; for ( i = 0 , y = 170 ; i < 12 ; y += 10 , i ++ ) { setlinestyle ( i = 0 , i , 0 ) ; line ( 100 , y , 400 , y ) ; } outtextxy ( 100 , 300 ,"lines with unlike thickness") ; for ( i = 0 , y = 320 ; i < 3 ; y += x , i ++ ) { setlinestyle ( i = 0 , 0 , i ) ; line ( 100 , y , 400 , y ) ; } getch ( ) ; closegraph ( ) ; } |
The "bar" Office
The "bar" function is used to depict a bar between two points on the screen. Its syntax is:
bar(left, top, right, bottom);
All the four parameters are of int types. These may exist integer type values or variables.
Where
left & peak :
specify the x & y co-ordinates of the beginning point. The "left" represents the x-coordinate of first signal and the "superlative" represents the y-coordinate of the showtime point).
correct & lesser:
specify the x &.y co-ordinates of the second indicate. The "right" represents the 10-coordinate of the 2nd point and the "bottom" represents the y-coordinate of the second point).
Example how to use bar function in C++ graphics mode:
| #include<graphics.h> #include<conio.h> main ( ) { int d , m , y = 100 , i ; d = detect ; initgraph ( & d , & m , "") ; cleardevice ( ) ; bar ( 100 , fifty , 130 , 300 ) ; getch ( ) ; closegraph ( ) ; } |
The "bar3d" Function
The "bar3d" function is used to describe a three-dimensional bar between 2 points on the screen. It is like to the "bar" function. Its syntax is:
bar3d(left, top, correct, bottom, depth, flag);
All vi parameters are of int types. These may exist integer type values or variables.
Where
left & summit:
specify the x & y co-ordinates of the offset point. The "left" represents the x-coordinate of kickoff point and the "top" represents the y-coordinate of the beginning point).
correct & bottom:
specify the x & y co-ordinates of the second indicate. The "correct" represents the x-coordinate of 2nd point and the "bottom" represents the y-coordinate of the second point).
depth :
specifies the depth of the bar in pixels.
Flag :
specifies the flag put on the superlative of the bar. If its value is zero and then no flag is put.
Example how to use bar3d part in C++ graphics style:
| #include<graphics.h> #include<conio.h> main ( ) { int d , m , y = 100 , i ; d = observe ; initgraph ( & d , & m , "") ; cleardevice ( ) ; bar3d ( 100 , l , 120 , 300 , 10 , 1 ) ; getch ( ) ; closegraph ( ) ; } |
The "getimage" Office
The getimage" function is used to save a bit image of the specified region into memory The syntax of this part is:
getimage (left, elevation, right, bottom, void far *bitmap);
where:
left, top, right & bottom :
specify the int type values that ascertain 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 high. Its range is upto 1 megabyte. This type modifier is ordinarily used to capture & process images.
The "putimage" Function
The "putimage" office is used to put the bit paradigm previously saved with "getimage" back onto the screen. The syntax of this role is:
putimage(left, summit, void far *bitmap);
where:
left, top
represent the int type values to put the prototype to the upper left comer of the epitome placed at (left top)
Instance how to use getimage and putimage function in C++ graphics manner:
| i 2 three 4 5 six 7 8 nine 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 , ten = 0 , y = 0 ; void far * ptr , * bp ; initgraph ( & gdriver , & gmode , "") ; cleardevice ( ) ; getimage ( x , y , 20 , twenty , bp ) ; getimage ( x , y , 20 , xx , ptr ) ; char ch ; while ( ch != 27 ) { ch = getch ( ) ; putimgae ( ten , y , bp , copy_put ) ; switch ( ch ) { case 'h': y - = v ; if ( y < 1 ) y = 1 ; suspension ; case 'p': y + = five ; if ( y > 460 ) y = 455 ; break ; example 'm': x + = 5 ; if ( 10 > 620 ) x = 615 ; break ; case 'k': 10 - = 5 ; if ( x < = ane ) x = i ; intermission ; } putimage ( x , y , ptr , copy_put ) ; } closegraph ( ) ; } |
Source: https://programmingdigest.com/c-graphics-with-example-codes-for-drawing-lines-rectangle-and-circle-using-graphics-function/
0 Response to "Draw a Circle in C++ Using Graphics"
Post a Comment