#include "svg.h" #include #include #include using namespace std; namespace svg { size_t BLOCK_WIDTH = 10; size_t input_block_width() { size_t width; while (true) { cout << "Введите ширину блока гистограммы (3-30px): "; cin >> width; if (width < 3) { cout << "Ширина слишком мала. Минимальное значение: 3px. Пожалуйста, повторите ввод.\n"; } else if (width > 30) { cout << "Ширина слишком велика. Максимальное значение: 30px. Пожалуйста, повторите ввод.\n"; } else { break; } } return width; } void set_block_width(size_t width) { BLOCK_WIDTH = width; } void begin(ostream& out, double width, double height) { out << "\n"; out << "\n"; } void end(ostream& out) { out << "\n"; } void text(ostream& out, double left, double baseline, const string& text) { out << "" << text << "\n"; } void rect(ostream& out, double x, double y, double width, double height, const string& stroke, const string& fill) { out << "\n"; } void show_histogram_svg(ofstream& out, const vector& bins, size_t block_width) { begin(out, IMAGE_WIDTH, IMAGE_HEIGHT); if (bins.empty()) { end(out); return; } const size_t max_count = *max_element(bins.begin(), bins.end()); if (max_count == 0) { end(out); return; } const double max_svg_width = IMAGE_WIDTH - TEXT_WIDTH - 40; const double scale = max_svg_width / max_count; double top = 0; for (size_t bin : bins) { double width = bin * scale * (block_width / 10.0); text(out, TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); rect(out, TEXT_WIDTH, top, width, BIN_HEIGHT, "black", "#aaffaa"); top += BIN_HEIGHT; } end(out); } }