Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib> // rand(), srand()
- #include <ctime> // time()
- using namespace std;
- // This program draws three trees on a canvas. The location and size of the trees are chosen randomly.
- int tree_sizes[] = { 3, 4, 5, 6, 7, 8 }; // Available tree sizes (number of rows of leaves)
- const int canvas_width = 60; // Horizontal space available to draw trees
- const int canvas_indent = 15; // Canvas begins in column number 16.
- // The trunk is the same for all trees.
- // This function draws a tree.
- void draw_tree(int size, int placement, string leaf = "@", string trunk = "H", string space = " ", string margin_symbol = "`", int margin = 2)
- {
- int midline = placement - 1; // Column index of the middle of the tree
- int num_rows = size; // Number of rows of leaves
- int side_margin = margin; // Space between the tree and the side of the box the tree is drawn in
- string leaf_symbol = leaf; // Symbol used for the tree leaves
- string trunk_symbol = trunk; // Symbol used for the tree trunk
- string space_symbol = space; // Symbol used outside the box the tree is drawn in
- string side_margin_symbol = margin_symbol; // Background symbol for area within the box that is not the leaves or the trunk
- // Draw the leaves.
- for (int row = 0; row < num_rows; ++row) // For each row of leaves,
- {
- for (int column = 0; column <= (midline + (num_rows - 1) + side_margin); ++column) // For each column in the box,
- {
- if ((column >= midline - row) && (column <= midline + row)) // If the column is part of the leaves,
- {
- cout << leaf_symbol; // Draw a leaf.
- }
- // Else if the column is between the tree and the box the tree is drawn in,
- else if (((column >= (midline - (num_rows - 1) - side_margin)) && (column < (midline - row))) ||
- ((column > (midline + row)) && (column <= (midline + (num_rows - 1) + side_margin))))
- {
- cout << margin_symbol; // Draw the background symbol.
- }
- else // Otherwise,
- {
- cout << space_symbol; // Draw a space.
- }
- }
- cout << endl; // Go to the next line.
- }
- // Draw the trunk.
- for (int row = 0; row < 2; ++row) // For the height of the trunk,
- {
- for (int column = 0; column <= (midline + (num_rows - 1) + (side_margin * 2)); ++column) // If the column is on the canvas,
- {
- if (column == midline) // If the column is the middle of the tree,
- {
- cout << trunk_symbol; // Draw the trunk.
- }
- // Else if the column is part of the background,
- else if (((column >= (midline - (num_rows - 1) - side_margin)) && (column < midline)) ||
- ((column > midline) && (column <= (midline + (num_rows - 1) + side_margin))))
- {
- cout << margin_symbol; // Draw the background.
- }
- else // Otherwise,
- {
- cout << space_symbol; // Draw a space.
- }
- }
- cout << endl; // Go to the next line.
- }
- }
- int main()
- {
- int tree_size = 0; // Number of rows of leaves
- int num_trees = 3; // How many trees to draw
- int tree_location = 0; // Column number of tree horizontally
- int num_possible_locations = 0; // Number of columns that could have the midline of the tree
- srand(time(NULL)); // Prepare the random number generator.
- for (int i = 0; i < num_trees; ++i) // For each tree,
- {
- tree_size = (rand() % 6) + 3; // Pick a tree size between 3 and 8.
- // Determine the range of locations for the tree midline within the canvas.
- num_possible_locations = (canvas_width - (tree_size - 1) - 2) - (canvas_indent - (tree_size - 1) - 2);
- tree_location = (rand() % num_possible_locations) + (canvas_indent + 1); // Pick the tree midline.
- tree_size = (rand() % 6) + 3; // Pick a tree size between 3 and 8.
- draw_tree(tree_size, tree_location); // Draw tree.
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement