C programming language graphics // http://lcrystal.spaces.live.com/blog/cns! 13dfc6235bbfb72c! 128. Entry

Source: Internet
Author: User
Tags what header
Basic knowledge of graph functions in TC


I really want to become a knowledgeableProgramPersonnel, but all the knowledge should be learned at 1.1 points. For college students who only write homework programs in Chinese education mode, there is no graphic experience at all. I don't even know what header files I need to use graphics. I checked some information online.ArticleFinally, I found the <> This article introduces a lot of useful basic knowledge.

First, if you want to use a graphical interface in TC, you need to "# include <graphics. h>" and write it at the beginning of the main function:
Void main ()
{
Int gdriver, gmode;
Detectgraph (& gdriver, & gmode)
Initgraph (& gdriver, & gmode, "C: \ turboc2 ");
}
Write closegraph () at the end. If you want to finish the program and it does not end immediately, you should write getch () at the end of the program ();
In addition, you can use the following function to change the color and background color of the graphic line.
Setcolor (INT); setbkcolor (INT); the meaning of the value is:
Black 0 black darkgray 8 dark gray
Blue 1 Blue lightblue 9 Shen LAN
Green 2 green lightgreen 10 light green
Cyan 3 blue lightcyan 11 light blue
Red 4 red lightred 12 light red
Magenta 5 magenta lightmagenta 13 Magenta
Brown 6 brown Yellow 14 yellow
Lightgray 7 light gray white 15 White

Cleardevice (); clear the screen content.
Now we will introduce some graphic functions.
Putpixel (int x, int y, int color); image. X and Y are the horizontal and vertical coordinates of the point. In TC, the screen coordinates are calculated at a resolution of 640*480.

Line (INT x0, int y0, int X1, int Y1); draw a straight line from point (x0, y0) to (x1, Y1.

Circle (int x, int y, int radius); draws a circle with (x, y) as the center and radius as the radius.

ARC (int x, int y, int stangle, int endangle, int radius); with (x, y) as the center, radius as the radius, draw an arc line from stangle to endangle.

Rectangle (INT X1, int Y1, int X2, inty2); uses (x1, Y1) as the upper left corner, and (X2, Y2) as the lower right corner to draw a rectangle.

Drawpoly (INT numpoints, int far * polypoints); draw a number of vertex numbers numpoints, each vertex coordinate is given by polypoints polygon. The polypoints integer array must have at least two times the number of vertices. The coordinates of each vertex are defined as X, Y, and X is first. It is worth noting that when a closed polygon is drawn, the numpoints value is incremented by the number of vertices of the actual polygon, and the coordinates of the first and last points in the array polypoints are the same.

Bar (INT X1, int Y1, int X2, int Y2); determines a rectangular window in the upper left corner (x1, Y1) and in the lower right corner (X2, Y2, then fill in the pattern and color as required.

Bar3d (INT X1, int Y1, int X2, int Y2, int depth, int topflag); When topflag is not 0, draw a three-dimensional cube. When topflag is 0, 3D images are rarely used in this way.

Let's draw a rectangle, for example, below. We can do this by first using setfillstyle (1, 2); then using bar3d (10, 10, 20, 20, 4, 1) to draw a rectangle with the color of 2. (I will explain it again below 1 and 2 .) We can also do this using rectangle (10, 10, 20, 20), but we also need to fill it with floodfill (15, 15, 3. (Assume that the foreground color is 3 ). The two parameters in setfillstyle (INT, INT) are defined as follows:
Setfillstyle (INT pattern, int color );
The pattern settings are as follows:
Empty_fill 0 is filled with background color
Solid_fill 1 to fill
Line_fill 2 is filled in a straight line
Ltslash_fill 3 is filled with diagonal lines (shadow lines)
Slash_fill 4 is filled with a rough slash (thick Shadow Line)
Bkslash_fill 5 is filled with a rough backslash (coarse Shadow Line)
Ltbkslash_fill 6 is filled with a backslash (Shadow Line)
Hatch_fill 7 is filled with a straight Grid
Xhatch_fill 8 is filled with oblique Mesh
Intterleave_fill 9 is filled with intervals
Wide_dot_fill 10 is filled with sparse points
Close_dos_fill 11 is filled with dense points
User_fill 12 is filled with user-defined styles
Color is the border color of a drawing, and Its Numerical meaning is the same as that described above.
Finally, how to output text on the graphic interface, you need to "# include <conio. h>" and then add outtextxy (int x, int y, char * s); s as the string to be output. Sprintf (S, "you want to show the number is % d", num) may be used. Here s should be a string pointer or array, and num is an integer variable.

For more information, see <C Programming Guide>

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.