code: добавлен вариант 10

master
EfimovaLA 11 месяцев назад
Родитель 2724b2df4a
Сommit ed39bf7802

@ -2,6 +2,8 @@
#include <vector> #include <vector>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "histogram_internal.h"
#include "svg.h"
using namespace std; using namespace std;
const size_t SCREEN_WIDTH = 80; const size_t SCREEN_WIDTH = 80;
@ -9,7 +11,7 @@ const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
struct Input struct Input
{ {
std::vector<double> numbers; vector<double> numbers;
size_t bin_count{}; size_t bin_count{};
}; };
@ -35,13 +37,9 @@ Input input_data()
int main() int main()
{ {
Input in = input_data(); Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg_vertical(bins);
show_histogram_text(bins);
} }

@ -64,3 +64,41 @@ show_histogram_svg(const std::vector<std::size_t>& bins){
svg_end(); svg_end();
} }
} }
void show_histogram_svg_vertical(const std::vector<std::size_t>& bins) {
const auto IMAGE_WIDTH = 300;
const auto IMAGE_HEIGHT = 400;
const auto TEXT_LEFT = 20;
const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 30;
const auto BIN_WIDTH = 30;
const auto BLOCK_HEIGHT = 10;
const auto MAX_ASTERISK = IMAGE_HEIGHT - TEXT_WIDTH;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double left = 0;
std::size_t maxbin = bins[0];
for (std::size_t i = 0; i < bins.size(); i++) {
if (maxbin < bins[i]) {
maxbin = bins[i];
}
}
if (maxbin <= 76) {
for (std::size_t i = 0; i < bins.size(); i++) {
double bin_height = BLOCK_HEIGHT * bins[i];
svg_text(left, TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(left, TEXT_WIDTH, BIN_WIDTH, bin_height);
left += BIN_WIDTH;
}
svg_end();
} else {
for (std::size_t i = 0; i < bins.size(); i++) {
double bin_height = MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
svg_text(left, TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(left, TEXT_WIDTH, BIN_WIDTH, bin_height);
left += BIN_WIDTH;
}
svg_end();
}
}

@ -3,7 +3,7 @@
#include <vector> #include <vector>
void void show_histogram_svg(const std::vector<std::size_t>& bins);
show_histogram_svg(const std::vector<std::size_t>& bins); void show_histogram_svg_vertical(const std::vector<std::size_t>& bins);
#endif // SHOW_HISTOGRAM_SVG_H_INCLUDED #endif // SHOW_HISTOGRAM_SVG_H_INCLUDED

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