C language graphics programming (III)-drawing functions ①

Source: Internet
Author: User
Tags getcolor

I. Screen and view setting and clearing functions
36. setviewport () is used to set the graphical view function.
37. clearviewport () clears the graphical viewport Function
38. cleardevice () clear screen function
39. getviewsettings (): Get the function for setting the image's viewport.
Ii. color palette and color Function
40. setpalette () sets the color palette function.
41. setallpalette () sets the entire palette.
42. setbackcolor () sets the background color Function
43. setcolor () sets the color function.
44. getbkcolor () returns the background color function.
45. getcolor () returns the color of the current drawing.
46. getmaxcolor () returns the maximum color value function
47. getpalette () gets the color palette Function
48. getpalettesize () returns the color palette size function.
49. getdefapalpalette () gets the default palette Function
3. Screen position Function
50. getmaxx () returns the maximum X coordinate function
51. getmaxy () returns the maximum y coordinate function
52. getx () returns the X coordinate function
53. Gety () returns the Y coordinate function.
54. moveTo () function
55. moverel () Relative mobile Function
----------------------------------------------------

Borland C ++ provides a complete set of comprehensive graphic functions. The previous section describes the graphic display functions. This section describes the graphic functions, the following section describes its graphic text functions.
In general, you must first clear the screen, set the graphical view, set the drawing color, and then draw a point or line or curve at a certain position on the screen. The functions with these functions are provided below.

I. Screen and view setting and clearing functions

36. setviewport () is used to set the graphical view function.
Function: setviewport () is used to set the current image.
Usage: function call method: void setviewport (INT left, int top, int right, int bottom, int clip );
Note: The left and top parameters are the coordinates in the upper left corner, right, and bottom are the coordinates in the lower right corner. They are all absolute screen coordinates.
The fifth parameter passed to setviewport () is clip. If clip is 1, the output image that exceeds the view is automatically cropped out, that is, all the drawings are limited to the current view, if the clip is 0, no cropping is performed. That is, the plotting will not be expanded beyond the perimeter of the view until the screen boundary. If setviewport () is input as an invalid parameter, the return value of graphresult () function is-11, which is still valid in the past.
The header file of the setviewport () function is graphics. h.
Return Value: None
For example, create a viewport with a diagonal value of (6, 6) and () with the cropping function:
Setviewport (6, 6, 88,88, 1 );

37. clearviewport () clears the graphical viewport Function
Function: The clearviewport () function is used to clear the current image viewport and move the cursor from the current position to the origin (0, 0 ).
Usage: Call the function by void clearviewport (void );
Note: after this function is called and executed, the current graphic view will no longer exist.
The header file corresponding to this function is graphics. h.
Return Value: None
For example, clear the current image view, but not the entire screen:
Clearviewport ();

38. cleardevice () clear screen function
Function: clears the entire screen and moves the current position to the screen origin.
Usage: This function is called by void cleardevice (void );
Note: This function is similar to the clrscr () function in text mode, but the clrscr () function cannot work in graphic mode. Similarly, the cleardevice () function cannot work in text mode.
The corresponding header file of this function is graphics. h.
Return Value: None
For example, clear the entire screen:
Cleardevice ();

39. getviewsettings (): Get the function for setting the image's viewport.
Function: getviewsettings () returns the coordinates of the current image and the cropping flag. The coordinates are absolute screen coordinates, and the cropping flags are 1 or 0.
Usage: This function is called by void getviewsettings (struct viewporttype * info );
Note: After the function is executed, load the information about the current view to the viewporttype structure pointed to by info.
The viewporttype structure is defined as follows:
Struct viewporttype {
Int left, top, right, bottom;
Int clipflag;
}
Here, left and top are used to store the coordinates in the upper left corner, right, and bottom respectively, and clipflag is used to store the cropping flag. If clipflag is set to 1, crop it to avoid exceeding the border of the view, if the value of clipflag is 0, no cropping is performed for output beyond the boundary.
The header file corresponding to the getviewsettings () function is graphics. h.
Return Value: None
For example, the coordinates of the current video corner are displayed:
Struct viewporttype Info;
Getviewsettings (& info );
Printf ("Left: % d top: % d/N", info. Left, info. Top );
Printf ("Right: % d bottom: % d/N", info. Right, info. Bottom );

Ii. color palette and color Function
In addition to the display card and corresponding graphics drivers, you must understand the color palette they support.

40. setpalette () sets the color palette function.
Function: setpalette () can be used to reset the color values of colors in the palette.
Usage: setpalette () function call method is void setpalette (INT value, int color );
Description: The parameter value is the color value, and the color is the color name corresponding to the color value. You can call this function to set a color value.

Table 1-7 pre-defined color palette and colors
-----------------------------------------------------------------
Color Palette color 0 color 1 color 2 color 3
-----------------------------------------------------------------
C0 black light green red light yellow
C1 black light blue pink white
C2 black green red brown
C3 black cyan pink light gray
-----------------------------------------------------------------
The header file corresponding to this function is graphics. h.
Return Value: None

41. setallpalette () sets the entire palette Function
Function: setallpalette () resets the color value for all colors in a color palette.
Usage: The function call method is void setallpalette (struct palettetype * P );
Note: This function is invalid in low-resolution graphic display mode. If the function is called successfully, set the color value in the palettetype structure pointed to by P to the new color value of all colors in the current color palette.
The structure of the palettetype is defined as follows:
Struct palettetype {
Unsigned char size;
Signed Char color [16];
};
Here, you must set the size to the number of colors in the palette, and add each color value to the corresponding elements of the color array before calling the function.
The header file corresponding to this function is graphics. h.
Return Value: None
For example, change the color palette of the 16-color mixer/VGA display card to the first 16 colors:
Struct palettetype;
Int I;
For (I = 0; I <16; I ++) p. Color [I] = I;
P. size = 16;
Setallpalette (& P );

42. setbackcolor () sets the background color Function
Function: setbackcolor () is used to change the first value of the color of the current palette to a specified color value to set the background color.
Usage: Call void setbackcolor (INT color );
Description: The color parameter specifies the background color. Its valid value can be the Color symbol name or the color value. The background colors are listed in Table 1-8.

Table 1-8 background color values
-----------------------------------------------------------------
Color symbol name color value
-----------------------------------------------------------------
Black (black) 0 darkgray (dark gray) 8
Blue (blue) 1 lightblue (light blue) 9
Green (green) 2 lightgreen (light green) 10
Cyan (blue) 3 lightcyan (light blue) 11
Red (red) 4 lightred (light red) 12
Magenta (pinghong) 5 lightmagenta (pink) 13
Brown (brown) 6 Yellow (yellow) 14
Lightgray (light gray) 7 White (white) 15
-----------------------------------------------------------------
By calling this function, the color of the entire screen is changed. If the new background color is the same as the color of the original screen, the image is invisible even though the image information is not lost. If the selected background color is a clear color, the invisible image is displayed again.
The header file of this function is graphics. h.
Return Value: None
For example, set the background color to light blue.
Setbackcolor (lightblue );

43. setcolor () sets the color function.
Function: setcolor () is used to set the color of the current drawing (or the foreground color ).
Usage: Call voids setcolor (INT color );
Description: The color parameter is the selected color of the current drawing. In high-resolution display mode, the selected color is the actual color value, which can also be represented by the Color symbol name. If the color value of the palette has been changed by the cradle function setpalette (), the symbolic name cannot have an expected effect.
In the low-resolution display mode (320x200), the selected color is the color number of the color palette, not the actual color value.
The header file of this function is graphics. h.
Return Value: None
For example, set 16 different colors to display 16 straight lines:
# Include <graphics. h>
Void main ()
{
Int driver, mode;
Int I;
Driver = detect;
Mode = 0;
Initgraph (& driver, & mode ,"");
MoveTo (0,200 );
For (I = 0; I <16; I ++)
{
Setcolor (I );
Linerel (20, 0 );
}
Getch ();
Restorecrtmode ();
}

44. getbkcolor () returns the background color function.
Function: getbackcolor () returns the current background color.
Usage: The function call method is int getbackcolor (void );
Note: after this function is called, the background color is obtained, but its value is not necessarily the color value on the color palette.
The header file corresponding to this function is graphics. h.
Return Value: returns the color value of the current drawing.
For example, the current background color value is returned:
Printf ("background color is % d", getbkcolor ());

45. getcolor () returns the color of the current drawing.
Usage: int getcolor (void) is used to call this function );
NOTE: For the color low-resolution display mode palette, the return value is the color number of the palette, rather than the actual color value. For the high-resolution display mode, the return value corresponds to the color value, unless the color of the palette has been changed by the setpalette () or setallpalette () function.
The corresponding header file of this function is graphics. h.
Return Value: returns the color of the current drawing.
Example: printf ("drawing color is % d", getcolor ());

46. getmaxcolor () returns the maximum color value function
Usage: The function call method is int getmaxcolor (void );
Note: after this function is called, the returned value in low resolution mode is 3 (valid color values in this mode are 0 to 3 ); in high-resolution display mode, the returned value can be 256 or higher (valid color values in this mode are 0 to 256 or higher ).
The corresponding header file of this function is graphics. h.
Return Value: returns the maximum color value in the current graphic mode.
For example, the maximum valid color value is displayed:
# Include <graphics. h>
Void main ()
{
Int driver, mode;
Driver = detect;
Mode = 0;
Initgraph (& driver, & mode ,"");
Printf ("; largest color: % d", getmaxcolor ());
Getch ();
Restorecrtmode ();
}

47. getpalette () gets the color palette Function
Function: Enter the value of the current palette in the palettetype structure.
Usage: getpalette () function call method is void getpalette (struct palettetype * P );
Note: after this function is called and executed, the value of the current color palette is loaded into the structure pointed to by P. You can obtain the settings of this color palette from this structure.
The structure of the palettetype is defined as follows:
# Define maxcolors 15
Struct palettetype {
Unsigned char size;
Signed Char colors [maxcolors + 1];
};
The size is used to store the number of valid colors of the current color palette, and a corresponding color value of the color palette is loaded into each element of the colors array.
Table 1-9 lists these color values (for highlight/VGA)
---------------------------------------------------
Color symbol name color value
---------------------------------------------------
Ega_black (black) 0
Ega_blue (blue) 1
Ega_green (green) 2
Ega_cyan (Cyan) 3
Ega_red (red) 4
Ega_magenta (EGA) 5
Ega_brown (brown) 20
Ega_lightgray (light gray) 7
Ega_darkgray (dark gray) 56
Ega_lightblue (pale blue) 57
Ega_lightgreen (pale green) 58
Ega_lightcyan (pale blue) 59
Ega_lightred (pale red) 60
Ega_lightmagenta (pink) 61
Ega_yellow (yellow) 62
Ega_white (pale white) 63

---------------------------------------------------
The header file corresponding to the getpalette () function is graphics. h.

Return Value: None
For example, display the number of colors supported by the automatic detection display mode:
# Include <graphics. h>
Void main ()
{
Int driver, mode;
Struct palettetype P;
Driver = detect; // Automatic Detection
Mode = 0;
Initgraph (& driver, & mode ,"");
Getpalette (7 P );
Printf ("number of colors palette: % d", p. size );
Getch ();
Restorecrtmode ();
}

48. getpalettesize () returns the color palette size function.
Function: getalettesize () returns the number of colors in the color palette.
Usage: This function is called in int getpalettesize (void );
Note: after calling this function, you will know the number of colors in the color palette that can be set in the current display mode.
The header file corresponding to this function is graphics. h.
Return Value: returns the number of colors supported by the current display mode.
For example, the number of colors supported by the current display mode is displayed.
Int number;
Number = getpalettesize ();
Printf ("palette size: % d", number );

49. getdefapalpalette () gets the default palette Function
Function: getdefapalpalette () returns a pointer to the default palette.
Usage: Call the function in struct palettetype * getdefapalpalette (void );
Note: The structure type of the palette is defined as follows:
# Define maxcolors 15
Struct palettetype {
Unsigned char size;
Signed Char colors [maxcolors + 1];
};
When you call initgraph () to initialize the graphic system, assign a value to the structure type variable of the color palette. As the default color palette, call this function to return a pointer to it.
The corresponding header file of this function is graphics. h.
Return Value: returns a pointer to the default color palette (structure.
For example, the color palette supported by the graphic system after Initialization is displayed.
# Include <graphics. h>
# Include <stdio. h>
# Include <conio. h>
# Include <stdlib. h>
Void main ()
{
Int graphdriver = detect, graphmode;
Struct palettetype * palette;
Int errorcode, color;
Initgraph (& graphdriver, & graphmode ,"");
Errorcode = graphresult ();
If (errorcode! = Grok ){
Printf ("graphics error % s/n", grapherrormsg (errorcode ));
Exit (1 );
}
Palette = getdefapalpalette ();
Printf ("palette-> size % d/N", palette-> size );
Getch ();
For (color = 0; color <palette-> size; color ++)
{
Setfillstyle (line_fill, color );
Bar (20 * (color-1), 0, 20 * color, 20 );
Getch ();
}
If (palette-> size> 1 ){
Do
Setpalette (random (palette-> size), random (palette-> size ));
While (! Kbhit ());
Getch ();
}
Setallpalette (palette );
Closegraph ();
}

3. Screen position Function
In the graphic mode, the screen coordinates of characters are replaced by pixel coordinates, which can be changed from 320x200 to 1280x1024 in the horizontal and vertical directions based on different display hardware. Due to resolution changes, the appropriate graph driver needs to be loaded. Therefore, most graphics programs start by detecting hardware and then use getmaxx () and getmaxy () function to determine the screen size, adjust subsequent operations to adapt to these screen restrictions.

50. getmaxx () returns the maximum X coordinate function
Function: getmaxx () returns the maximum X coordinate in the current graph mode, that is, the maximum horizontal coordinate.
Usage: The function call method is int Maxx (void );
Note: This function is independent of the graphical view settings and only depends on the resolution of the display card's display mode.
The header file corresponding to this function is graphics. h.
Return Value: returns the maximum X coordinate value of the current display mode.
Example: For an example of using this function, see the example of the 51. getmaxy () function.

51. getmaxy () returns the maximum y coordinate function
Function: getmaxy () returns the maximum y coordinate in the current graph mode, that is, the maximum vertical coordinate.
Usage: The function call method is int getmaxy (void );
Note: This function is independent of the graphical view settings and only depends on the resolution of the display card's display mode.
The header file corresponding to this function is graphics. h.
Return Value: returns the maximum y coordinate value of the current display mode.
# Include <graphics. h>
Void main ()
{
Int driver, mode;
Driver = detect;
Mode = 0;
Initgraph (& driver, & mode ,"");
Printf ("max X, Y: % d", getmaxx (), getmaxy ());
Getch ();
Restorecrtmode ();
}

52. getx () returns the X coordinate function
Function: getx () returns the X coordinates (horizontal pixel coordinates) of the current position in the current graph mode ).
Usage: This function is called in int getx (void );
Note: The coordinates here are relative to the current graphic view. If no graphical view is set, the default graphic view is the whole screen. This is similar to using the wherex () function in text mode.
The header file of the getx () function is graphics. h.
Return Value: returns the current X coordinate value.
For example, see the example in the 53. Gety () function.

53. Gety () returns the Y coordinate function.
Function: Gety () returns the Y coordinate (vertical pixel coordinate) of the current position in graph mode ).
Usage: This function is called in int Gety (void );
Note: The coordinates returned by the function are relative to the current graphic view. If no graphical view is set, the default graphic view is the entire screen, which is similar to the wherey () function in text mode.
The header file corresponding to the Gety () function is graphics. h.
Return Value: returns the current y coordinate value.
For example, the X and Y coordinates of the current position are displayed in graphic mode:
Printf ("X: % d, Y: % d", getx (), Gety ());

54. moveTo () function
Function: The moveTo () function moves the current position to the specified coordinate position.
Note: This is equivalent to the operation of the gotoxy () function in text mode. It provides equivalent functions.
The header file corresponding to this function is graphics. h.
Return Value: None
For example, move the current position to the position:
MoveTo (66,88 );

55. moverel () Relative mobile Function
Function: The moverel () function moves the current location to a new location based on the specified offset.
Usage: This function is called by void moverel (INT dx, int Dy );
Note: The DX parameter is the relative moving distance of X and the Dy parameter is the relative moving distance of Y. Relatively mobile applications tend to be more convenient than absolute mobile applications.
The header file corresponding to this function is graphics. h.
Return Value: None
For example, in the following program, when the statement is executed to the current position (90, 90), the execution will be moved to the position (100,120. That is, the X direction is relatively moving 10, and the Y direction is relative moving 30:
Moverel (10, 30 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.