diff --git a/src/ScreensaverPlugin.cpp b/src/ScreensaverPlugin.cpp index 3b6ad4b..4b17fe9 100644 --- a/src/ScreensaverPlugin.cpp +++ b/src/ScreensaverPlugin.cpp @@ -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 dx {rect.x, rect.width}; + std::uniform_real_distribution 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 dist {}; return dist(_eng); } + +double ScreensaverPlugin::random_between(double v1, double v2) { + std::uniform_real_distribution dist {v1, v2}; + return dist(_eng); +} diff --git a/src/ScreensaverPlugin.hpp b/src/ScreensaverPlugin.hpp index 8f77403..84a67ee 100644 --- a/src/ScreensaverPlugin.hpp +++ b/src/ScreensaverPlugin.hpp @@ -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_