|
3 | 3 | #include <random> |
4 | 4 | #include <vector> |
5 | 5 | #include <algorithm> |
| 6 | +#include <string> |
6 | 7 |
|
7 | 8 | using namespace std; |
8 | 9 | using namespace std::experimental::drawing; |
@@ -38,27 +39,36 @@ vector<vector<int>> init_sort_steps(int count) { |
38 | 39 | return result; |
39 | 40 | } |
40 | 41 |
|
41 | | -void sample_draw::operator()(::std::experimental::drawing::context& ctxt, double /*elapsedTimeInMilliseconds*/) { |
| 42 | +void sample_draw::operator()(context& ctxt, double elapsedTimeInMilliseconds) { |
| 43 | + static double timer = 0.0; |
| 44 | + const double phaseTime = 1250.0; |
42 | 45 | const int elementCount = 10; // height |
43 | | - static vector<vector<int>> vec = init_sort_steps(elementCount); |
44 | | - int stepCount = vec.size(); // width |
| 46 | + const static auto vec = init_sort_steps(elementCount); |
| 47 | + const int stepCount = vec.size(); // width |
| 48 | + const auto x = min(static_cast<int>(timer / phaseTime), stepCount - 1); |
45 | 49 | ctxt.set_source_rgb(0.392156899, 0.5843137503, 0.9294118285); |
46 | | - ctxt.paint(); |
47 | | - double left, top, right, bottom, unitWidth, unitHeight, beginX, beginY; |
| 50 | + ctxt.paint(); // Paint background. |
| 51 | + double left, top, right, bottom, beginX; |
48 | 52 | ctxt.clip_extents(left, top, right, bottom); |
49 | | - unitWidth = trunc(((right - left) * 0.8) / stepCount); |
50 | | - unitHeight = trunc(((bottom - top) * 0.8) / elementCount); |
| 53 | + const auto radius = trunc(min((right - left) * 0.8 / elementCount, (bottom - top) + 120.0) / 2.0); |
51 | 54 | beginX = trunc((right - left) * 0.05); |
52 | | - beginY = trunc((bottom - top) * 0.05); |
| 55 | + const double y = trunc((bottom - top) * 0.5); |
53 | 56 | const double two_pi = 3.1415926535897932 * 2.0; |
54 | | - for (int x = 0; x < stepCount; ++x) { |
55 | | - for (int y = 0; y < elementCount; ++y) { |
56 | | - //ctxt.move_to(beginX * x, beginY * y); |
57 | | - const auto radius = trunc(min(unitWidth, unitHeight) / 2.0); |
58 | | - ctxt.arc(radius * x * 2.0 + radius + beginX + (4.0 * x), radius * y * 2.0 + radius + beginY + (4.0 * y), radius, 0.0, two_pi); |
59 | | - double greyColor = 1.0 - (vec[x][y] / (elementCount - 1.0)); |
60 | | - ctxt.set_source_rgb(greyColor, greyColor, greyColor); |
61 | | - ctxt.fill(); |
62 | | - } |
| 57 | + ctxt.move_to(20.0, 50.0); |
| 58 | + ctxt.set_source_rgb(1.0, 1.0, 1.0); |
| 59 | + ctxt.select_font_face("Segoe UI", font_slant::normal, font_weight::normal); |
| 60 | + ctxt.set_font_size(40.0); |
| 61 | + ctxt.show_text(string("Phase ").append(to_string(x + 1)).c_str()); |
| 62 | + for (int i = 0; i < elementCount; ++i) { |
| 63 | + ctxt.arc(radius * i * 2.0 + radius + beginX + (4.0 * i), y, radius, 0.0, two_pi); |
| 64 | + double greyColor = 1.0 - (vec[x][i] / (elementCount - 1.0)); |
| 65 | + ctxt.set_source_rgb(greyColor, greyColor, greyColor); |
| 66 | + ctxt.fill(); |
63 | 67 | } |
| 68 | + timer += elapsedTimeInMilliseconds; |
| 69 | + timer = timer > phaseTime * (stepCount + 3) ? 0.0 : timer; |
64 | 70 | } |
| 71 | + |
| 72 | +//const auto radius = trunc(min( |
| 73 | +// ((right - left) * 0.8) / stepCount, |
| 74 | +// ((bottom - top) * 0.8) / elementCount) / 2.0); |
0 commit comments