PHP Print a method of solid and hollow shape with side length n _php tutorial

Source: Internet
Author: User
Tags php print

PHP prints a method of a solid and hollow shape with a side length of n


This article mainly introduces PHP printing a side-length n solid and hollow type of the method, the example of the PHP loop to draw graphics skills, the need for friends can refer to the next

The example in this paper describes how PHP prints a solid and hollow diamond with an edge length of N. Share to everyone for your reference. The specific analysis is as follows:

Calculation method for solid type of diamond:
$n: Edge Length
$i: Current line, 0 start
$rows: Total number of rows

Upper
Number of spaces in front = $n-$i-1
Number of characters = $i *2+1

Lower
Number of spaces in front = $i-$n +1
Number of characters = ($rows-$i) *2-1

Use Str_pad to reduce loops such as For/while

The code is as follows:

/**
* Print solid Diamond type
* @param int $n edge length, default 5
* @param string $s the characters shown, default *
* @return String
*/
function Soliddiamond ($n =5, $s = ' * ') {
$str = ";
Calculate Total rows
$rows = $n *2-1;
Loop calculates the * of each line
for ($i =0; $i < $rows; $i + +) {
if ($i < $n) {//Upper
$str. = Str_pad (", ($n-$i-1),"). Str_pad (", $i *2+1, $s)." \ r \ n ";
}else{//Lower
$str. = Str_pad (", ($i-$n + 1),"). Str_pad (", ($rows-$i) *2-1, $s). "\ r \ n";
}
}
return $str;
}
Echo ' &lt;xmp&gt;'; &amp;lt;br/&amp;gt;echo Soliddiamond (5); &amp;lt;br/&amp;gt;echo '&lt;/xmp&gt; ';

The code is as follows:

*
***
*****
*******
*********
*******
*****
***
*

The calculation method of hollow type of diamond:
$n: Edge Length
$i: Current line, 0 start
$rows: Total number of rows

Upper
Number of spaces in front = $n-$i-1
Number of empty spaces = $i *2+1-2
Number of characters = $i *2+1-The number of empty spaces

Lower
Number of spaces in front = $i-$n +1
Number of empty spaces = ($rows-$i) *2-1-2
Number of characters = ($rows-$i) *2-1-the number of empty spaces

The code is as follows:

/**
* Print Hollow Diamond type
* @param int $n edge length, default 5
* @param string $s the characters shown, default *
* @return String
*/
function Hollowdiamond ($n =5, $s = ' * ') {
$str = ";
Calculate Total rows
$rows = $n *2-1;
Loop calculates the * of each line
for ($i =0; $i < $rows; $i + +) {
if ($i < $n) {//Upper
$tmp = $i *2+1;
$str. = Str_pad (", ($n-$i-1),"). Str_pad (Str_pad (", $tmp-2," ', Str_pad_both), $tmp, $s, str_pad_both). " \ r \ n ";
}else{//Lower
$tmp = ($rows-$i) *2-1;
$str. = Str_pad (", ($i-$n + 1),"). Str_pad (Str_pad (", $tmp-2, '", Str_pad_both), $tmp, $s, str_pad_both). "\ r \ n";
}
}
return $str;
}
Echo ' &lt;xmp&gt;'; &amp;lt;br/&amp;gt;echo Hollowdiamond (5); &amp;lt;br/&amp;gt;echo '&lt;/xmp&gt; ';

The code is as follows:

*
* *
* *
* *
* *
* *
* *
* *
*

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/962925.html www.bkjia.com true http://www.bkjia.com/PHPjc/962925.html techarticle PHP Print a solid and hollow shape with a side length of n This article mainly introduces PHP printing a side-length n of the solid and hollow type of the method, the example analysis of the PHP Loop statement drawing ...

  • Related Article

    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.