master
Yaroslav Spesivtsev 16 часов назад
Родитель e8f60325e9
Сommit 451f768394

@ -40,3 +40,21 @@ std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bi
return bins;
};
void show_histogram_svg(const std::vector<size_t>& 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();
}

@ -2,3 +2,4 @@
#include <vector>
size_t find_minmax(const std::vector<double>& numbers, double& min, double& max);
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
void show_histogram_svg(const std::vector<size_t>& bins);

@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
struct Input{
std::vector<double> 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;
}

@ -1,5 +1,6 @@
#include "svg.h"
#include <iostream>
#include <string>
void svg_begin(double width, double height) {
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\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 << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />\n";
}
void svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
}

Загрузка…
Отмена
Сохранить