From 1e34abf21f85ecfa18e14f7fb214814453d5b0c9 Mon Sep 17 00:00:00 2001 From: KovalenkoDM Date: Sun, 28 Apr 2024 21:18:43 +0300 Subject: [PATCH] svg output + personal --- main_original.cpp | 3 ++- svg.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++ svg.h | 3 +++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 svg.cpp create mode 100644 svg.h diff --git a/main_original.cpp b/main_original.cpp index 74e2129..2cd3772 100644 --- a/main_original.cpp +++ b/main_original.cpp @@ -2,6 +2,7 @@ #include #include "histogram.h" #include "text.h" +#include "svg.h" using namespace std; @@ -26,6 +27,6 @@ Input input_data() { int main() { Input in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_text(bins); + show_histogram_svg(bins); return 0; } \ No newline at end of file diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..fd6b1b0 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,61 @@ +#include +#include "svg.h" + +using namespace std; + +const auto IMAGE_WIDTH = 400; +const auto IMAGE_HEIGHT = 310; +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; + +void svg_begin(double width, double height) { + cout << "\n"; + cout << "\n"; + cout << "\n"; + +} + +void svg_end(double top) { + cout << "\n"; + cout << "\n"; +} + +void svg_text(double left, double baseline, string text) { + cout << "\n"; + cout << "" << text <<"\n"; +} + +void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") { + cout << "\n"; + cout << "\n"; + +} + + + +void show_histogram_svg(const vector& bins) { + double max = 0, scale = 1; + int maxlen = IMAGE_WIDTH - TEXT_WIDTH; + for (auto el: bins) { + if (max < el) { + max = el; + } + } + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + double top = 5; + scale = max * BLOCK_WIDTH / maxlen; + 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 / scale, BIN_HEIGHT); + top += BIN_HEIGHT; + } + svg_end(top); +} \ No newline at end of file diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..b2d42d1 --- /dev/null +++ b/svg.h @@ -0,0 +1,3 @@ +#include + +void show_histogram_svg(const std::vector& bins); \ No newline at end of file