Advertisement
janac

Draw symmetric shape with randomized size and location

Dec 24th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib> // rand(), srand()
  5. #include <ctime> // time()
  6. using namespace std;
  7.  
  8. // This program draws three trees on a canvas. The location and size of the trees are chosen randomly.
  9.  
  10. int tree_sizes[] = { 3, 4, 5, 6, 7, 8 }; // Available tree sizes (number of rows of leaves)
  11. const int canvas_width = 60; // Horizontal space available to draw trees
  12. const int canvas_indent = 15; // Canvas begins in column number 16.
  13. // The trunk is the same for all trees.
  14.  
  15. // This function draws a tree.
  16. void draw_tree(int size, int placement, string leaf = "@", string trunk = "H", string space = " ", string margin_symbol = "`", int margin = 2)
  17. {
  18.     int midline = placement - 1; // Column index of the middle of the tree
  19.     int num_rows = size; // Number of rows of leaves
  20.     int side_margin = margin; // Space between the tree and the side of the box the tree is drawn in
  21.     string leaf_symbol = leaf; // Symbol used for the tree leaves
  22.     string trunk_symbol = trunk; // Symbol used for the tree trunk
  23.     string space_symbol = space; // Symbol used outside the box the tree is drawn in
  24.     string side_margin_symbol = margin_symbol; // Background symbol for area within the box that is not the leaves or the trunk
  25.  
  26.     // Draw the leaves.
  27.     for (int row = 0; row < num_rows; ++row) // For each row of leaves,
  28.     {
  29.         for (int column = 0; column <= (midline + (num_rows - 1) + side_margin); ++column) // For each column in the box,
  30.         {
  31.             if ((column >= midline - row) && (column <= midline + row)) // If the column is part of the leaves,
  32.             {
  33.                 cout << leaf_symbol; // Draw a leaf.
  34.             }
  35.             // Else if the column is between the tree and the box the tree is drawn in,
  36.             else if (((column >= (midline - (num_rows - 1) - side_margin)) && (column < (midline - row))) ||
  37.                 ((column > (midline + row)) && (column <= (midline + (num_rows - 1) + side_margin))))
  38.             {
  39.                 cout << margin_symbol; // Draw the background symbol.
  40.             }
  41.             else // Otherwise,
  42.             {
  43.                 cout << space_symbol; // Draw a space.
  44.             }
  45.         }
  46.         cout << endl; // Go to the next line.
  47.     }
  48.  
  49.     // Draw the trunk.
  50.     for (int row = 0; row < 2; ++row) // For the height of the trunk,
  51.     {
  52.         for (int column = 0; column <= (midline + (num_rows - 1) + (side_margin * 2)); ++column) // If the column is on the canvas,
  53.         {
  54.             if (column == midline) // If the column is the middle of the tree,
  55.             {
  56.                 cout << trunk_symbol; // Draw the trunk.
  57.             }
  58.             // Else if the column is part of the background,
  59.             else if (((column >= (midline - (num_rows - 1) - side_margin)) && (column < midline)) ||
  60.                 ((column > midline) && (column <= (midline + (num_rows - 1) + side_margin))))
  61.             {
  62.                 cout << margin_symbol; // Draw the background.
  63.             }
  64.             else // Otherwise,
  65.             {
  66.                 cout << space_symbol; // Draw a space.
  67.             }
  68.         }
  69.         cout << endl; // Go to the next line.
  70.     }      
  71. }
  72.  
  73. int main()
  74. {
  75.     int tree_size = 0; // Number of rows of leaves
  76.     int num_trees = 3; // How many trees to draw
  77.     int tree_location = 0; // Column number of tree horizontally
  78.     int num_possible_locations = 0; // Number of columns that could have the midline of the tree
  79.    
  80.     srand(time(NULL)); // Prepare the random number generator.
  81.  
  82.     for (int i = 0; i < num_trees; ++i) // For each tree,
  83.     {
  84.         tree_size = (rand() % 6) + 3; // Pick a tree size between 3 and 8.
  85.         // Determine the range of locations for the tree midline within the canvas.
  86.         num_possible_locations = (canvas_width - (tree_size - 1) - 2) - (canvas_indent - (tree_size - 1) - 2);
  87.         tree_location = (rand() % num_possible_locations) + (canvas_indent + 1); // Pick the tree midline.
  88.         tree_size = (rand() % 6) + 3; // Pick a tree size between 3 and 8.
  89.         draw_tree(tree_size, tree_location); // Draw tree.
  90.     }
  91.  
  92.     return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement