From 5deb5167969b2f2ef5473abf92d0f383e5fa43ed Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 21 Apr 2024 22:16:17 +0300 Subject: [PATCH] =?UTF-8?q?build:=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D0=BC=D0=BC=D0=B0=20=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BD=D0=B0=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB?= =?UTF-8?q?=D0=B8.=20=D0=9E=D1=81=D1=83=D1=89=D0=B5=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=20=D0=B3?= =?UTF-8?q?=D0=B8=D1=81=D1=82=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B5=20SVG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 2 +- histogram_internal.h | 8 ++++++ main.cpp | 4 +-- my_project.cbp | 5 ++++ svg.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++ svg.h | 21 ++++++++++++++++ unittest.cbp | 38 +++++++++++++++++++++++++++++ unittest.cpp | 12 +++++++++ 8 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 histogram_internal.h create mode 100644 svg.cpp create mode 100644 svg.h create mode 100644 unittest.cbp create mode 100644 unittest.cpp diff --git a/histogram.cpp b/histogram.cpp index fa1de22..8d1d766 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,6 +1,6 @@ #include "histogram.h" -static void +void find_minmax(const std::vector& numbers, double& min, double& max){ min = numbers[0]; max = numbers[0]; diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..59c1874 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,8 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED +#include + +void +find_minmax(const std::vector& numbers, double& min, double& max); + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/main.cpp b/main.cpp index b1eefd9..d74026d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include #include #include "histogram.h" -#include "text.h" +#include "svg.h" using namespace std; @@ -35,6 +35,6 @@ main() { auto 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/my_project.cbp b/my_project.cbp index ab42b91..86dd073 100644 --- a/my_project.cbp +++ b/my_project.cbp @@ -32,10 +32,15 @@ + + + + + diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..8d30bab --- /dev/null +++ b/svg.cpp @@ -0,0 +1,58 @@ +#include +#include "svg.h" + +using namespace std; + +void +svg_begin(double width, double height) { + cout << "\n"; + cout << "\n"; +} + +void +svg_end() { + cout << "\n"; +} + +void +svg_text(double left, double baseline, string text){ + cout << "" << text << ""; +} + +void +svg_rect(double x, double y, double width, double height, string stroke, string fil){ + cout << ""; +} + +void +show_histogram_svg(const 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); + + size_t max_count = 0; + for (size_t x: bins) { + if (x > max_count) { + max_count = x; + } + } + + + double top = 0; + for (size_t bin : bins) { + const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red","#ffeeee"); + top += BIN_HEIGHT; + } + svg_end(); +} diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..86a4a8f --- /dev/null +++ b/svg.h @@ -0,0 +1,21 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED +#include +#include + +void +svg_begin(double width, double height); + +void +svg_end(); + +void +show_histogram_svg(const std::vector& bins); + +void +svg_text(double left, double baseline, std::string text); + +void +svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fil = "black"); + +#endif // SVG_H_INCLUDED diff --git a/unittest.cbp b/unittest.cbp new file mode 100644 index 0000000..7f9621b --- /dev/null +++ b/unittest.cbp @@ -0,0 +1,38 @@ + + + + + + diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..a9ba7e5 --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,12 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "histogram_internal.h" + +TEST_CASE("distinct positive numbers") { + double min = 0; + double max = 0; + find_minmax({1, 2}, min, max); + CHECK(min == 1); + CHECK(max == 2); +}