Skip to content

Commit 59448de

Browse files
committed
Simple animated bubble sort visualization.
1 parent bdef2a1 commit 59448de

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

win32/N3888_RefImpl/sample_draw.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <random>
44
#include <vector>
55
#include <algorithm>
6+
#include <string>
67

78
using namespace std;
89
using namespace std::experimental::drawing;
@@ -38,27 +39,36 @@ vector<vector<int>> init_sort_steps(int count) {
3839
return result;
3940
}
4041

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;
4245
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);
4549
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;
4852
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);
5154
beginX = trunc((right - left) * 0.05);
52-
beginY = trunc((bottom - top) * 0.05);
55+
const double y = trunc((bottom - top) * 0.5);
5356
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();
6367
}
68+
timer += elapsedTimeInMilliseconds;
69+
timer = timer > phaseTime * (stepCount + 3) ? 0.0 : timer;
6470
}
71+
72+
//const auto radius = trunc(min(
73+
// ((right - left) * 0.8) / stepCount,
74+
// ((bottom - top) * 0.8) / elementCount) / 2.0);

0 commit comments

Comments
 (0)