Created
September 28, 2020 10:59
-
-
Save foxfabi/8b7832e0563e04030b1ebcb8df8f9c96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Draws a christmas tree | |
* | |
* Author: Fabian Dennler | |
*/ | |
$repeat = 3; // Amount of stages | |
print("Zeichen: "); | |
fscanf(STDIN, "%s", $character); // Character to use for drawing | |
print("Höhe: "); | |
fscanf(STDIN, "%d", $width); // Maximum width of tree | |
$output[0] = 1; // Tree top is always 1 character | |
$index = 1; // Index of tree row | |
// Prepare the tree stage | |
// Every row increment last row by 2 | |
for ($i = $index; $i < intval($width); $i++) { | |
$output[$i] = $output[$i - 1] + 2; | |
} | |
// Amount of character of the last row | |
$max = $output[sizeof($output) - 1]; | |
// Draw the tree stages | |
for ($i = 0; $i < $repeat; $i++) { | |
foreach ($output as $key => $value) { | |
tree($value, $max, $character); | |
} | |
} | |
// Draw the tree trunk | |
tree(3, $max, $character); | |
tree(3, $max, $character); | |
tree(3, $max, $character); | |
/** | |
* Draws the tree stage | |
* | |
* @param $value Amount of character in the row | |
* @param $max Width of tree | |
*/ | |
function tree($value, $max, $character) { | |
$spaces = 0.5 * ($max - $value); | |
// Draw the indent of this row | |
for ($k = 0; $k <= $spaces; $k++) { | |
print(" "); | |
} | |
// Draw the character of this row | |
for($k = 0; $k < $value; $k++) { | |
print($character); | |
} | |
print(chr(10)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment