TupikovAA 5 дней назад
Родитель b086795769
Сommit 574585c9e0

@ -1,9 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?> <?xml version='1.0' encoding='UTF-8'?>
<svg width='400' height='300' viewBox='0 0 400 300' xmlns='http://www.w3.org/2000/svg'> <svg width='400' height='300' viewBox='0 0 400 300' xmlns='http://www.w3.org/2000/svg'>
<text x='20' y='20'>4</text> <text x='20' y='35' font-size='14' fill='black'>1</text>
<rect x='50' y='0' width='155' height='30' stroke='black' fill='#aaffaa' /> <rect x='50' y='0' width='83.3333' height='30' stroke='#333333' fill='#4CAF50' />
<text x='20' y='50'>1</text> <line x1='50' y1='15' x2='133.333' y2='15' stroke='white' stroke-width='3' stroke-dasharray='20,7' />
<rect x='50' y='30' width='38.75' height='30' stroke='black' fill='#aaffaa' /> <text x='20' y='75' font-size='14' fill='black'>3</text>
<text x='20' y='80'>1</text> <rect x='50' y='40' width='250' height='30' stroke='#333333' fill='#4CAF50' />
<rect x='50' y='60' width='38.75' height='30' stroke='black' fill='#aaffaa' /> <line x1='50' y1='55' x2='300' y2='55' stroke='white' stroke-width='3' stroke-dasharray='20,7' />
<text x='20' y='115' font-size='14' fill='black'>1</text>
<rect x='50' y='80' width='83.3333' height='30' stroke='#333333' fill='#4CAF50' />
<line x1='50' y1='95' x2='133.333' y2='95' stroke='white' stroke-width='3' stroke-dasharray='20,7' />
<rect x='45' y='0' width='260' height='120' fill='none' stroke='black' stroke-width='2'/>
</svg> </svg>

До

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

После

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

@ -1,26 +1,25 @@
# depslib dependency file v1.0 # depslib dependency file v1.0
1750444036 source:c:\users\taa41\desktop\ïðîãà\lab1\main.cpp 1750586188 source:c:\users\taa41\desktop\ïðîãà\lab1\main.cpp
<iostream> <iostream>
<fstream>
"histogram.h" "histogram.h"
"svg.h" "svg.h"
<fstream>
1749173822 c:\users\taa41\desktop\ïðîãà\lab1\histogram.h 1749173822 c:\users\taa41\desktop\ïðîãà\lab1\histogram.h
<vector> <vector>
1750443911 source:c:\users\taa41\desktop\ïðîãà\lab1\svg.cpp 1750586035 source:c:\users\taa41\desktop\ïðîãà\lab1\svg.cpp
"svg.h" "svg.h"
<algorithm> <algorithm>
<string> <string>
<iostream>
1750442384 c:\users\taa41\desktop\ïðîãà\lab1\svg.h 1750586140 c:\users\taa41\desktop\ïðîãà\lab1\svg.h
<vector> <vector>
<string> <string>
<iostream> <iostream>
<fstream> <fstream>
1750443947 source:c:\users\taa41\desktop\ïðîãà\lab1\histogram.cpp 1750444430 source:c:\users\taa41\desktop\ïðîãà\lab1\histogram.cpp
<iostream> <iostream>
"histogram.h" "histogram.h"
"histogram_internal.h" "histogram_internal.h"

@ -2,29 +2,29 @@
<CodeBlocks_layout_file> <CodeBlocks_layout_file>
<FileVersion major="1" minor="0" /> <FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" /> <ActiveTarget name="Debug" />
<File name="histogram.cpp" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="svg.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="-1" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="289" topLine="0" /> <Cursor1 position="2445" topLine="53" />
</Cursor> </Cursor>
</File> </File>
<File name="main.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="histogram.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="142" topLine="0" /> <Cursor1 position="168" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="svg.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="-2" zoom_2="0"> <File name="svg.h" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="1151" topLine="0" /> <Cursor1 position="862" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="svg.h" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="main.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="671" topLine="0" /> <Cursor1 position="81" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="histogram.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="histogram.cpp" open="1" top="1" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="168" topLine="0" /> <Cursor1 position="1932" topLine="56" />
</Cursor> </Cursor>
</File> </File>
</CodeBlocks_layout_file> </CodeBlocks_layout_file>

@ -1,24 +1,25 @@
#include <iostream> #include <iostream>
#include <fstream>
#include "histogram.h" #include "histogram.h"
#include "svg.h" #include "svg.h"
#include <fstream>
using namespace std; using namespace std;
int main() { int main() {
Input data = input_data(); Input data = input_data();
size_t block_width = svg::input_block_width();
auto bins = make_histogram(data.numbers, data.bin_count); auto bins = make_histogram(data.numbers, data.bin_count);
// Çàïðîñ ïàðàìåòðîâ ïóíêòèðà
int dash_len, gap_len;
cout << "Enter dash length (e.g., 20): ";
cin >> dash_len;
cout << "Enter gap length (e.g., 10): ";
cin >> gap_len;
show_histogram_text(bins, block_width); show_histogram_text(bins);
ofstream svg_file("histogram.svg"); ofstream svg_file("histogram.svg");
svg::show_histogram_svg(svg_file, bins, block_width); svg::show_histogram_svg(svg_file, bins, dash_len, gap_len);
svg_file.close(); svg_file.close();
return 0; return 0;
} }

@ -1,34 +1,10 @@
#include "svg.h" #include "svg.h"
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <iostream>
using namespace std; using namespace std;
namespace svg { 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) { void begin(ostream& out, double width, double height) {
out << "<?xml version='1.0' encoding='UTF-8'?>\n"; out << "<?xml version='1.0' encoding='UTF-8'?>\n";
out << "<svg width='" << width << "' height='" << height << "' " out << "<svg width='" << width << "' height='" << height << "' "
@ -51,7 +27,15 @@ namespace svg {
<< "' fill='" << fill << "' />\n"; << "' fill='" << fill << "' />\n";
} }
void show_histogram_svg(ofstream& out, const vector<size_t>& bins, size_t block_width) { void line(ostream& out, double x1, double y1, double x2, double y2,
const string& stroke, int dash_length, int gap_length) {
out << "<line x1='" << x1 << "' y1='" << y1 << "' "
<< "x2='" << x2 << "' y2='" << y2 << "' "
<< "stroke='" << stroke << "' stroke-width='2' "
<< "stroke-dasharray='" << dash_length << " " << gap_length << "' />\n";
}
void show_histogram_svg(ofstream& out, const vector<size_t>& bins, int dash_length, int gap_length) {
begin(out, IMAGE_WIDTH, IMAGE_HEIGHT); begin(out, IMAGE_WIDTH, IMAGE_HEIGHT);
if (bins.empty()) { if (bins.empty()) {
@ -59,29 +43,40 @@ void show_histogram_svg(ofstream& out, const vector<size_t>& bins, size_t block_
return; return;
} }
const size_t max_count = *max_element(bins.begin(), bins.end()); size_t max_count = *max_element(bins.begin(), bins.end());
if (max_count == 0) { if (max_count == 0) {
end(out); end(out);
return; return;
} }
// Óâåëè÷èâàåì ìàêñèìàëüíóþ øèðèíó äëÿ ëó÷øåãî îòîáðàæåíèÿ
const double max_svg_width = IMAGE_WIDTH - TEXT_WIDTH - 40; double max_width = IMAGE_WIDTH - TEXT_WIDTH - 100;
double scale = max_width / max_count;
const double scale = max_svg_width / max_count;
double top = 0; double top = 0;
for (size_t bin : bins) { for (size_t bin : bins) {
double width = bin * scale;
// Ïîäïèñü çíà÷åíèÿ (êðóïíåå è ÷åò÷å)
out << "<text x='" << TEXT_LEFT << "' y='" << top + TEXT_BASELINE + 15
<< "' font-size='14' fill='black'>" << bin << "</text>\n";
double width = bin * scale * (block_width / 10.0); // Îñíîâíîé ñòîëáåö
rect(out, TEXT_WIDTH, top, width, BIN_HEIGHT, "#333333", "#4CAF50");
text(out, TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); // Ïóíêòèðíàÿ ëèíèÿ (òîëùå è êîíòðàñòíåå)
rect(out, TEXT_WIDTH, top, width, BIN_HEIGHT, "black", "#aaffaa"); out << "<line x1='" << TEXT_WIDTH << "' y1='" << top + BIN_HEIGHT/2
<< "' x2='" << TEXT_WIDTH + width << "' y2='" << top + BIN_HEIGHT/2
<< "' stroke='white' stroke-width='3' "
<< "stroke-dasharray='" << dash_length << "," << gap_length << "' />\n";
top += BIN_HEIGHT; top += BIN_HEIGHT + 10; // Äîáàâëÿåì îòñòóï ìåæäó ñòîëáöàìè
} }
// Äîáàâëÿåì ðàìêó âîêðóã âñåé ãèñòîãðàììû
out << "<rect x='" << TEXT_WIDTH-5 << "' y='0' width='" << max_width+10
<< "' height='" << top << "' fill='none' stroke='black' stroke-width='2'/>\n";
end(out); end(out);
} }
} }

@ -19,9 +19,8 @@ namespace svg {
void text(std::ostream& out, double left, double baseline, const std::string& text); void text(std::ostream& out, double left, double baseline, const std::string& text);
void rect(std::ostream& out, double x, double y, double width, double height, void rect(std::ostream& out, double x, double y, double width, double height,
const std::string& stroke = "black", const std::string& fill = "#dddddd"); const std::string& stroke = "black", const std::string& fill = "#dddddd");
void show_histogram_svg(std::ofstream& out, const std::vector<size_t>& bins, size_t block_width); void line(std::ostream& out, double x1, double y1, double x2, double y2,
size_t input_block_width(); const std::string& stroke, int dash_length, int gap_length);
void set_block_width(size_t width); void show_histogram_svg(std::ofstream& out, const std::vector<size_t>& bins, int dash_length, int gap_length);
} }
#endif #endif

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