From 7bb15b9746a30f2351b9eb308af84b43fbd48f8d Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 22 Apr 2024 00:17:18 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=20?= =?UTF-8?q?=D0=B8=D0=BD=D0=B4=D0=B8=D0=B2=D0=B8=D0=B4=D1=83=D0=B0=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD?= =?UTF-8?q?=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram_internal.h | 3 +++ svg.cpp | 8 ++++++-- svg.h | 3 +++ unittest.cpp | 12 ++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/histogram_internal.h b/histogram_internal.h index 59c1874..7cfb60d 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1,8 +1,11 @@ #ifndef HISTOGRAM_INTERNAL_H_INCLUDED #define HISTOGRAM_INTERNAL_H_INCLUDED #include +#include void find_minmax(const std::vector& numbers, double& min, double& max); +std::string +bin_color(size_t bin, size_t max_count); #endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/svg.cpp b/svg.cpp index 8d30bab..89fe2fe 100644 --- a/svg.cpp +++ b/svg.cpp @@ -28,6 +28,10 @@ svg_rect(double x, double y, double width, double height, string stroke, string cout << ""; } +string bin_color(size_t bin, size_t max_count){ + return ("#" + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count)); +} + void show_histogram_svg(const vector& bins) { const auto IMAGE_WIDTH = 400; @@ -46,12 +50,12 @@ show_histogram_svg(const vector& bins) { } } - double top = 0; for (size_t bin : bins) { const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count; + const auto color = bin_color(bin,max_count); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red","#ffeeee"); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"grey", color); top += BIN_HEIGHT; } svg_end(); diff --git a/svg.h b/svg.h index 86a4a8f..66a0f20 100644 --- a/svg.h +++ b/svg.h @@ -15,6 +15,9 @@ show_histogram_svg(const std::vector& bins); void svg_text(double left, double baseline, std::string text); +std::string +bin_color(size_t bin, size_t max_count); + void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fil = "black"); diff --git a/unittest.cpp b/unittest.cpp index a9ba7e5..dbb684b 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -3,6 +3,8 @@ #include "doctest.h" #include "histogram_internal.h" +using namespace std; + TEST_CASE("distinct positive numbers") { double min = 0; double max = 0; @@ -10,3 +12,13 @@ TEST_CASE("distinct positive numbers") { CHECK(min == 1); CHECK(max == 2); } + +TEST_CASE("bin color") { + size_t max_bin = 10; + size_t min_bin = 3; + string rat = "#" + to_string(10 - (min_bin * 9) / max_bin) + to_string(10 - (min_bin * 9) / max_bin) + to_string(10 - (min_bin * 9) / max_bin); + auto test1 = bin_color(max_bin,max_bin); + auto test2 = bin_color(min_bin,max_bin); + CHECK(test1 == "#111"); + CHECK(test2 == rat); +}