From 8c17279d7f02f7630ee6de7c9ba2774247f8d6ed Mon Sep 17 00:00:00 2001 From: KuybedaAY Date: Fri, 2 May 2025 02:39:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B8?= =?UTF-8?q?=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + main.cpp | 3 ++- marks.svg | 0 marks.txt | 3 +++ svg.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ svg.h | 10 ++++++++++ unittest.cpp | 12 ++++++++++++ 7 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 marks.svg create mode 100644 marks.txt create mode 100644 svg.cpp create mode 100644 svg.h diff --git a/.gitignore b/.gitignore index e589a4c..e5a9a31 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /lab1.depend /lab1.layout /unittest.layout +/unittest.depend diff --git a/main.cpp b/main.cpp index d78849b..fd793a9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,9 @@ #include "histogram.h" +#include "svg.h" int 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/marks.svg b/marks.svg new file mode 100644 index 0000000..e69de29 diff --git a/marks.txt b/marks.txt new file mode 100644 index 0000000..2cdb9aa --- /dev/null +++ b/marks.txt @@ -0,0 +1,3 @@ +10 +3 3 4 4 4 4 4 5 5 5 +3 diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..ed9bfed --- /dev/null +++ b/svg.cpp @@ -0,0 +1,51 @@ +#include "svg.h" +#include + +void svg_begin(double width, double height) { + std::cout << "\n"; + std::cout << "\n"; +} + +void svg_end() { + std::cout << "\n"; +} + +void svg_text(double left, double baseline, const std::string& text) { + std::cout << "" << text << "\n"; +} + +void svg_rect(double x, double y, double width, double height, + const std::string& stroke, const std::string& fill) { + std::cout << "\n"; +} + +void show_histogram_svg(const std::vector& bins) { + const size_t IMAGE_WIDTH = 400; + const size_t IMAGE_HEIGHT = 300; + const size_t TEXT_LEFT = 20; + const size_t TEXT_BASELINE = 20; + const size_t TEXT_WIDTH = 50; + const size_t BIN_HEIGHT = 30; + const size_t BLOCK_WIDTH = 10; + + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + + double top = 0; + size_t max_count = 0; + for (size_t count : bins) { + if (count > max_count) max_count = count; + } + + for (size_t bin : bins) { + const double bin_width = static_cast(bin) / max_count * (IMAGE_WIDTH - TEXT_WIDTH); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "#aaffaa"); + top += BIN_HEIGHT; + } + + svg_end(); +} diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..62c8e73 --- /dev/null +++ b/svg.h @@ -0,0 +1,10 @@ +#pragma once +#include +#include + +void svg_begin(double width, double height); +void svg_end(); +void svg_text(double left, double baseline, const std::string& text); +void svg_rect(double x, double y, double width, double height, + const std::string& stroke = "black", const std::string& fill = "black"); +void show_histogram_svg(const std::vector& bins); diff --git a/unittest.cpp b/unittest.cpp index e69de29..a9ba7e5 100644 --- a/unittest.cpp +++ 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); +}