ArtyushinaVV 2 лет назад
Родитель 4efaa7d490
Сommit 560d928711

@ -4,19 +4,22 @@
#include "histogram.h" #include "histogram.h"
using namespace std; using namespace std;
void bool
find_minmax(const vector<double> &numbers, double& min, double& max) find_minmax(const vector<double>& numbers, double& min, double& max)
{ {
if (numbers.size() == 0) {
min = numbers[0]; return false;
}
else {
max = numbers[0]; max = numbers[0];
for (size_t i = 0; i < numbers.size(); i++) min = numbers[0];
}
for(size_t i = 0; i < numbers.size(); i++)
{ {
if (numbers[i] > max) if (numbers[i] > max) max = numbers[i];
max = numbers[i]; if (numbers[i] < min) min = numbers[i];
if (numbers[i] < min)
min = numbers[i];
} }
return true;
} }
vector<size_t> make_histogram (const vector<double>& numbers, size_t &bin_count) vector<size_t> make_histogram (const vector<double>& numbers, size_t &bin_count)
@ -26,7 +29,9 @@ vector<size_t> make_histogram (const vector<double>& numbers, size_t &bin_count)
double min, max; double min, max;
float low, hi; float low, hi;
find_minmax(numbers, min, max); if (!find_minmax(numbers, min, max))
cout << "error in find_minmax: empty vector";
double bin_size = (max - min) / bin_count; double bin_size = (max - min) / bin_count;
low = min; low = min;
@ -34,19 +39,19 @@ vector<size_t> make_histogram (const vector<double>& numbers, size_t &bin_count)
for (size_t i = 0; i < numbers.size(); i++) for (size_t i = 0; i < numbers.size(); i++)
{ {
bool found = false; bool flag = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++) for (size_t j = 0; (j < bin_count - 1) && !flag; j++)
{ {
low = min + j * bin_size; low = min + j * bin_size;
hi = min + (j + 1) * bin_size; hi = min + (j + 1) * bin_size;
if ((low <= numbers[i]) && (numbers[i] < hi)) if ((low <= numbers[i]) && (numbers[i] < hi))
{ {
bins[j]++; bins[j]++;
found = true; flag = true;
} }
} }
if (found==false) if (flag==false)
{ {
bins[bin_count - 1]++; bins[bin_count - 1]++;
} }

@ -1,5 +1,5 @@
#pragma once #pragma once
#include <vector> #include <vector>
std::vector<size_t> ;std::vector<size_t>
make_histogram(const std::vector<double> &numbers, size_t &bin_count); make_histogram(const std::vector<double> &numbers, size_t &bin_count);

@ -0,0 +1,5 @@
#pragma once
#include <vector>
bool
find_minmax(const std::vector<double> &numbers, double& min, double& max)

@ -29,8 +29,16 @@ cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"'
} }
void color_find(double bin, int maxb, int& color){
color = (10 - (bin * 9) / maxb);
if (color < 0) { cout << "cant take color";
color = 9;
}
}
void void
show_histogram_svg(const vector<size_t>& bins) { show_histogram_svg(const vector<size_t>& bins) {
const auto IMAGE_WIDTH = 400; const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300; const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 20; const auto TEXT_LEFT = 20;
@ -38,23 +46,37 @@ show_histogram_svg(const vector<size_t>& bins) {
const auto TEXT_WIDTH = 50; const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30; const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10; const auto BLOCK_WIDTH = 10;
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(400, 300);
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0; double top = 0;
double max_count = bins[0]; const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH;
for (size_t i = 0; i < bins.size(); i++) { int maxb = bins[0];
if (bins[i] > max_count) int color;
max_count = bins[i];
for (size_t j = 1; j < bins.size(); j++)
{
if (bins[j] > maxb) maxb = bins[j];
} }
int maxb1 = maxb * BLOCK_WIDTH;
for (size_t bin : bins) { for (size_t bin : bins) {
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH)*(bin/max_count);
double bin_width;
color_find(bin, maxb, color);
if (maxb1 > MAX_ASTERISK)
{
bin_width = BLOCK_WIDTH * MAX_ASTERISK * (bin / maxb1);
}
else bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "red", "#aaffaa"); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "violet", to_string(color));
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
svg_end(); svg_end();
} }

10
svg.h

@ -1,12 +1,22 @@
#pragma once #pragma once
#include <vector> #include <vector>
using namespace std; using namespace std;
void
color_find(double bin, int maxb, int& color);
void void
svg_begin(double width, double height); svg_begin(double width, double height);
void void
svg_end(); svg_end();
void
svg_text(double left, double baseline, string text);
void
svg_rect(double x, double y, double width, double height, string stroke, string fill);
void void
show_histogram_svg(const vector<size_t>& bins); show_histogram_svg(const vector<size_t>& bins);

@ -8,32 +8,29 @@
TEST_CASE("distinct positive numbers") { TEST_CASE("distinct positive numbers") {
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({1, 2}, min, max); CHECK(find_minmax({}, min, max) == false);
CHECK(min == 1);
CHECK(max == 2);
} }
TEST_CASE("distinct identical numbers") {
TEST_CASE("unit vector") {
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({1}, min, max); find_minmax({5, 5}, min, max);
CHECK(min == 1); CHECK(min == 5);
CHECK(max == 1); CHECK(max == 5);
} }
TEST_CASE("distinct negative numbers") { TEST_CASE("distinct one number") {
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({-1,-2}, min, max); find_minmax({3}, min, max);
CHECK(min == -2); CHECK(min == 3);
CHECK(max == -1); CHECK(max == 3);
} }
TEST_CASE("same nambers") { TEST_CASE("distinct negative numbers") {
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({1,1}, min, max); find_minmax({-3, -1}, min, max);
CHECK(min == 1); CHECK(min == -3);
CHECK(max == 1); CHECK(max == -1);
} }

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