From cd149ef988f33688d5a870c24e0040356c8f6ad2 Mon Sep 17 00:00:00 2001 From: "Igor (ZharkovIG)" Date: Sun, 20 Oct 2024 02:14:20 +0300 Subject: [PATCH] =?UTF-8?q?build:=20=D0=9E=D1=81=D1=83=D1=89=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4?= =?UTF-8?q?=20=D0=B3=D0=B8=D1=81=D1=82=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC?= =?UTF-8?q?=D1=8B=20=D0=B2=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B5=20?= =?UTF-8?q?SVG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 38 ++------------------------------- histogram.h | 2 +- main.cpp | 40 ++-------------------------------- svg.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ svg.h | 23 ++++++++++++++++++++ 5 files changed, 87 insertions(+), 75 deletions(-) create mode 100644 svg.cpp create mode 100644 svg.h diff --git a/histogram.cpp b/histogram.cpp index 1097195..f8defdf 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,7 +1,7 @@ #include "histogram.h" void -find_minmax(const std::vector& numbers, double& min, double& max){ +find_minmax(std::vector& numbers, double& min, double& max){ min = numbers[0]; max = numbers[0]; @@ -17,7 +17,7 @@ find_minmax(const std::vector& numbers, double& min, double& max){ std::vector -make_histogram(const std::vector& numbers, size_t bin_count){ +make_histogram(std::vector& numbers, size_t bin_count){ std::vector bins(bin_count); @@ -42,37 +42,3 @@ make_histogram(const std::vector& numbers, size_t bin_count){ } return bins; } -/*#include "histogram.h" -using namespace std; -static void -find_minmax(std::vector& numbers, double& min, double& max){ - min = numbers[0]; - max = numbers[0]; - for (double x : numbers) { - if (x < min) { - min = x; - } - else if (x > max) { - max = x; - } - } -} - -std::vector -make_histogram(std::vector& numbers, size_t bin_count) { - double min, max; - find_minmax(numbers, min, max); - - std::vector bins(bin_count, 0); - double bin_size = (max - min) / bin_count; - - for (double number : numbers) { - size_t bin_index = bin_count - 1; // default to last bin - if (number < max) { - bin_index = static_cast((number - min) / bin_size); - } - bins[bin_index]++; - } - - return bins; -}*/ diff --git a/histogram.h b/histogram.h index a4a8a88..46f3355 100644 --- a/histogram.h +++ b/histogram.h @@ -4,7 +4,7 @@ #include std::vector -make_histogram(const std::vector& numbers, size_t bin_count); +make_histogram(std::vector& numbers, size_t bin_count); diff --git a/main.cpp b/main.cpp index 31f1fd8..efe800b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include #include #include "histogram.h" -#include "text.h" +#include "svg.h" using namespace std; struct Input { @@ -28,48 +28,12 @@ input_data(){ return in; } -/* -void -show_histogram_text(const vector& bins){ - size_t i; - size_t height = 0; - size_t max_count = 0; - for (size_t x: bins) { - if (x > max_count) { - max_count = x; - } - } - if (max_count > MAX_ASTERISK) { - for(size_t bin:bins){ - size_t height = bin; - height = MAX_ASTERISK * (static_cast(bin) / max_count); - for(size_t i = 0; i < height; i++){ - cout<<"*"; - } - cout<<"|"; - if(bin<100) cout<<" "; - if(bin<10) cout<<" "; - cout< +#include +#include +#include "svg.h" +using namespace std; +void +svg_begin(double width, double height) { + cout << "\n"; + cout << "\n"; +} + +void +svg_end() { + cout << "\n"; +} + +void +svg_text(double left, double baseline, string text){ + cout << "" << text << ""; +} + +void +svg_rect(double x, double y, double width, double height, string stroke, string fil){ + cout << ""; +} + +void +show_histogram_svg(const vector& bins) { + const auto IMAGE_WIDTH = 400; + const auto IMAGE_HEIGHT = 300; + const auto TEXT_LEFT = 20; + const auto TEXT_BASELINE = 20; + const auto TEXT_WIDTH = 50; + const auto BIN_HEIGHT = 30; + const auto BLOCK_WIDTH = 10; + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + + size_t max_count = 0; + for (size_t x: bins) { + if (x > max_count) { + max_count = x; + } + } + + + double top = 0; + for (size_t bin : bins) { + const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red","#ffeeee"); + top += BIN_HEIGHT; + } + svg_end(); +} + diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..70ccf76 --- /dev/null +++ b/svg.h @@ -0,0 +1,23 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED +#include +#include + +void +svg_begin(double width, double height); + +void +svg_end(); + +void +show_histogram_svg(const std::vector& bins); + +void +svg_text(double left, double baseline, std::string text); + +void +svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fil = "black"); + + + +#endif // SVG_H_INCLUDED