GladkyMS 1 год назад
Родитель 903f95582d
Сommit f32bc01b85

@ -0,0 +1,57 @@
#include "svg.h"
#include <iostream>
#include<string>
void svg_begin(double width, double height) {
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
std::cout << "<svg ";
std::cout << "width='" << width << "' ";
std::cout << "height='" << height << "' ";
std::cout << "viewBox='0 0 " << width << " " << height << "' ";
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void svg_end() {
std::cout << "</svg>\n";
}
void
svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='" << baseline << "'>"<<text<<"</text>";
}
void svg_rect(double x, double y, double width, double height,std::string stroke = "black", std::string fill = "black") {
std::cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"'/>";
}
void show_histogram_svg(const std::vector<size_t>& bins, size_t bin_count) {
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;
double mxbins = bins[0];
for (double x : bins)
{
if (x > mxbins)
mxbins = x;
}
double k;
if (mxbins > IMAGE_WIDTH-TEXT_WIDTH)
k = floor((IMAGE_WIDTH - TEXT_WIDTH) / mxbins);
else
k = 1;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins) {
std::string color = "#" + std::to_string(bin*100);
const double bin_width = BLOCK_WIDTH * bin*k;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,color,color);
top += BIN_HEIGHT;
}
svg_end();
}

@ -0,0 +1,4 @@
#pragma once
#include <vector>
void show_histogram_svg(const std::vector<size_t>& bins, size_t bin_count);

@ -3,6 +3,7 @@
#include<cmath> #include<cmath>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h"
const size_t SCREEN_WIDTH = 80; const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
using namespace std; using namespace std;
@ -34,5 +35,5 @@ int main()
{ {
Input in = input_data(); Input in = input_data();
vector <size_t> bins = make_histogram(in.numbers, in.bin_count); vector <size_t> bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, in.bin_count); show_histogram_svg(bins, in.bin_count);
} }

@ -128,12 +128,14 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="histogram.cpp" /> <ClCompile Include="histogram.cpp" />
<ClCompile Include="svg.cpp" />
<ClCompile Include="text.cpp" /> <ClCompile Include="text.cpp" />
<ClCompile Include="vector.cpp" /> <ClCompile Include="vector.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="histogram.h" /> <ClInclude Include="histogram.h" />
<ClInclude Include="histogram_internal.h" /> <ClInclude Include="histogram_internal.h" />
<ClInclude Include="svg.h" />
<ClInclude Include="text.h" /> <ClInclude Include="text.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

@ -24,6 +24,9 @@
<ClCompile Include="text.cpp"> <ClCompile Include="text.cpp">
<Filter>Исходные файлы</Filter> <Filter>Исходные файлы</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="svg.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="histogram.h"> <ClInclude Include="histogram.h">
@ -35,5 +38,8 @@
<ClInclude Include="histogram_internal.h"> <ClInclude Include="histogram_internal.h">
<Filter>Файлы заголовков</Filter> <Filter>Файлы заголовков</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="svg.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
Загрузка…
Отмена
Сохранить