88using namespace std ;
99using namespace std ::experimental::drawing;
1010
11- vector<vector<int >> init_sort_steps (int count) {
11+ vector<vector<int >> init_sort_steps (int count, unsigned long mtSeed = 1009UL ) {
1212 vector<vector<int >> result;
13- result.push_back ([count]() {
13+ result.push_back ([count, mtSeed ]() {
1414 vector<int > init;
1515 for (int i = 0 ; i < count; ++i) {
1616 init.push_back (i);
1717 }
18- mt19937 rng (1009 );
18+ mt19937 rng (mtSeed );
1919 shuffle (begin (init), end (init), rng);
2020 return init;
2121 }());
@@ -38,49 +38,42 @@ vector<vector<int>> init_sort_steps(int count) {
3838 }
3939 return result;
4040}
41- // TODO: Lerp between phases with a pause at the end before beginning the next phase. auto it_index = it - begin(vec);
41+
4242void sample_draw::operator ()(context& ctxt, double elapsedTimeInMilliseconds) {
4343 static double timer = 0.0 ;
44- const double power = 3.0 ;
45- const double lerpTime = 1500.0 ;
46- const double phaseTime = lerpTime + 500.0 ;
44+ const double power = 3.0 , lerpTime = 1250.0 , phaseTime = lerpTime + 500.0 , two_pi = 6.2831853071795864 ;
4745 const double normalizedTime = min (fmod (timer, phaseTime) / lerpTime, 1.0 );
4846 const double adjustment = (normalizedTime < 0.5 ) ? pow (normalizedTime * 2.0 , power) / 2.0 :
4947 ((1.0 - pow (1.0 - ((normalizedTime - 0.5 ) * 2.0 ), power)) * 0.5 ) + 0.5 ;
50- const int elementCount = 10 ; // height
48+ const int elementCount = 12 ;
5149 const static auto vec = init_sort_steps (elementCount);
52- const int stepCount = vec.size (); // width
53- const auto x = min (static_cast <int >(timer / phaseTime), stepCount - 1 );
50+ const int phaseCount = static_cast <int >(vec.size ()), x = min (static_cast <int >(timer / phaseTime), phaseCount - 1 );
5451 ctxt.set_source_rgb (0.392156899 , 0.5843137503 , 0.9294118285 );
5552 ctxt.paint (); // Paint background.
56- double left, top, right, bottom, beginX ;
53+ double left, top, right, bottom;
5754 ctxt.clip_extents (left, top, right, bottom);
58- const auto radius = trunc (min ((right - left) * 0.8 / elementCount, (bottom - top) + 120.0 ) / 2.0 );
59- beginX = trunc ((right - left) * 0.05 );
60- const double y = trunc ((bottom - top) * 0.5 );
61- const double two_pi = 3.1415926535897932 * 2.0 ;
62- ctxt.move_to (20.0 , 50.0 );
55+ const double radius = trunc (min ((right - left) * 0.8 / elementCount, (bottom - top) + 120.0 ) / 2.0 );
56+ const double beginX = trunc ((right - left) * 0.1 ), y = trunc ((bottom - top) * 0.5 );
57+ ctxt.move_to (beginX, 50.0 );
6358 ctxt.set_source_rgb (1.0 , 1.0 , 1.0 );
6459 ctxt.select_font_face (" Segoe UI" , font_slant::normal, font_weight::normal);
6560 ctxt.set_font_size (40.0 );
6661 ctxt.show_text (string (" Phase " ).append (to_string (x + 1 )).c_str ());
6762 for (int i = 0 ; i < elementCount; ++i) {
6863 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)))
64+ if (x < phaseCount - 1 ) {
65+ const auto i2 = find (begin (vec[x + 1 ]), end (vec[x + 1 ]), currVal) - begin (vec[x + 1 ]);
66+ const auto x1r = radius * i * 2.0 + radius + beginX, x2r = radius * i2 * 2.0 + radius + beginX;
67+ const auto yr = y - ((i2 == i ? 0.0 : (radius * 4.0 * (normalizedTime < 0.5 ? normalizedTime : 1.0 - normalizedTime)))
7468 * (i % 2 == 1 ? 1.0 : -1.0 ));
75- ctxt.arc ((bx - ax ) * adjustment + ax, yr , radius, 0.0 , two_pi);
69+ ctxt.arc (trunc ((x2r - x1r ) * adjustment + x1r), trunc (yr) , radius - 3.0 , 0.0 , two_pi);
7670 }
7771 else {
7872 ctxt.arc (radius * i * 2.0 + radius + beginX + (4.0 * i), y, radius, 0.0 , two_pi);
7973 }
80- double greyColor = 1.0 - (vec[x][i] / (elementCount - 1.0 ));
74+ double greyColor = 1.0 - (currVal / (elementCount - 1.0 ));
8175 ctxt.set_source_rgb (greyColor, greyColor, greyColor);
8276 ctxt.fill ();
8377 }
84- timer += elapsedTimeInMilliseconds;
85- timer = (timer > phaseTime * (stepCount + 3 )) ? 0.0 : timer;
78+ timer = (timer > phaseTime * (phaseCount + 2 )) ? 0.0 : timer + elapsedTimeInMilliseconds;
8679}
0 commit comments