diff --git a/Histogram/histogram.cpp b/Histogram/histogram.cpp index 01b1a10..e61b64e 100644 --- a/Histogram/histogram.cpp +++ b/Histogram/histogram.cpp @@ -40,3 +40,21 @@ std::vector make_histogram(const std::vector& numbers, size_t bi return bins; }; +void show_histogram_svg(const std::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); + for (size_t i = 0; i < bins.size(); ++i) { + const double bin_width = BLOCK_WIDTH * bins[i]; + const double top = i * BIN_HEIGHT; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i])); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "#aaffaa"); // Пример цвета + } + svg_end(); +} diff --git a/Histogram/histogram.h b/Histogram/histogram.h index 1c2e056..8a5d92a 100644 --- a/Histogram/histogram.h +++ b/Histogram/histogram.h @@ -2,3 +2,4 @@ #include size_t find_minmax(const std::vector& numbers, double& min, double& max); std::vector make_histogram(const std::vector& numbers, size_t bin_count); +void show_histogram_svg(const std::vector& bins); diff --git a/Histogram/main.cpp b/Histogram/main.cpp index 14b5629..1455d88 100644 --- a/Histogram/main.cpp +++ b/Histogram/main.cpp @@ -1,6 +1,7 @@ #include #include - +#include "histogram.h" +#include "text.h" struct Input{ std::vector numbers; size_t bin_count{}; @@ -24,12 +25,11 @@ Input input_data() { return in; }; - using namespace std; 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; } diff --git a/Histogram/svg.cpp b/Histogram/svg.cpp index a2baf61..02a4501 100644 --- a/Histogram/svg.cpp +++ b/Histogram/svg.cpp @@ -1,5 +1,6 @@ #include "svg.h" #include +#include void svg_begin(double width, double height) { std::cout << "\n"; @@ -15,4 +16,9 @@ void svg_end() { } void svg_rect(double x, double y, double width, double height, std::string stroke, std::string fill) { - std::cout << "\n"; +} + +void svg_text(double left, double baseline, std::string text) { + std::cout << "" << text << "\n"; +}