SukhotinMD 3 недель назад
Родитель ec765e16b4
Сommit 538508ef98

@ -1,5 +1,9 @@
<?xml version='1.0' encoding='UTF-8'?>
<svg width='400' height='300' viewBox='0 0 400 300' xmlns='http://www.w3.org/2000/svg'>
<text x='20' y='35'>2</text>
<rect x='50' y='0' width='20' height='30' stroke = 'purple' fill='red' />
<text x='20' y='20'>2</text>
<rect x='50' y='0' width='140' height='30' stroke='grey' fill='#319c28' />
<text x='20' y='50'>5</text>
<rect x='50' y='30' width='350' height='30' stroke='grey' fill='#7d79c6' />
<text x='20' y='80'>3</text>
<rect x='50' y='60' width='210' height='30' stroke='grey' fill='#5e7341' />
</svg>

До

Ширина:  |  Высота:  |  Размер: 237 B

После

Ширина:  |  Высота:  |  Размер: 448 B

@ -1,6 +1,9 @@
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <sstream>
#include <iomanip>
#include "text.h"
#include "histogram.h"
@ -9,6 +12,25 @@
using namespace std;
std::string getRandomHexColor() {
random_device rd; // Èñòî÷íèê ñëó÷àéíûõ ÷èñåë
mt19937 gen(rd()); // Ãåíåðàòîð Mersenne Twister
std::uniform_int_distribution<> dis(0, 255); // Ðàâíîìåðíîå ðàñïðåäåëåíèå [0, 255]
int r = dis(gen); // Êðàñíûé
int g = dis(gen); // Çåëåíûé
int b = dis(gen); // Ñèíèé
// Ôîðìèðóåì HEX-ñòðîêó
std::stringstream hexStream;
hexStream << "#"
<< std::hex << std::setw(2) << std::setfill('0') << r
<< std::hex << std::setw(2) << std::setfill('0') << g
<< std::hex << std::setw(2) << std::setfill('0') << b;
return hexStream.str();
}
void
svg_begin(double width, double height) {
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
@ -29,97 +51,46 @@ svg_text(double left, double baseline, string text) {
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>" << endl;
}
void svg_rect(double x, double y, double width, double height, string stroke, string fill) {
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke = '" << stroke << "' fill='" << fill << "' />" << endl;
void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") {
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />" << endl;
}
void
show_histogram_svg(const vector<size_t> bins) {
auto mm = *(max_element(bins.begin(), bins.end()));
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;
auto BLOCK_WIDTH = 10;
BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH) / mm;
svg_begin(400, 300);
svg_text(20, 35, to_string(bins[0]));
svg_rect(50, 0, bins[0] * 10, 30, "purple", "red");
svg_end();
}
void show_histogram_text(const vector <size_t> bins, const size_t bin_count) {
// Ìàêñèìàëüíîå â êîîðçèíàõ
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int max_in_bins = 0;
for (int i = 0; i < bin_count; i++) {
if (bins[i] > max_in_bins) {
max_in_bins = bins[i];
}
}
if (max_in_bins > MAX_ASTERISK) {
for (int i = 0; i < bin_count; i++) {
if (bins[i] < 100) {
cout << " ";
}
if (bins[i] < 10) {
cout << " ";
}
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_in_bins);
cout << bins[i] << "|";
for (int j = 0; j < height; j++) {
cout << "*";
}
cout << endl;
}
double top = 0;
for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "grey", getRandomHexColor());
top += BIN_HEIGHT;
}
else {
// Åñëè ìàêñèìàëüíîå ÷èñëî ñðåäè êîîðçèí ìåíüøå èëè ðàâíî 76
for (int i = 0; i < bin_count; i++) {
if (bins[i] < 100) {
cout << " ";
}
if (bins[i] < 10) {
cout << " ";
}
cout << bins[i] << "|";
for (int j = 0; j < bins[i]; j++) {
cout << "*";
}
cout << endl;
}
}
//svg_begin(400, 300);
//svg_text(20, 35, to_string(bins[0]));
//svg_rect(50, 0, bins[0] * 10, 30, "purple", "red");
svg_end();
}
}
Загрузка…
Отмена
Сохранить