Copy codeThe Code is as follows: <? Php
// The following code will draw a very beautiful leaf
// Define the point of view of PI
Define ("PII", M_PI/180 );
// Create an image resource and set the background to white. The foreground color to black.
$ Im = imagecreate (670,500 );
$ White = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF );
$ G = imagecolorallocate ($ im, 0x00, 0x00, 0x00 );
// The Code instantiated below shows that the initial values $ x, $ y, $ L, and $ a are not divided into 300,500,100,270.
Function drawLeaf ($ g, $ x, $ y, $ L, $ ){
Global $ im;
$ B = 50;
$ C = 9;
$ S1 = 2;
$ S2 = 3;
$ S3 = 1.2;
If ($ L> $ s1 ){
// Calculate the leaf positioning above
$ X2 = $ x + $ L * cos ($ a * PII );
$ Y2 = $ y + $ L * sin ($ a * PII );
$ X2R = $ x2 + $ L/$ s2 * cos ($ a + $ B) * PII );
$ Y2R = $ y2 + $ L/$ s2 * sin ($ a + $ B) * PII );
$ X2L = $ x2 + $ L/$ s2 * cos ($ a-$ B) * PII );
$ Y2L = $ y2 + $ L/$ s2 * sin ($ a-$ B) * PII );
// Calculate the position of the leaf.
$ X1 = $ x + $ L/$ s2 * cos ($ a * PII );
$ Y1 = $ y + $ L/$ s2 * sin ($ a * PII );
$ X1L = $ x1 + $ L/$ s2 * cos ($ a-$ B) * PII );
$ Y1L = $ y1 + $ L/$ s2 * sin ($ a-$ B) * PII );
$ X1R = $ x1 + $ L/$ s2 * cos ($ a + $ B) * PII );
$ Y1R = $ y1 + $ L/$ s2 * sin ($ a + $ B) * PII );
// Separate the trunk and leaf area of the leaves
ImageLine ($ im, (int) $ x, (int) $ y, (int) $ x2, (int) $ y2, $ g );
ImageLine ($ im, (int) $ x2, (int) $ y2, (int) $ x2R, (int) $ y2R, $ g );
ImageLine ($ im, (int) $ x2, (int) $ y2, (int) $ x2L, (int) $ y2L, $ g );
ImageLine ($ im, (int) $ x1, (int) $ y1, (int) $ x1L, (int) $ y1L, $ g );
ImageLine ($ im, (int) $ x1, (int) $ y1, (int) $ x1R, (int) $ y1R, $ g );
// Re-recursive call itself
DrawLeaf ($ g, $ x2, $ y2, $ L/$ s3, $ a + $ C );
DrawLeaf ($ g, $ x2R, $ y2R, $ L/$ s2, $ a + $ B );
DrawLeaf ($ g, $ x2L, $ y2L, $ L/$ s2, $ a-$ B );
DrawLeaf ($ g, $ x1L, $ y1L, $ L/$ s2, $ a-$ B );
DrawLeaf ($ g, $ x1R, $ y1R, $ L/$ s2, $ a + $ B );
}
}
// Instantiate
DrawLeaf ($ g, 300,500,100,270 );
Header ("Content-type: image/png ");
Imagepng ($ im );
?>
In PHP programming, recursive calls are often used with static variables. For more information about static variables, see the PHP manual. We hope the following code will be more conducive to understanding recursive and static variables.Copy codeThe Code is as follows: header ("Content-type: text/plain ");
Function static_function (){
Static $ I = 0;
If ($ I ++ <10 ){
Echo $ I. "\ n ";
Static_function ();
}
}
Static_function (); this code outputs numbers from 1 to 10. During the second operation of the static_function, variable I is retained and cannot be released because it is a static variable, so that the auto-increment value can be obtained.