Draw Circle Function in C++

C++ Graphics

C++ GRAPHICS Introduction:

C++ GRAPHICS Functions relating to graphics are used to create unlike shapes in different colors. The graphics functions crave a graphics monitor (nowadays almost all computers have graphics monitors) and a graphics card 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 dissimilar styles.
  • Text in unlike 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 programme.
  • Borland Graphics Interface (BGI) files. These files contain graphics commuter programs that initialize the reckoner monitor into graphics mode. These files have BGI extension.
  • Grapheme font style files having extension "chr". These files are used to process text or characters in graphics manner.

Display Mode:

The output of a program can be displayed on the screen in two modes. These modes are:

  1. Text Mode
  2. Graphics Mode

C++ Text Mode:

In a text mode, the screen is normally divided into fourscore columns and 25 rows. The topmost row is 0 and the bottom-most row is 24. Similarly. the leftmost cavalcade is 0 and the rightmost column is 79. In-text mode, only text tin exist 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 reckoner 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 split up 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 than clearer the graphics is.

The monitor types (brandish adapter) and their respective resolution are given beneath.

Monitor type Resolution Color
CGA

VGA

SVGA

640×200

640×480

800×600

sixteen

sixteen

256

Initializing C++ Graphics Mode:

The computer display system must be initialized into graphics mode before calling the graphics office.

The "initgraph" function is used to initialize the brandish into graphics mode. This function is a office of the "graphics.h" header file. Then this file is included in the program before executing "initgraph" function.

The syntax of initgraph" function is:

initgraph(&commuter, &mode, "path");

Where:

Driver:

Represents the graphics driver installed on the computer. It may be an integer variable or an integer constant identifier, e.g. CGA, EGA, SVGA, etc.

The-graphics commuter can also be automatically detected by using the keyword "Notice". Letting compiler find the graphic driver is known every bit motorcar-detect.

If the driver is to exist automatically detected, the variable driver is declared every bit of integer type and DETECT value is assigned to it as shown below.

int driver, mode;

commuter = Notice;

This statement must be placed earlier "initgraph" function. When the above statement is executed. he computer automatically detects the graphic driver and the graphics mode.

Style

Represents output resolution on the computer screen. The normal mode for VGA is VGAHI. It gives the highest resolution

If the driver is on automobile-detected, so its use is optional. The reckoner automatically detects the commuter also as the manner.

&

represents the addresses of abiding numerical identifiers of commuter and mode. If constants (e.g., VGA, VGAHI) are used, so "&" operator is non used as 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 as:

initgraph (VGA, VGAHI, "C:\TC\\BGI");

Use of double backslash "\" is to be noted. Ane 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 mode must exist of int type but their names may exist different.

Example how to use initgraph computer display into C++ graphics manner:

In the above example, the BGI files must be in the specified directory, 1.e., in "c:\tclbgi". If the BGI files are in the directory path"c:\tc" and so the to a higher place argument is written equally:

initgraph(&d, &thousand, "C:\\TC");

The "cleardevice" Function:

The "cleardevice" function is used to clear the screen in graphics mode. Information technology is similar to "clrscr" function that is used to articulate the screen text mode. Its syntax is:

cleardevice();

Closing Graphics Mode

The "closegraph" office is used to restore the screen to the text manner.

When graphics mode is initialized, memory is allocated to the graphics arrangement. When "closegraph" office is executed, information technology de-allocates all memory allocated to the graphics system. This function is usually used a the terminate of the program. Its syntax is:

closegraph();

Text in Graphics Fashion

In graphics node: text tin can also exist written in dissimilar fonts, styles. sizes, colors, and directions. The graphic functions ordinarily used to create and impress text are described below.

 The "outtext" Role

The "outtext" function is used to impress text on the calculator screen in graphics style. The text is printed at the current cursor position on the screen. Its syntax is:

outtext(string);

Where:

string

Represents the characters that are to be printed on the screen. It may be a string variable or string constant. The string constant is enclosed in double-quotes.

Example how to use cleardevice, closegraph and outtext role and impress Programming digest into C++ graphics manner:

The "moveto" Function

The "moveto" office is used to move the current cursor position to a specified location on the screen where the output is to be printed. It is similar to "gotoxy" function used in text mode. Its syntax is:

moveto (x, y);

Where:

10

Represents the ten-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. Information technology is the vertical altitude 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.

Case how to use moveto role and print Programming digest into C++ graphics mode:

The "outtextxy" Office

The "outtextxy" office is similar to the outtext" function but it is used to print text on the screen at a specified location. This role serves the purpose of both the "moveto" and "outtext" functions. Its syntax is:

outtextxy (10, y, string);

 where:

10

represents the x-coordinate of the screen. It is the horizontal distance in pixels from the left of the screen. Information technology 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 altitude 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 string of characters that is to exist printed on the computer screen. It may be a string variable or a string. constant. The string constant is enclosed in double quotes.

Example how to use outtextxy role and print Programming digest into C++ graphics mode:

The "settextstyle" Function

The "settextstyle" office is used to define the text style in graphics mode. The text style includes the font blazon, font size and text direction. The syntax of this function is given equally: settextstyle (way, dir, size);

All the three parameters are of int type. These may be int type values or variables.

Where:

Way:

specifies the font style. 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 ane. Information technology may be a numerical constant identifier. Information technology 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 function and print Programming digest into C++ graphics mode:

The "setcolor" Function

The setcolor" function is used to ascertain color of the objects and the text in graphics way. Its syntax is: setcolor (co);

 where:

со

Represents the colour. It may be an int type value or variable. For VGA, its value is from   0 to xv. It may as well be a numerical abiding identifier, east.g. BLUE, Greenish, Cherry-red etc.

The "setbkcolor" Function

The "setbkcolor" function is used in graphics mode to define the background color of the screen. Its syntax is: setbkcolor(co);

Where:

co

Specifies the color. Information technology may be an information technology type value or variable. For VGA. Its value is from 0 to 15. Information technology may also be numerical abiding identifier eg blue, green and red etc.

Example how to employ setcolor and setbkcolor office and impress Programming digest into C++ graphics fashion:

Creating Objects in C++ Graphics Mode

Different objects, due east.k. lines, circles, rectangles and many other shapes are created in graphics mode using various built-in functions. Following are the functions that are ordinarily used to create graphics objects:

The "circumvolve" Function

The "circumvolve" function is used to draw a circumvolve on the screen. Its syntax is:

circle(x, y, radius);

All the three parameters are of int type. These may exist int type values or variables.

Where

 x & y

Specifies the centre point of the circle. These are the ten- coordinate and y-coordinate of the center of the circle on the screen.

Radius

Specifies the radius of the circle.

Instance how to brand a circle using circle function in C++ graphics way:

The "arc" Function

The arc function is used to draw a round arc starting from a specified angle and up to some other 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:

 ten & y

specify the center point of the circle. These are the ten- coordinate and y-coordinate of the eye of the arc on the screen.

 Stangle:

specifies the starting angle of the arc in degree.

Endangle:

specifies the ending angle of the arc in degree.

 radius:

specifies the radius of the arc.

Note:

The arc office can also be used to depict a circumvolve by giving the starting angle 0 and ending angle 360. Similarly, it can as well exist used to depict line by giving the same values for starting and catastrophe angles.

Case how to make  arc in c++ using arc function in graphics mode:

The "line" Function

The "line" function is used to draw a line betwixt two points on the screen. Its syntax is:

 line (x1, y1, x2, y2);

All the 4 parameters are of int type. These may be integer type values or variables.

Where:

x1 & y1

specify  the ten & y co-ordinates of first signal.

x2 & y2

specify the x & y co-ordinates of second point.

For example, to draw a line between left upper corner & right bottom corner of the screen for VGA monitor, the argument is written as:

line(0, 0, 639, 479);

 or

line (639, 479,0, 0);

The point (0, 0) represents the upper left corner of the screen and point (639, 479) represents the bottom right corner of the screen.

Similarly, to draw a line betwixt bottom 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 correct corner of the screen.

Example how to make lines in c++ using line function in graphic style:

The "rectangle" Function

The "rectangle office 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 exist integer blazon values or variables.

where

x1 & y1:

 specify the 10 & y co-ordinates of the first point.

x2 & y2:

 specify the 10 & y co-ordinates of the second | oint.

 For example, to draw a rectangle between upper left comer & bottom 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 signal (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 make a rectangle in c++ using the rectangle function in graphics mode:

The "setlinestyle" Function

The "setlinestyle" function is used to fix the line style of different objects that are fatigued. The lines of these objects can be fatigued in different styles, patterns and thickness. These attributes of the line are defined through this function. Its syntax is:

 setlinestyle (style, pattern, thickness);

It has three parameters all of int types. These may be int type values or variables.

Where

style:

specifies the line style. 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 3.

Example how to brand fashionable lines in c++  using setlinestyle function in graphic mode:

The "bar" Part

The "bar" function is used to draw a bar between two points on the screen. Its syntax is:

bar(left, tiptop, right, bottom);

All the 4 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 10-coordinate of first signal and the "acme" represents the y-coordinate of the first indicate).

 right & bottom:

specify the x &.y co-ordinates of the 2nd point. The "correct" represents the x-coordinate of the 2nd bespeak and the "lesser" represents the y-coordinate of the second bespeak).

Example how to use bar office in C++ graphics mode:

The "bar3d" Office

The "bar3d" office is used to draw a three-dimensional bar between 2 points on the screen. Information technology is similar to the "bar" role. Its syntax is:

bar3d(left, peak, right, lesser, depth, flag);

All six parameters are of int types. These may exist integer type values or variables.

Where

 left & top:

specify the 10 & y co-ordinates of the offset point. The "left" represents the x-coordinate of first point and the "superlative" represents the y-coordinate of the start point).

right & bottom:

specify the ten & y co-ordinates of the 2d signal. The "correct" represents the x-coordinate of 2d point and the "lesser" represents the y-coordinate of the second 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 nothing then no flag is put.

Case how to use bar3d role in C++ graphics mode:

The "getimage" Part

The getimage" office is used to salvage a bit image of the specified region into retention The syntax of this function is:

getimage (left, top, 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 prototype.

The storage capacity of far pointer is very high. Its range is upto 1 megabyte. This type modifier is usually used to capture & process images.

The "putimage" Function

 The "putimage" part is used to put the bit epitome previously saved with "getimage" back onto the screen. The syntax of this role is:

 putimage(left, superlative, void far *bitmap);

where:

left, top

represent the int type values to put the image to the upper left comer of the image placed at (left top)

Example how to use getimage and putimage function in C++ graphics mode:

kaufmanparliveartle.blogspot.com

Source: https://programmingdigest.com/c-graphics-with-example-codes-for-drawing-lines-rectangle-and-circle-using-graphics-function/

Belum ada Komentar untuk "Draw Circle Function in C++"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel