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