From 47c9936890685523390b1026305551b1e497e79a Mon Sep 17 00:00:00 2001
From: TabolinIA <TabolinIA@mpei.ru>
Date: Wed, 22 May 2024 10:39:37 +0300
Subject: [PATCH] =?UTF-8?q?code:=20=D0=B8=D0=BD=D0=B4=D0=B8=D0=B2=D0=B8?=
 =?UTF-8?q?=D0=B4=D1=83=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=B2=D0=B0?=
 =?UTF-8?q?=D1=80=D0=B8=D0=B0=D0=BD=D1=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 main.cpp | 13 ++++++++++++-
 svg.cpp  | 41 +++++++++++++++++++----------------------
 svg.h    |  2 +-
 3 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/main.cpp b/main.cpp
index 6dca8b2..921d4e7 100644
--- a/main.cpp
+++ b/main.cpp
@@ -10,6 +10,7 @@ using namespace std;
 struct Input {
     vector<double> numbers;
     size_t bin_count{};
+    size_t user_block_width;
 };
 
 Input
@@ -25,6 +26,16 @@ input_data() {
     }
     cerr << "Enter bin count: ";
     cin >> in.bin_count;
+    in.user_block_width = 15;
+    do {
+        cerr  << "Enter block width: ";
+        cin >> in.user_block_width;
+        if ((in.user_block_width < 3) || (in.user_block_width > 30)){
+            cerr << "Error: block width must be included in the interval: [3; 30]" << endl;
+        }
+    }
+    while ((in.user_block_width < 3) || (in.user_block_width > 30));
+
     return in;
 }
 
@@ -34,6 +45,6 @@ main()
 {
     auto in = input_data();
     auto bins = make_histogram(in.numbers, in.bin_count);
-    show_histogram_svg(bins);
+    show_histogram_svg(bins, in.user_block_width);
     return 0;
 }
diff --git a/svg.cpp b/svg.cpp
index 35737c7..c5e9af9 100644
--- a/svg.cpp
+++ b/svg.cpp
@@ -30,7 +30,7 @@ void svg_rect(double x, double y, double width, double height, string stroke = "
 
 
 void
-show_histogram_svg(const vector<size_t>& bins) {
+show_histogram_svg(const vector<size_t>& bins, size_t user_block_width) {
 
     const auto IMAGE_WIDTH = 400;
     const auto IMAGE_HEIGHT = 300;
@@ -38,7 +38,7 @@ show_histogram_svg(const vector<size_t>& bins) {
     const auto TEXT_BASELINE = 20;
     const auto TEXT_WIDTH = 50;
     const auto BIN_HEIGHT = 30;
-    const auto BLOCK_WIDTH = 10;
+    const auto BLOCK_WIDTH = user_block_width;
 
     const size_t MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH - 1;
 
@@ -49,27 +49,24 @@ show_histogram_svg(const vector<size_t>& bins) {
         }
     }
 
-    if (maxbin > MAX_WIDTH) {
-
-    svg_begin(400, 300);
+    svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
     double top = 0;
-    for (size_t bin : bins) {
-        const double bin_width = MAX_WIDTH * (bin / maxbin);
-        svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
-        svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
-        top += BIN_HEIGHT;
-    }
-    svg_end();
-    }
-
-    svg_begin(400, 300);
-    double top = 0;
-    for (size_t bin : bins) {
-        const double bin_width = BLOCK_WIDTH * bin;
-        svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
-        svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
-        top += BIN_HEIGHT;
+    if (maxbin<=76){
+        for (size_t bin : bins) {
+            double bin_width = BLOCK_WIDTH * bin;
+            svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
+            svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
+            top += BIN_HEIGHT;
+        }
+        svg_end();
+    } else {
+        for (size_t bin : bins) {
+            double bin_width= MAX_WIDTH * (static_cast<double>(bin) / maxbin);
+            svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
+            svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
+            top += BIN_HEIGHT;
+        }
+        svg_end();
     }
-    svg_end();
 }
 
diff --git a/svg.h b/svg.h
index adca9a4..04f0f6c 100644
--- a/svg.h
+++ b/svg.h
@@ -1,6 +1,6 @@
 #ifndef SVG_H_INCLUDED
 #define SVG_H_INCLUDED
 void
-show_histogram_svg(const std::vector<size_t>& bins);
+show_histogram_svg(const std::vector<size_t>& bins, size_t user_block_width);
 
 #endif // SVG_H_INCLUDED