DragonOsman

ppp2chapter15ex6

Sep 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. // Osman Zakir
  2. // 7 / 4 / 2017
  3. // Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
  4. // Chapter 15 Exercise 6
  5. // chapter15ex6.cpp
  6. // Exercise Specifications:
  7. /**
  8. * Design and implement a bar graph class. Its basic data is a vector<double>
  9. * holding N values, and each value should be represented by a β€œbar” that is
  10. * a rectangle where the height represents the value.
  11. */
  12.  
  13. #include <vector>
  14. #include <iostream>
  15. #include <algorithm>
  16. #include "../../Graph.h"
  17. #include "../../Window.h"
  18.  
  19. namespace Graph_lib
  20. {
  21.     struct Bar_graph : Shape
  22.     {
  23.         Bar_graph(const Point &graph_tl, const int graph_width, const int graph_height, const Color &bar_fill,
  24.             const Color &bar_outline, const Color &graph_outline, const Color &graph_fill, const std::vector<double> &values);
  25.         void draw_lines() const;
  26.         int graph_width() const { return m_graph_width; }
  27.         int graph_height() const { return m_graph_height; }
  28.         const std::vector<double> &values_v() const { return m_values; }
  29.         const Vector_ref<Rectangle> &bars_v() const { return m_bars_v; }
  30.         void set_graph_width(const int value) { m_graph_width = value; }
  31.         void set_graph_height(const int value) { m_graph_height = value; }
  32.     private:
  33.         Point m_graph_tl;   // top-left of rectangle bouning graph
  34.         int m_graph_width;  // width of rectangle bounding graph
  35.         int m_graph_height; // height of rectangle bounding graph
  36.         std::vector<double> m_values;
  37.         Color m_bar_fill;
  38.         Color m_bar_outline;
  39.         Color m_graph_outline;
  40.         Color m_graph_fill;
  41.         Vector_ref<Rectangle> m_bars_v;
  42.     };
  43. }
  44.  
  45. int main()
  46. {
  47.     using namespace std;
  48.     using namespace Graph_lib;
  49.  
  50.     constexpr int win_x = 100;
  51.     constexpr int win_y = 100;
  52.     constexpr int win_width = 600;
  53.     constexpr int win_height = 400;
  54.     const Point win_tl{ win_x, win_y };
  55.     Graph_lib::Window win{ win_tl, win_width, win_height, "Bar graphs" };
  56.  
  57.     try
  58.     {
  59.         constexpr int graph_width = win_width - 40;
  60.         constexpr int graph_height = win_height - 60;
  61.         vector<double> values{ 5.6, 5.6, 4.3, 5.0 };
  62.         const Point rect_tl{ 100, 100 };
  63.         Bar_graph bar_graph{ Point{100, 100}, graph_width, graph_height, Color::blue, bar_graph.color(),
  64.             Color::black, Color::invisible, values };
  65.         bar_graph.set_color(Color::black);
  66.         bar_graph.set_fill_color(Color::blue);
  67.  
  68.         win.attach(bar_graph);
  69.  
  70.         gui_main();
  71.     }
  72.     catch (const runtime_error &e)
  73.     {
  74.         Text err_msg_start{ Point{(win_width / 2) - 200, win_height / 2}, "Runtime error: " };
  75.         Text err_msg{ Point{(win_width / 2) + err_msg_start.label().length() + 5, win_height / 2}, e.what() };
  76.         err_msg_start.set_color(Color::black);
  77.         err_msg.set_color(Color::black);
  78.  
  79.         win.attach(err_msg_start);
  80.         win.attach(err_msg);
  81.  
  82.         gui_main();
  83.     }
  84. }
  85.  
  86. Graph_lib::Bar_graph::Bar_graph(const Point &graph_tl, const int graph_width, const int graph_height, const Color &bar_fill,
  87.     const Color &bar_outline, const Color &graph_outline, const Color &graph_fill, const std::vector<double> &values)
  88.     :m_graph_tl{ graph_tl }, m_graph_width{ graph_width }, m_graph_height{ graph_height }, m_bar_fill{ bar_fill },
  89.     m_bar_outline{ bar_outline }, m_graph_outline{ graph_outline }, m_graph_fill{ graph_fill }, m_values{ values}
  90. {
  91.     const std::size_t N = values.size();
  92.     Rectangle bounding_rect{ graph_tl, graph_width, graph_height };
  93.     bounding_rect.set_color(graph_outline);
  94.     bounding_rect.set_fill_color(graph_fill);
  95.     bounding_rect.draw();
  96.     if (N == 0)
  97.     {
  98.         error("invalid number of values!");
  99.     }
  100.     int bar_width = (graph_width / N);
  101.     double bar_spaces = bar_width * 1.0 / (2 * N - 1);
  102.     bar_width -= bar_spaces;
  103.     auto it = std::max_element(values.begin(), values.end());
  104.     int max_value = *it;
  105.     for (std::size_t i = 0; i < N; ++i)
  106.     {
  107.         int bar_height = std::round(values[i]) * (graph_height / max_value);
  108.         m_bars_v.push_back(new Rectangle{
  109.             Point{ (i * bar_width) + bar_spaces, bar_height }, bar_width, bar_height
  110.         });
  111.         m_bars_v[i].set_color(bar_outline);
  112.         m_bars_v[i].set_fill_color(bar_fill);
  113.     }
  114. }
  115.  
  116. void Graph_lib::Bar_graph::draw_lines() const
  117. {
  118.     for (std::size_t i = 0; i < m_bars_v.size(); ++i)
  119.     {
  120.         m_bars_v[i].draw();
  121.     }
  122. }
Add Comment
Please, Sign In to add comment