PHP is still more commonly used, so I studied the PHP recursive algorithm, here to take out and share with you, hope to be useful to everyone. PHP, a nested abbreviated name, is an abbreviation for the English Super Text preprocessing language (Php:hypertext preprocessor). PHP is an HTML embedded language, is a server-side implementation of embedded HTML document scripting language, language style has similar to the C language, is now widely used by many web site programmers. PHP's unique syntax mixes C, Java, Perl, and PHP's self-innovative syntax.
It can perform dynamic Web pages more quickly than CGI or Perl. Dynamic pages made with PHP are compared to other programming languages, and PHP is executed in an HTML document, which is much more efficient than CGI, which is also embedded in the HTML document, and PHP is executed on the server side, compared to the scripting language JavaScript Take full advantage of the performance of the server, the PHP execution engine will also be the user frequently access to the PHP program in memory, other users do not need to recompile the program once again to access the program, as long as the direct execution of in-memory code, which is the embodiment of PHP efficiency.
PHP has a very powerful feature, all CGI or JavaScript functionality PHP can be implemented, and support almost all the popular database and operating system. We introduce the PHP recursive algorithm in detail here.
PHP Recursive algorithm code:
- PHP
- Defines the value of the angle of a pi point
- Define ("PII", m_pi/180);
- Creates a new image resource and defines its background as white with a foreground color of black
- $ im = imagecreate (670,500);
- $ White = imagecolorallocate ($im, 0xff,0xff,0xff);
- $ g = imagecolorallocate ($im, 0x00,0x00,0x00);
- From the code instantiated below you can tell that the initial value $x, $y, $L, $a not divided into 300,500,100,270
- Functiondrawleaf ($g, $x, $y, $L, $a) {
- Global$im;
- $ B = - ;
- $ C = 9 ;
- $ S1 = 2 ;
- $ S2 = 3 ;
- $ S3 = 1 . 2;
- if ($L>$s 1) {
- Calculate the positioning of the leaves above
- $ X2 = $x + $L *cos ($a *pii);
- $ y2 = $y + $L *sin ($a *pii);
- $ X2R = $x $L/$s 2*cos (($a + $B) *pii);
- $ y2r = $y $L/$s 2*sin (($a + $B) *pii);
- $ x2l = $x $L/$s 2*cos (($a-$B) *pii);
- $ y2l = $y $L/$s 2*sin (($a-$B) *pii);
- Calculate the position of the leaves below
- $ X1 = $x + $L/$s 2*cos ($a *pii);
- $ Y1 = $y + $L/$s 2*sin ($a *pii);
- $ x1l = $x 1+ $L/$s 2*cos (($a-$B) *pii);
- $ y1l = $y 1+ $L/$s 2*sin (($a-$B) *pii);
- $ x1r = $x 1+ $L/$s 2*cos (($a + $B) *pii);
- $ y1r = $y 1+ $L/$s 2*sin (($a + $B) *pii);
- Do not draw the leaves of the trunk and foliage
- Imageline ($im, (int) $x, (int) $y, (int) $x 2, (int) $y 2, $g);
- Imageline ($im, (int) $x 2, (int) $y 2, (int) $x 2R, (int) $y 2R, $g);
- Imageline ($im, (int) $x 2, (int) $y 2, (int) $x 2L, (int) $y 2L, $g);
- Imageline ($im, (int) $x 1, (int) $y 1, (int) $x 1L, (int) $y 1L, $g);
- Imageline ($im, (int) $x 1, (int) $y 1, (int) $x 1R, (int) $y 1R, $g);
- Recursively calls itself again
- Drawleaf ($g, $x 2, $y 2, $L/$s 3, $a + $C);
- Drawleaf ($g, $x 2R, $y 2R, $L/$s 2, $a + $B);
- Drawleaf ($g, $x 2L, $y 2L, $L/$s 2, $a-$B);
- Drawleaf ($g, $x 1L, $y 1L, $L/$s 2, $a-$B);
- Drawleaf ($g, $x 1R, $y 1R, $L/$s 2, $a + $B);
- }
- }
- Instantiation of
- Drawleaf ($g, 300,500,100,270);
- Header ("Content-type:image/png");
- Imagepng ($im);
- ?> in my personal experience with PHP programming, recursive calls are often used with static variables. The meaning of static variables can be referenced in the PHP manual. Hopefully, the following code will be more useful for understanding recursion and static variables.
- Header ("Content-type:text/plain");
- Functionstatic_function () {
- static$ I = 0 ;
- if ($i + +<) {
- Echo$i. " n ";
- Static_function ();
- }
- }
- Static_function ();
This code will output a number from 1 to 10. When the Static_function function is run for the second time, the variable i is still not released because it is a static variable, and thus the self-increment value can be obtained.
http://www.bkjia.com/PHPjc/446491.html www.bkjia.com true http://www.bkjia.com/PHPjc/446491.html techarticle PHP is still more commonly used, so I studied the PHP recursive algorithm, here to take out and share with you, hope to be useful to everyone. PHP, a nested abbreviated name, is English super ...