A detailed introduction to PHP recursive algorithms

Source: Internet
Author: User
PHP recursive algorithm code: Copy the code as follows: & lt ;? Php // define the point-of-PI angle value define (& quot; PII & quot;, M_PI/180); // create an image resource and define its background as white, the foreground color is black $ im = imagecreate (670,500); $ white = imagecolorallocate ($ im, 0xFF,

PHP recursive algorithm code:


Copy the code as follows:
<? Php
// Define the angle value of PI ("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.
FunctiondrawLeaf ($ 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 my personal PHP programming experience, 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 PHP recursive algorithms and static variables.
Copy the code as follows:
Header ("Content-type: text/plain"); functionstatic_function ()
{
Static $ I = 0;
If ($ I ++ <10)
{
Echo $ I. "\ n ";
Static_function ();
}
}
Static_function ();

This PHP recursive algorithm code outputs numbers ranging 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.

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.