In the previous lessons, I introduced the first routine of CChart and drew a simple line chart. Some of you may have to worry about it. In the past, CChart would have been easier. Nothing special. I can use MoveTo and LineTo to draw on the screen myself.
These students underestimated the CChart function.
Although CChart is clumsy, it has been a great feature for ten years. Stupid, I feel that CChart is no worse than MsChart, TeeChart, ChartDirector and other large companies in terms of functions. Of course, there is still a gap between calling interfaces and some image details processing systems and big company products.
This is also impossible. As a non-professional amateur programmer, being stupid, developing these 100,000 lines of code alone cannot be compared with the company's professionals.
Today, we will bring you the first advanced feature of CChart-contour map. The reason why the contour feature was stupid was originally developed for the CChart library.
To draw a contour map, you must first consider the functions of the contour map. In practical applications, the functions of a contour map may be of various types, such as measurement data and program simulation calculation results. In the end, it can be expressed as z = f (x, y), where x and y are plane coordinates, and z is the height of the contour line.
For example, we will plot a contour line for a relatively simple function. Where f (x, y) = 1.0/(x-1.0) * (x-1.0) + y * y + 1.0 ). As you can imagine, this function has a maximum value at the coordinate point (1.0, 0.0. Let's draw the contour lines and see if the results are correct.
First, follow the method in Lesson 1 to create a VC6 project named Lesson04.
Follow the procedure of the first lesson to complete Step 9. The response routine of the WM_CREATE message in Step 7 is not filled at the moment, as shown below.
case WM_CREATE:break;
Then, follow the method in Lesson 3 to allow our program to respond to double-click the mouse.
wcex.style= CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
Create a contour function. In front of the WndProc function, enter the following code.
double f(double x, double y){return 1.0/((x-1.0)*(x-1.0)+y*y+1.0);}
Modify the response routine of the WM_CREATE message
case WM_CREATE:chartWnd.Attach(hWnd, kTypeContourLine);chartWnd.GetChart()->SetFieldFcn(f);{double xRange[2], yRange[2];xRange[0] = -2.0;xRange[1] = 2.0;yRange[0] = -2.0;yRange[1] = 2.0;chartWnd.GetChart()->SetPlotRange(xRange, yRange);}break;
Note that the type of the contour map is ktype1_line.
Compile and run the program to get the result.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130916135146453? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>
Haha, the contour map is so easy to come out. The figure shows that () is indeed an extreme point.
In the above Code, SetFieldFcn is used to set the contour function, and SetPlotRange is used to set the range of X and Y coordinates of the drawing.
Some people carefully looked at the above figure and found that the line in the figure was not smooth. Can this problem be solved?
Please add a code before break in the response routine of the WM_CREATE message.
chartWnd.GetChart()->SetContourPrecision(8);
Run it again and try again. Is the curve much smoother?
Here, you can set the precision of the contour line with setmediaprecision, which is generally set to 5 or 6. The higher the setting, the longer the delay will be during the first draw.
Some colleagues think that the contour lines are not a bit sparse. Well, please break in the response routine of the WM_CREATE message; add a code before.
chartWnd.GetChart()->SetContourLineNum(50);
Running effect.
650) this. width = 650; "src =" http://img.blog.csdn.net/20130916135236281? Watermark/2/text/plain =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/SouthEast "alt =" SouthEast "/>
There are enough contour lines.
Here, SetContourLineNum is used to set the number of contour height.
So far, students have used CChart to draw contour lines!
Of course, the color of the contour map is a bit monotonous. In subsequent courses, we will introduce another similar view-cloud map, which will be colorful and colorful.
Class passed!