Programming the leaves of all shapes and shapes. Author: Three middle teaching coordinator of Bozhou, Anhui Province, China: 236800 e-mail:wydz511@sohu.com |
Go outdoors, appreciate the beauty of nature, greeted by a variety of different, beautiful trees. This makes people in the relaxed and happy to begin to marvel at the greatness of nature. So there is the impulse to use the computer to draw the beautiful scenery. After some exploration, the program compiled out, drawing the effect is really quite a lot, as shown in Figure 1, the author according to its shape named Fern leaves, reeds, veins, wen Zhu, trees, young vines, grass, grapes, welcome pine, roses, five-star flowers, dandelion, tower, Feather, Phoenix, Peacock, spray, scarecrow and so on. Look closely at the regularity of their shape, that is, self-similarity, a tree divided by a fork is a small tree. In order to illustrate the principle, the author introduces the algorithm of drawing in the case of a very typical small tree in two-dimensional cases. Set this tree to send two forks, as shown in the dark section of Figure 2. Drawing this tree along the direction of the arrow requires the following steps. |
① first from the starting point to the first fork point, set the distance is L1; ② turn left θ1, ③ draw the subtree, ④ right back to θ1, and then turn left θ, this theta angle is equivalent to the curvature of the tree; ⑤ went forward to the second fork point, and went L2; ⑥ to the right θ2; ⑦, ⑧ to the left θ2, considering that if there are minor branches under the branch, it should be more to the left to the curvature of the tree; ⑨ right back to θ, back L2, return to the second fork point; ⑩ to the right, back to Theta, back to the beginning of the L1. At this point a complete tree was born. Algorithm analysis: In the algorithm, ③ and ⑦ used recursive method to call itself, it is this step simulates the nature of the natural tree growth. The ⑨ and ⑩ two steps are backtracking algorithms that ensure that the starting point of the tree can be returned to the turn when the subtree is finished. For the general case the program core code is as follows: Sub Leaf (ByVal n as Integer, ByVal L as single) ' Fern Leaf (n is recursive depth, size L) fern leaf and tree structure similar to Dim i as integer ' I is cyclic Variable If n = 0 Then Exit sub ' recursive end condition for i = branch to 1 Step-1 ' branch means number of branches FD L1 * i/br Anch * l LT Seta1 leaf n-1, I/branch * l/k1 ' left subtree size scale factor is K1   ; rt seta1–seta ' tree curvature is Seta FD L2 * i/branch * l RT Seta2 &nbs p; leaf n-1, I/branch * l/k2 ' right subtree size ratio factor of K2 LT Seta2 + Seta Next i for i = 1 to branch ' backtracking master dry RT Seta BK L2 * i/branch * l RT Seta BK L1 * i/branch * l Next I End Sub |
|
Program Description: This procedure is a multi-branch tree drawing, mainly by recursion and circulation to describe. The smart thing you need to think about is how the program is executed, which is a good chance for you to understand recursion and circulation more profoundly. In the program called a number of functions called turtle mapping, if there is a obedient little turtle under your control, it can understand the following command: FD N, in the current position forward n step, BK N, in the current position of N-step, RT N, at the current position to the right n degrees, LT N, at the current position to turn left n degrees. These functions are well implemented with VB line statements, which are not described in detail here. The code may be boring, but you'll feel the fun of programming as you look at these beautiful effects. The program interface is shown in Figure 3: The program can easily adjust the parameters of the operation, by adjusting the scale coefficient can change the size of the subtree, so as to determine the shape of the picture is more like a tree or a leaf; adjusting the tilt angle of the subtree can form different tree species, and the curvature affects the shape of the tree most in each parameter. Adjust it to create different categories of patterns. The animated button also allows you to observe the growing process of a leaf from small to large. By programming these varied and varied tree patterns, these patterns may reveal from the geometrical level the nature of the biological growth, waves and other phenomena of the principle, that is, the whole is from its own part of the continuous replication of the formation. The extension of the program: ① If you add three-dimensional functionality, you can paint a more realistic tree chart. ② as long as you change the rules a little, you can also draw a variety of similar to the fractal law of the graph. Let's do it, the infinite laws of nature are waiting for us to continue to explore and discover.
|
Program download page: Fern leaf Builder
This article has been published in the "Computer newspaper" 2005 12 (2005.4.16), and income 2005 "Computer newspaper" bound. Project name: programmed to draw a leaf program name: Fern leaf Builder date of production: 2005.2.16 P.C.: 236800 author e-mail:wydz511@sohu.com qq:84115144 |