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

master
ShchipkovMY 2 лет назад
Родитель 43853d7091
Сommit 7fc474ccfd

@ -5,8 +5,24 @@
#include <string> #include <string>
#include "svg.h" #include "svg.h"
using namespace std; using namespace std;
double sr(const vector<size_t>& bins) {
if (bins.empty()) {
return 0;
}
size_t totalHeight = 0;
for (size_t bin : bins) {
totalHeight += bin;
}
return static_cast<double>(totalHeight) / bins.size();
}
void void
svg_begin(double width, double height) svg_begin(double width, double height)
{ {
@ -50,8 +66,10 @@ show_histogram_svg(const vector<size_t>& bins)
const auto BLOCK_WIDTH = 10; const auto BLOCK_WIDTH = 10;
const auto BLACK = "black"; const auto BLACK = "black";
const auto RED = "red"; const auto RED = "red";
const auto GREEN = "green";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
size_t x = sr(bins);
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
@ -69,7 +87,13 @@ show_histogram_svg(const vector<size_t>& bins)
{ {
double bin_width = (MAX_WIDTH)*(bin/max_count); double bin_width = (MAX_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
if (bin_width<=MAX_WIDTH/max_count*x)
{
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, GREEN);
}
else{
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED);
}
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }

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