Draw Circle in Code Blocks

C++ Graphics

C++ GRAPHICS Introduction:

C++ GRAPHICS Functions relating to graphics are used to create unlike shapes in unlike colors. The graphics functions require a graphics monitor (nowadays near all computers have graphics monitors) and a graphics bill of fare such every bit VGA, SVGA or EGA. Color monitor is recommended for viewing graphics in colors.

The graphics include:

  • Lines in unlike colors and styles.
  • Dissimilar shapes like circles, rectangles in different styles.
  • Text in different fonts, sizes, and styles.

The following files are needed with the C++ compiler to piece of 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 take BGI extension.
  • Character font style files having extension "chr". These files are used to process text or characters in graphics mode.

Display Mode:

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

  1. Text Style
  2. Graphics Way

C++ Text Mode:

In a text style, the screen is commonly divided into lxxx columns and 25 rows. The topmost row is 0 and the bottom-well-nigh 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 ane graphics object cannot exist displayed.

 C++ Graphics Mode:

Images and other graphic objects are displayed on the SC graphics mode. In this mode, the output is displayed on the computer screen in points or pixels.

In graphics mode, the screen is divided into small-scale dots called For case, 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 than clearer the graphics is.

The monitor types (brandish adapter) and their corresponding resolution are given below.

Monitor type Resolution Colour
CGA

VGA

SVGA

640×200

640×480

800×600

16

16

256

Initializing C++ Graphics Mode:

The computer display system must exist initialized into graphics mode before calling the graphics function.

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

The syntax of initgraph" office is:

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

Where:

Commuter:

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

The-graphics driver tin can as well be automatically detected past using the keyword "DETECT". Letting compiler detect the graphic driver is known as auto-detect.

If the driver is to be automatically detected, the variable commuter is declared as of integer type and Discover value is assigned to it as shown below.

int commuter, mode;

driver = DETECT;

This statement must be placed earlier "initgraph" role. When the above statement is executed. he reckoner automatically detects the graphic driver and the graphics fashion.

Fashion

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

If the driver is on automobile-detected, then its use is optional. The reckoner automatically detects the driver as well every bit the manner.

&

represents the addresses of constant numerical identifiers of driver and mode. If constants (eastward.chiliad., VGA, VGAHI) are used, then "&" operator is not used as 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", then the consummate path is written every bit:

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

Use of double backslash "\" is to be noted. One backslash is used every bit escape character and other for the directory path. If the BGI files are in the current directory, and so the path is written equally:

initgraph (VGA, VGAHI, " ");

Annotation:

The variables for commuter and mode must be of int type but their names may be different.

Case how to use initgraph figurer display into C++ graphics mode:

In the in a higher place instance, the BGI files must be in the specified directory, ane.e., in "c:\tclbgi". If the BGI files are in the directory path"c:\tc" and so the above statement is written as:

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

The "cleardevice" Function:

The "cleardevice" role is used to articulate the screen in graphics mode. It is similar to "clrscr" part that is used to clear the screen text way. Its syntax is:

cleardevice();

Endmost Graphics Way

The "closegraph" function is used to restore the screen to the text way.

When graphics mode is initialized, memory is allocated to the graphics organisation. When "closegraph" function is executed, it de-allocates all memory allocated to the graphics organization. This function is usually used a the end of the programme. Its syntax is:

closegraph();

Text in Graphics Mode

In graphics node: text tin also be written in different fonts, styles. sizes, colors, and directions. The graphic functions commonly used to create and print text are described below.

 The "outtext" Part

The "outtext" function is used to print text on the computer screen in graphics mode. The text is printed at the current cursor position on the screen. Its syntax is:

outtext(string);

Where:

cord

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.

Instance how to use cleardevice, closegraph and outtext function and print Programming digest into C++ graphics mode:

The "moveto" Part

The "moveto" part is used to movement the current cursor position to a specified location on the screen where the output is to exist printed. It is similar to "gotoxy" role used in text mode. Its syntax is:

moveto (x, y);

Where:

x

Represents the 10-coordinate of the screen. It is the horizontal distance in pixels from the left of the screen. It may be an unsigned int type 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 distance in pixels from the height of the screen. It may exist an unsigned int type value or variable. For VGA, its value is from 0 to 479.

Example how to use moveto part and print Programming digest into C++ graphics fashion:

The "outtextxy" Part

The "outtextxy" office is like to the outtext" role only it is used to impress 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, cord);

 where:

ten

represents the 10-coordinate of the screen. It is the horizontal distance in pixels from the left of the screen. Information technology may be unsigned int blazon value or variable. For VGA, its value is from 0 to 639.

Y

represents the y-coordinate of the screen. It 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.

String

represents the string of characters that is to be printed on the reckoner screen. Information technology may exist a string variable or a string. abiding. The string constant is enclosed in double quotes.

Instance how to utilize outtextxy function and print Programming digest into C++ graphics mode:

The "settextstyle" Function

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

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

Where:

Style:

specifies the font style. Its value range is from 0 to 10.

Dir:

specifies the direction of the text in which it is to exist displayed. Its value is from 0 to ane. It may be a numerical constant identifier. It is HORIZ DIR (for horizontal direction) or VERT_DIR (for vertical management).

 Size:

specifies the font size of the text. Its value is from 0 to 72.

Example how to use settextstyle role 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 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 15. It may also be a numerical constant identifier, eastward.g. BLUE, Light-green, RED etc.

The "setbkcolor" Function

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

Where:

co

Specifies the colour. It may be an it blazon value or variable. For VGA. Its value is from 0 to 15. It may also be numerical constant identifier eg blue, green and carmine etc.

Case how to utilise setcolor and setbkcolor function and print Programming digest into C++ graphics style:

Creating Objects in C++ Graphics Mode

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

The "circle" Role

The "circle" function is used to describe a circle on the screen. Its syntax is:

circle(ten, y, radius);

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

Where

 x & y

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

Radius

Specifies the radius of the circle.

Example how to make a circle using circle role in C++ graphics style:

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 (10, y, stangle, endangle, radius);

All the five parameters are of int types. These may be constants or variables.

Where:

 ten & y

specify the heart point of the circle. 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 catastrophe angle of the arc in degree.

 radius:

specifies the radius of the arc.

Annotation:

The arc function can likewise be used to draw a circle past giving the starting angle 0 and ending angle 360. Similarly, information technology can also be used to draw line by giving the same values for starting and ending angles.

Example how to make  arc in c++ using arc part in graphics mode:

The "line" Function

The "line" function is used to depict a line between two 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 indicate.

x2 & y2

specify the ten & 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 equally:

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 bottom right corner of the screen.

Similarly, to draw 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 indicate (0,479) represents the bottom 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 role in graphic mode:

The "rectangle" Function

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

 rectangle (x1, y1, x2, y2);

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

where

x1 & y1:

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

x2 & y2:

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

 For example, to describe a rectangle betwixt upper left comer & bottom correct 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 indicate (639, 479) represents the lesser right comer of the screen.

Example how to make a rectangle in c++ using the rectangle function in graphics style:

The "setlinestyle" Part

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

 setlinestyle (style, blueprint, thickness);

It has 3 parameters all of int types. These may exist int type values or variables.

Where

mode:

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 exist from 0 to 3.

Example how to make stylish lines in c++  using setlinestyle function in graphic mode:

The "bar" Function

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

bar(left, top, right, bottom);

All the iv 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 commencement bespeak and the "superlative" represents the y-coordinate of the showtime indicate).

 correct & bottom:

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

Example how to utilise bar function in C++ graphics mode:

The "bar3d" Function

The "bar3d" function is used to draw a 3-dimensional bar between two points on the screen. It is similar to the "bar" role. Its syntax is:

bar3d(left, superlative, right, bottom, depth, flag);

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

Where

 left & pinnacle:

specify the x & y co-ordinates of the outset betoken. The "left" represents the x-coordinate of first betoken and the "top" represents the y-coordinate of the get-go point).

right & bottom:

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

depth :

specifies the depth of the bar in pixels.

Flag :

specifies the flag put on the peak of the bar. If its value is goose egg so no flag is put.

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

The "getimage" Office

The getimage" part is used to save a flake paradigm of the specified region into memory The syntax of this function is:

getimage (left, top, correct, lesser, void far *bitmap);

where:

 left, summit, 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 epitome.

The storage chapters 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" function 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, elevation

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

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

pulliamrabliver.blogspot.com

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

0 Response to "Draw Circle in Code Blocks"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel