Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

75 строки
1.9 KiB
C++

#include <iostream>
#include <vector>
#include "string"
#include "svg.h"
using namespace std;
void
svg_begin(double width, double height) {
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
cout << "<svg ";
cout << "width='" << width << "' ";
cout << "height='" << height << "' ";
cout << "viewBox='0 0 " << width << " " << height << "' ";
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void
svg_end() {
cout << "</svg>\n";
}
void
svg_text(double TEXT_LEFT, double BASELINE, string text, size_t bukva_scale) {
cout << "<text x='" << TEXT_LEFT << "' font-size = '" << bukva_scale << "' y='"<< BASELINE <<"'> " << text << "</text>";
}
void svg_rect(double TEXT_WIDTH, double top, double bin_width, double BIN_HEIGHT, string grad){
cout << "<rect x='"<<TEXT_WIDTH<<"' y='"<<top<<"' width='"<<bin_width<<"' height='"<<BIN_HEIGHT<<"' stroke ='cyan' fill='#"<<grad<<grad<<grad<<"' />\n";
}
void
show_histogram_svg(const 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;
const double SCALE = IMAGE_WIDTH - TEXT_WIDTH;
size_t grad = 0;
double max_count = 0;
size_t bukva_scale = 12;
cin >> bukva_scale;
for (double x: bins) {
if (x > max_count) {
max_count = x;
}
}
svg_begin(800,300);
double top = 0;
for (size_t i = 0; i < bins.size(); i++) {
const double bin_width = SCALE * ( bins[i] / max_count);
grad = (11 - (bins[i] * 9) / max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i]), bukva_scale);
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, to_string(grad));
top += BIN_HEIGHT;
cout << endl;
cout << bin_width;
}
svg_end();
}