Home >Backend Development >PHP Tutorial >How to print a solid and hollow diamond shape with side length N_PHP tutorial

How to print a solid and hollow diamond shape with side length N_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:54883browse

How to print a solid and hollow diamond with side length N in php

This article mainly introduces how to print a solid and hollow diamond with side length N in php Methods and examples analyze the techniques of drawing graphics using PHP loop statements. Friends in need can refer to it

The example in this article describes how PHP prints a solid and hollow diamond shape with side length N. Share it with everyone for your reference. The specific analysis is as follows:

Solid diamond calculation method:
$n: side length
$i: current line, starting from 0
$rows: Total number of rows

Upper part
Number of preceding spaces =$n-$i-1
Number of characters=$i*2+1

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

Use str_pad to reduce for/while loops

The code is as follows:

/**
* Print solid diamond
* @param int $n side length, default 5
* @param String $s characters displayed, default*
* @return String
*/
function solidDiamond($n=5, $s='*'){
$str = '';
// Calculate the total number of rows
$rows = $n*2-1;
// Loop to calculate * for each row
for($i=0; $i<$rows; $i++){
if($i<$n){ // Upper
$str .= str_pad('', ($n-$i-1), ' '). str_pad('', $i*2+1, $s)."rn";
}else{ // Lower part
$str .= str_pad('', ($i-$n+1), ' '). str_pad('', ($rows-$i)*2-1, $s). "rn";
}
}
return $str;
}
echo ''; <br> echo solidDiamond(5); <br> echo '';

The code is as follows:

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

Hollow diamond calculation method:
$n: side length
$i: current line, starting from 0
$rows: Total number of rows

Upper part
Number of preceding spaces =$n-$i-1
Number of empty spaces =$i*2+1-2
Number of characters = $i*2+1 - number of empty spaces

Lower part
Number of preceding spaces =$i-$n+1
Number of empty spaces = ($rows-$i)*2-1-2
Number of characters = ($rows-$i)*2-1 - Number of empty spaces

The code is as follows:

/**
* Print hollow diamond
* @param int $n side length, default 5
* @param String $s characters displayed, default*
* @return String
*/
function hollowDiamond($n=5, $s='*'){
$str = '';
// Calculate the total number of rows
$rows = $n*2-1;
// Loop to calculate * for each row
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). "rn";
}else{ // Lower part
$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). "rn";
}
}
return $str;
}
echo ''; <br> echo hollowDiamond(5); <br> echo '';

The code is as follows:

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

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/962925.htmlTechArticleHow to print a solid and hollow diamond with side length N in php. This article mainly introduces how to print a solid and hollow diamond with side length N in php. The method of solid and hollow diamond shapes with side length N, example analysis of php loop statement drawing...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn