Родитель
62efa8240e
Сommit
8f1c0dc2ae
@ -1,46 +1,43 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min){
|
||||
min = x;
|
||||
}
|
||||
else if (x > max){
|
||||
max = x;
|
||||
#include "histogram_internal.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
max = 0;
|
||||
if (numbers.size() != 0) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (auto el : numbers) {
|
||||
if (el > max) {
|
||||
max = el;
|
||||
}
|
||||
if (el < min) {
|
||||
min = el;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {min = 0;}
|
||||
}
|
||||
|
||||
|
||||
std::vector <size_t> make_histogram(const std::vector<double>&numbers, size_t &bin_count, size_t &number_count, size_t &max_count) {
|
||||
double min, max;
|
||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
||||
vector<size_t> bins(bin_count);
|
||||
double max, min;
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
std::vector<size_t> bins(bin_count);
|
||||
max_count = bins[0];
|
||||
for (size_t i = 0; i < number_count; i++){
|
||||
double binSize = (max - min) / bin_count;
|
||||
for (size_t i = 0; i < numbers.size(); i++) {
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++){
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)){
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
||||
auto lo = min + j * binSize;
|
||||
auto hi = min + (j + 1) * binSize;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
if (bins[j] > max_count){
|
||||
max_count = bins[j];
|
||||
}
|
||||
}
|
||||
if (!found){
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
if (bins[bin_count - 1] > max_count){
|
||||
max_count = bins[bin_count - 1];
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>&numbers, size_t & bin_count, size_t & number_count, size_t & max_count);
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
|
@ -1,77 +1,61 @@
|
||||
#include "iostream"
|
||||
#include <iostream>
|
||||
#include "svg.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
const auto IMAGE_WIDTH = 400;
|
||||
const auto IMAGE_HEIGHT = 300;
|
||||
const auto IMAGE_HEIGHT = 310;
|
||||
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;
|
||||
|
||||
void svg_begin(double width, double height, std::vector <double>& test)
|
||||
{
|
||||
test.push_back(width);
|
||||
test.push_back(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_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";
|
||||
cout << "<line x1='10' y1='2' x2='" << width + 10 <<"' y2='2' stroke-dasharray = '10 10' stroke='black'/>\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 = "red")
|
||||
{
|
||||
std::cout<< "<rect x='"<< x <<"' y='"<< y <<"' width='"<< width <<"' height='"<< height<<"' stroke='black' fill='red' ></rect>";
|
||||
void svg_end(double top, ostream& stream) {
|
||||
stream << "<line x1='10' y1='" << top << "' x2='" << IMAGE_WIDTH <<"' y2='" << top << "' stroke-dasharray = '10 10' stroke='black'/>\n";
|
||||
stream << "</svg>\n";
|
||||
}
|
||||
|
||||
void svg_line(double x, double y, double x2, double y2, std::vector <double>& test)
|
||||
{
|
||||
test.push_back(x);
|
||||
test.push_back(y);
|
||||
test.push_back(x2);
|
||||
test.push_back(y2);
|
||||
std::cout<<"<line x1='"<< x <<"' y1='"<< y <<"' x2='"<< x2 <<"' y2='"<< y2 <<"' stroke-dasharray = '10 10' stroke='black';stroke-width:4' />";
|
||||
void svg_text(double left, double baseline, string text) {
|
||||
cout << "<line x1='10' y1='" << baseline - TEXT_BASELINE << "' x2='10' y2='" << baseline <<"' stroke-dasharray = '10 10' stroke='black'/>\n";
|
||||
cout << "<text x='" << left << "' y='" << baseline << "'>" << text <<"</text>\n";
|
||||
}
|
||||
|
||||
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 << "' stroke='" << stroke << "' height='" << height << "' fill='" << fill << "' />\n";
|
||||
cout << "<line x1='" << IMAGE_WIDTH <<"' y1='" << y << "' x2='" << IMAGE_WIDTH <<"' y2='" << y + height <<"' stroke-dasharray = '10 10' stroke='black'/>\n";
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
show_histogram_svg(const std::vector<size_t>& bins, size_t & max_count, size_t & bin_count, std::vector <double>& test)
|
||||
{
|
||||
|
||||
const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH)/max_count;
|
||||
svg_begin(IMAGE_WIDTH+4, IMAGE_HEIGHT, test);
|
||||
double top = 4;
|
||||
|
||||
svg_line(TEXT_LEFT, top, IMAGE_WIDTH+5, top, test);
|
||||
svg_line(TEXT_LEFT-10, top, TEXT_LEFT-10, top+3*BIN_HEIGHT, test);
|
||||
svg_line(IMAGE_WIDTH+5, top, IMAGE_WIDTH+5, 2*top+3*BIN_HEIGHT, test);
|
||||
svg_line(TEXT_LEFT, 2*top+3*BIN_HEIGHT, IMAGE_WIDTH+5, 2*top+3*BIN_HEIGHT, test);
|
||||
for (double t : test)
|
||||
{
|
||||
std:: cout << t<< '\n';
|
||||
void show_histogram_svg(const vector<size_t>& bins) {
|
||||
double max = 0, scale = 1;
|
||||
int maxlen = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
for (auto el: bins) {
|
||||
if (max < el) {
|
||||
max = el;
|
||||
}
|
||||
}
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
double top = 5;
|
||||
scale = max * BLOCK_WIDTH / maxlen;
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = BLOCK_WIDTH * bin;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
|
||||
svg_rect(TEXT_WIDTH+2, top+2, bin_width, BIN_HEIGHT);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width / scale, BIN_HEIGHT);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
svg_end()
|
||||
{
|
||||
std::cout << "</svg>\n";
|
||||
svg_end(top, cout);
|
||||
}
|
||||
|
@ -1,10 +1,3 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void show_histogram_svg(const std::vector<size_t>& bins, size_t & max_count, size_t & bin_count, std::vector <double>& test);
|
||||
void svg_begin(double width, double height, std::vector <double>& test);
|
||||
void svg_text(double left, double baseline, std::string text);
|
||||
void svg_rect(double x, double y, double width, double height, std::string stroke, std::string fill);
|
||||
void svg_line(double x, double y, double width, double height, std::vector <double>& test);
|
||||
void svg_end();
|
||||
void show_histogram_svg(const std::vector<size_t>& bins);
|
||||
|
@ -1,34 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "text.h"
|
||||
|
||||
void show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count){
|
||||
using namespace std;
|
||||
|
||||
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
if (max_count > MAX_ASTERISK){
|
||||
std::vector<size_t> hights(bin_count);
|
||||
for (size_t i = 0; i < bin_count; i++){
|
||||
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||
hights[i] = height;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < bin_count; i++){
|
||||
printf("%3d|", bins[i]);
|
||||
for (size_t j = 0; j < hights[i]; j++){
|
||||
std::cout<<"*";
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
void show_histogram_text(const vector<size_t>& bins) {
|
||||
size_t maxCount = bins[0];
|
||||
for (auto bin : bins) {
|
||||
if (maxCount < bin) {
|
||||
maxCount = bin;
|
||||
}
|
||||
}
|
||||
else{
|
||||
for (size_t i = 0; i < bin_count; i++)
|
||||
for (auto bin : bins) {
|
||||
if (bin < 100)
|
||||
{
|
||||
printf("%3d|", bins[i]);
|
||||
for (size_t j = 0; j < bins[i]; j++){
|
||||
std::cout<<"*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
cout << " ";
|
||||
}
|
||||
if (bin < 10)
|
||||
{
|
||||
cout << " ";
|
||||
}
|
||||
size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast<double>(bin) / maxCount);
|
||||
cout << bin << "|" << string(height, '*');
|
||||
cout << "\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1 +1,4 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
void show_histogram_text(const std::vector<size_t>& bins);
|
||||
|
Загрузка…
Ссылка в новой задаче