Skip to content

Commit 28586ff

Browse files
committed
Interpolated motion animated bubble sort visualization.
1 parent 59448de commit 28586ff

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

win32/N3888_RefImpl/sample_draw.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ vector<vector<int>> init_sort_steps(int count) {
1515
for (int i = 0; i < count; ++i) {
1616
init.push_back(i);
1717
}
18-
mt19937 rng(100);
18+
mt19937 rng(1009);
1919
shuffle(begin(init), end(init), rng);
2020
return init;
2121
}());
@@ -38,10 +38,15 @@ vector<vector<int>> init_sort_steps(int count) {
3838
}
3939
return result;
4040
}
41-
41+
// TODO: Lerp between phases with a pause at the end before beginning the next phase. auto it_index = it - begin(vec);
4242
void sample_draw::operator()(context& ctxt, double elapsedTimeInMilliseconds) {
4343
static double timer = 0.0;
44-
const double phaseTime = 1250.0;
44+
const double power = 3.0;
45+
const double lerpTime = 1500.0;
46+
const double phaseTime = lerpTime + 500.0;
47+
const double normalizedTime = min(fmod(timer, phaseTime) / lerpTime, 1.0);
48+
const double adjustment = (normalizedTime < 0.5) ? pow(normalizedTime * 2.0, power) / 2.0 :
49+
((1.0 - pow(1.0 - ((normalizedTime - 0.5) * 2.0), power)) * 0.5) + 0.5;
4550
const int elementCount = 10; // height
4651
const static auto vec = init_sort_steps(elementCount);
4752
const int stepCount = vec.size(); // width
@@ -60,15 +65,22 @@ void sample_draw::operator()(context& ctxt, double elapsedTimeInMilliseconds) {
6065
ctxt.set_font_size(40.0);
6166
ctxt.show_text(string("Phase ").append(to_string(x + 1)).c_str());
6267
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);
68+
const auto currVal = vec[x][i];
69+
if (x < stepCount - 1) {
70+
const auto bi = find(begin(vec[x + 1]), end(vec[x + 1]), currVal) - begin(vec[x + 1]);
71+
const auto ax = radius * i * 2.0 + radius + beginX + (4.0 * i);
72+
const auto bx = radius * bi * 2.0 + radius + beginX + (4.0 * bi);
73+
const auto yr = y - ((bi == i ? 0.0 : (radius * 4.0 * (normalizedTime < 0.5 ? normalizedTime : 1.0 - normalizedTime)))
74+
* (i % 2 == 1 ? 1.0 : -1.0));
75+
ctxt.arc((bx - ax) * adjustment + ax, yr, radius, 0.0, two_pi);
76+
}
77+
else {
78+
ctxt.arc(radius * i * 2.0 + radius + beginX + (4.0 * i), y, radius, 0.0, two_pi);
79+
}
6480
double greyColor = 1.0 - (vec[x][i] / (elementCount - 1.0));
6581
ctxt.set_source_rgb(greyColor, greyColor, greyColor);
6682
ctxt.fill();
6783
}
6884
timer += elapsedTimeInMilliseconds;
69-
timer = timer > phaseTime * (stepCount + 3) ? 0.0 : timer;
85+
timer = (timer > phaseTime * (stepCount + 3)) ? 0.0 : timer;
7086
}
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)