added some more random number utilities

This commit is contained in:
Bob Polis 2020-11-16 10:39:58 +01:00
parent c3935312c4
commit 2e0d3a79f0
2 changed files with 27 additions and 14 deletions

View File

@ -25,27 +25,33 @@ void ScreensaverPlugin::make_black() {
}
cairo_rectangle_t ScreensaverPlugin::random_rect() {
cairo_rectangle_t rect;
return random_rect_in_rect(_r);
}
cairo_rectangle_t ScreensaverPlugin::random_rect_in_rect(const cairo_rectangle_t& rect) {
cairo_rectangle_t r;
std::uniform_real_distribution<double> dx {rect.x, rect.width};
std::uniform_real_distribution<double> dy {rect.y, rect.height};
double v1, v2;
v1 = random_x();
v2 = random_x();
v1 = dx(_eng);
v2 = dx(_eng);
if (v1 < v2) {
rect.x = v1;
rect.width = v2 - v1;
r.x = v1;
r.width = v2 - v1;
} else {
rect.x = v2;
rect.width = v1 - v2;
r.x = v2;
r.width = v1 - v2;
}
v1 = random_y();
v2 = random_y();
v1 = dy(_eng);
v2 = dy(_eng);
if (v1 < v2) {
rect.y = v1;
rect.height = v2 - v1;
r.y = v1;
r.height = v2 - v1;
} else {
rect.y = v2;
rect.height = v1 - v2;
r.y = v2;
r.height = v1 - v2;
}
return rect;
return r;
}
double ScreensaverPlugin::random_x() {
@ -62,3 +68,8 @@ double ScreensaverPlugin::random01() {
std::uniform_real_distribution<double> dist {};
return dist(_eng);
}
double ScreensaverPlugin::random_between(double v1, double v2) {
std::uniform_real_distribution<double> dist {v1, v2};
return dist(_eng);
}

View File

@ -31,9 +31,11 @@ class ScreensaverPlugin {
void make_black();
cairo_rectangle_t random_rect();
cairo_rectangle_t random_rect_in_rect(const cairo_rectangle_t& rect);
double random_x();
double random_y();
double random01();
double random_between(double v1, double v2);
};
#endif // _ScreensaverPlugin_H_