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

88 строки
2.4 KiB
C++

#include "svg.h"
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
namespace svg {
size_t BLOCK_WIDTH = 10;
size_t input_block_width() {
size_t width;
while (true) {
cout << "Ââåäèòå øèðèíó áëîêà ãèñòîãðàììû (3-30px): ";
cin >> width;
if (width < 3) {
cout << "Øèðèíà ñëèøêîì ìàëà. Ìèíèìàëüíîå çíà÷åíèå: 3px. Ïîæàëóéñòà, ïîâòîðèòå ââîä.\n";
} else if (width > 30) {
cout << "Øèðèíà ñëèøêîì âåëèêà. Ìàêñèìàëüíîå çíà÷åíèå: 30px. Ïîæàëóéñòà, ïîâòîðèòå ââîä.\n";
} else {
break;
}
}
return width;
}
void set_block_width(size_t width) {
BLOCK_WIDTH = width;
}
void begin(ostream& out, double width, double height) {
out << "<?xml version='1.0' encoding='UTF-8'?>\n";
out << "<svg width='" << width << "' height='" << height << "' "
<< "viewBox='0 0 " << width << " " << height << "' "
<< "xmlns='http://www.w3.org/2000/svg'>\n";
}
void end(ostream& out) {
out << "</svg>\n";
}
void text(ostream& out, double left, double baseline, const string& text) {
out << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
}
void rect(ostream& out, double x, double y, double width, double height,
const string& stroke, const string& fill) {
out << "<rect x='" << x << "' y='" << y << "' width='" << width
<< "' height='" << height << "' stroke='" << stroke
<< "' fill='" << fill << "' />\n";
}
void show_histogram_svg(ofstream& out, const vector<size_t>& bins, size_t block_width) {
begin(out, IMAGE_WIDTH, IMAGE_HEIGHT);
if (bins.empty()) {
end(out);
return;
}
const size_t max_count = *max_element(bins.begin(), bins.end());
if (max_count == 0) {
end(out);
return;
}
const double max_svg_width = IMAGE_WIDTH - TEXT_WIDTH - 40;
const double scale = max_svg_width / max_count;
double top = 0;
for (size_t bin : bins) {
double width = bin * scale * (block_width / 10.0);
text(out, TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
rect(out, TEXT_WIDTH, top, width, BIN_HEIGHT, "black", "#aaffaa");
top += BIN_HEIGHT;
}
end(out);
}
}