code: добавил 7 вариант
Этот коммит содержится в:
24
svg.cpp
24
svg.cpp
@@ -5,8 +5,24 @@
|
||||
#include <string>
|
||||
#include "svg.h"
|
||||
|
||||
|
||||
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
|
||||
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 BLACK = "black";
|
||||
const auto RED = "red";
|
||||
const auto GREEN = "green";
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
|
||||
|
||||
size_t x = sr(bins);
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user