The Draw Second Function button’s code is identical; only instead of calling the FunctionEvaIlO function, it calls the Function Eva120 function, which calculates adifferent function
The actual graph of the function is shown in Figure 6.18. The problem with the code so far is that there are gaps between successive points of the function. To “close” these gaps, you can replace the PSet method (which turns on a single pixel) with the Line method to draw a line segment from the last point to the new one. In other words;’drawing the line segments between successive points on the control will close the gaps you see in the plot of Figure 6.18.
If you replace the line that turns on the pixels with the following one:
Picture1.Line -(t, FunctionEval1(t»)· the plot of the same function will looklike Figure-6.15. The solid line is much more suitable for plotting functions. Notice that the coordinates of the Line method are relative to the current, which is the second endpoint of the previous line segment drawn. The previous Line method draws a line segment that joins two adjacent points on the plot. In the application’s code, the lines that USe the !’Set method to turn on isolated pixels are commented out. . The Graph application is the core of a data plotting application. You can supply your own functions and add features such as axis-numbering, major and minor ticks along the axes, legends, andso on. You can also add color, graph multiple functions at once, and in general, use the Graph application as a starting point fo a custom data plotting application. The code behind the Draw Both Functions bus ton, for instance, plots both functions, each in a different color. The user-defined coordinate system is based on the values of the first function only. The drawback of the Graph application is that the functions to be plotted must be hard-coded, which makes the application less flexible. In Chapter 21, you’ll learn how to evaluate any math function supplied by the user at runtime, with the Script control (a new control introduced with Visual Basic 6).
VB6 at Work: The Spiral Project
The Spiral application demonstrates how to draw complicated, mathematically defined curves with Visual Basic’s methods. it is a computer rendition of an old toy that literally made drawing curves a child’s game. The actual toy consists of . two plastic circles dented along their circumference. The large circle remains fixed. on a piece of paper while the smaller one slides around the larger one wit! the help of a pen. The pen.is inserted in a small hole somewhere off the center of t1′ smaller circle-The dents around the circumference of the two circles help the us keep them in contact as the outer circle slides around the inner one. As the oute .circle moves ar~und the inner one, the pen leaves intricate patterns on the’ pape If you find this description complicated, open the Spiral application and draw few curves. The curves shown in Figures-6.19 and 6.20 are the traces left by a fixed on a small circle that rotates around a bigger circle. The operation of the p gram is really easy to understand if you spend a moment with it and watch the curves being drawn. The curves drawn with this toy can be described mathematically. The follow’ equations describe the curve where the diameters of the two circles are Rl and and the pen is fixed at a point that’s r units frem the smaller circle’s center.
The t variable is the angle of rotation and it increases with time. Each complete rotation of the small circle around the large one takes 360 degrees (or 2·pi radians). One complete rotation of the outer circle, however, doesn’t complete the curve. The smaller circle must rotate many times around the inner circle to produce the entire curve. The exact number of rotations depends on the ratio of the radii of the two circles
The Spiral application uses a quick-and-dirty algorithm to figure out how many rotations are required to complete a curve. If the ratio of the two radii is an integer, the algorithm works. If the ratio is not an integer, the algorithm may not recognize the end of the curve in time and will continue drawing over the existing curve. In this case, you can always click the Stop button to stop the drawing. To plot the actual curve, you can turn on the pixel at (X, Y), or you can draw a line segment from the previous point to the new one. The complete code for drawing the spiral curves of Figures 6.19 and 6.20 is shown next
The DrawRoulette() Subroution
The program consists of two nested loops. The inner loop draws the trace of a complete rotation of the small circle around the large one. The outer loop determines how many times the small circle must rotate around the large one to produce a closed curve.
Experiment with the Spiral application. Try various combinations of the inner and outer radii by adjusting their values with the corresponding scroll bar. You will notice that the inner radius can assume negative values too. The negative isues correspond to a small circle that slides inside the larger one. The last scroll bar controis the position of the pen in the small circle. It’s the distance of the pen from the center of the smaller circle. For points on the circumference of the smaller circle, set the last Scroll bar to O.