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 ' <xmp>'; &lt;br/&gt;echo Soliddiamond (5); &lt;br/&gt;echo '</xmp> ';
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 ' <xmp>'; &lt;br/&gt;echo Hollowdiamond (5); &lt;br/&gt;echo '</xmp> ';
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 ...