From cc853ac0ef6f1447bb6e138b8bb46448986fb4d2 Mon Sep 17 00:00:00 2001 From: "Dmitriy (BerezhkovDA)" Date: Sat, 4 May 2024 18:25:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=83=D0=BD=D0=BA=D1=82=206:=20=D0=B2?= =?UTF-8?q?=D1=8B=D0=B2=D0=BE=D0=B4=20=D0=B3=D0=B8=D1=81=D1=82=D0=BE=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D0=BC=D0=BC=D1=8B=20=D0=B2=20SVG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sem2_lab1/histogram.cpp | 3 +- sem2_lab1/sem2_lab1.cpp | 3 +- sem2_lab1/sem2_lab1.vcxproj | 2 ++ sem2_lab1/sem2_lab1.vcxproj.filters | 6 ++++ sem2_lab1/svg.cpp | 52 +++++++++++++++++++++++++++++ sem2_lab1/svg.h | 3 ++ 6 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 sem2_lab1/svg.cpp create mode 100644 sem2_lab1/svg.h diff --git a/sem2_lab1/histogram.cpp b/sem2_lab1/histogram.cpp index 385ccfa..53c0a3c 100644 --- a/sem2_lab1/histogram.cpp +++ b/sem2_lab1/histogram.cpp @@ -1,6 +1,7 @@ #include "histogram.h" using namespace std; -static void find_minmax(const vector& numbers, double& min, double& max) { +void find_minmax(const vector& numbers, double& min, double& max) { + if (numbers.size() == 0) return; min = numbers[0]; max = numbers[0]; for (double x : numbers) diff --git a/sem2_lab1/sem2_lab1.cpp b/sem2_lab1/sem2_lab1.cpp index 005c754..40f3d2e 100644 --- a/sem2_lab1/sem2_lab1.cpp +++ b/sem2_lab1/sem2_lab1.cpp @@ -5,6 +5,7 @@ #include #include "histogram.h" #include "show_histogram.h" +#include "svg.h" using namespace std; struct Input { @@ -29,7 +30,7 @@ int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_text(bins); + show_histogram_svg(bins,in.numbers.size()); return 0; } diff --git a/sem2_lab1/sem2_lab1.vcxproj b/sem2_lab1/sem2_lab1.vcxproj index 88cb2e8..f8d47d6 100644 --- a/sem2_lab1/sem2_lab1.vcxproj +++ b/sem2_lab1/sem2_lab1.vcxproj @@ -130,6 +130,7 @@ + @@ -138,6 +139,7 @@ + diff --git a/sem2_lab1/sem2_lab1.vcxproj.filters b/sem2_lab1/sem2_lab1.vcxproj.filters index e60da40..46a52d6 100644 --- a/sem2_lab1/sem2_lab1.vcxproj.filters +++ b/sem2_lab1/sem2_lab1.vcxproj.filters @@ -24,6 +24,9 @@ Исходные файлы + + Исходные файлы + @@ -38,5 +41,8 @@ Файлы заголовков + + Файлы заголовков + \ No newline at end of file diff --git a/sem2_lab1/svg.cpp b/sem2_lab1/svg.cpp new file mode 100644 index 0000000..2d81807 --- /dev/null +++ b/sem2_lab1/svg.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace std; +void svg_begin(double width, double height) { + cout << "\n"; + cout << "\n"; +} +void svg_text(double left, double baseline, string text) { + cout << "" << text << "" << endl; +} +void svg_end() { + cout << "\n"; +} +void svg_rect(double x, double y, double width, double height,string stroke = "black", string fill = "green") +{ + cout << ""; +} +void show_histogram_svg(const vector& bins, int number_cnt) { + 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; + string colors[] {"#0000ff","#00ff00","#ff0000" ,"#00ffff" ,"#ff00ff" ,"#ffff00","#ffffff","#aaaaaa" }; + svg_begin(400, 300); + double top = 0; + int color_ptr=0; + double max = bins[0]; + for (size_t bin : bins) if (bin > max) max = bin; + cout << max; + double k = (IMAGE_WIDTH - TEXT_WIDTH) / max; + for (size_t bin : bins) { + const double bin_width = k * bin; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"black",colors[color_ptr]); + top += BIN_HEIGHT; + color_ptr++; + if (color_ptr >= colors->size()) color_ptr = 0; + } + + svg_end(); + +} + diff --git a/sem2_lab1/svg.h b/sem2_lab1/svg.h new file mode 100644 index 0000000..d8705e4 --- /dev/null +++ b/sem2_lab1/svg.h @@ -0,0 +1,3 @@ +#pragma once +#include +void show_histogram_svg(const std::vector& bins,int number_cnt); \ No newline at end of file