Php prints a solid and hollow rhombus type with a side length of N

Source: Internet
Author: User
This article mainly introduces the php method of printing a solid and hollow rhombus with a side length of N. the example shows the graph drawing technique of php loop statements, for more information about how to use php to print a solid and hollow rhombus with a side length of N, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Calculation method of solid diamond:
$ N: edge length
$ I: current row, starting from 0
$ Rows: total number of rows

Upper part
Front space = $ n-$ I-1
Character count = $ I * 2 + 1

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

Use str_pad to reduce loops such as for and while.

The code is as follows:

/**
* Print solid diamond
* @ Param int $ n edge length. the default value is 5.
* @ Param String $ s: the character displayed. default value *
* @ Return String
*/
Function solidDiamond ($ n = 5, $ s = '*'){
$ Str = '';
// Calculate the total number of rows
$ Rows = $ n * 2-1;
// Calculate the number of rows in a loop *
For ($ I = 0; $ I <$ rows; $ I ++ ){
If ($ I <$ n) {// upper part
$ Str. = str_pad ('', ($ n-$ i-1 ),''). str_pad ('', $ I * 2 + 1, $ s ). "\ r \ n ";
} Else {// Lower part
$ Str. = str_pad ('', ($ I-$ n + 1 ),''). str_pad ('', ($ rows-$ I) * 2-1, $ s ). "\ r \ n ";
}
}
Return $ str;
}
Echo'

'; &lt;Br/&gt; echo solidDiamond (5); &lt;br/&gt; echo' ';

The code is as follows:

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

Hollow rhombus computing method:
$ N: edge length
$ I: current row, starting from 0
$ Rows: total number of rows

Upper part
Front space = $ n-$ I-1
Number of blank spaces = $ I * 2 + 1-2
Characters = $ I * 2 + 1-Number of blank spaces

Lower part
Number of leading spaces = $ I-$ n + 1
Number of blank spaces = ($ rows-$ I) * 2-1-2
Number of characters = ($ rows-$ I) * 2-1-Number of blank spaces

The code is as follows:

/**
* Print hollow rhombus
* @ Param int $ n edge length. the default value is 5.
* @ Param String $ s: the character displayed. default value *
* @ Return String
*/
Function hollowDiamond ($ n = 5, $ s = '*'){
$ Str = '';
// Calculate the total number of rows
$ Rows = $ n * 2-1;
// Calculate the number of rows in a loop *
For ($ I = 0; $ I <$ rows; $ I ++ ){
If ($ I <$ n) {// upper part
$ Tmp = $ I * 2 + 1;
$ Str. = str_pad ('', ($ n-$ i-1 ),''). str_pad ('', $ tmp-2,'', STR_PAD_BOTH), $ tmp, $ s, STR_PAD_BOTH ). "\ r \ n ";
} Else {// Lower part
$ Tmp = ($ rows-$ I) * 2-1;
$ Str. = str_pad ('', ($ I-$ n + 1 ),''). str_pad ('', $ tmp-2,'', STR_PAD_BOTH), $ tmp, $ s, STR_PAD_BOTH ). "\ r \ n ";
}
}
Return $ str;
}
Echo'

'; &lt;Br/&gt; echo hollowDiamond (5); &lt;br/&gt; echo' ';

The code is as follows:

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

I hope this article will help you with php programming.

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.