feat: improve UX

- invalid iteration counts are met with a usage error
- pressing 'h' will print out an (incomplete) help menu
- information about the current best point and iteration count is shown
This commit is contained in:
dvdrw 2024-11-25 14:45:05 +01:00
parent fe62617beb
commit 1b2ff590ec
Signed by: dvdrw
GPG Key ID: 3ED4E5A371C20DD7

View File

@ -1,4 +1,4 @@
#include "screen.hpp"
#include <screen.hpp>
#include "vec.hpp"
#include <cmath>
#include <cstdio>
@ -16,7 +16,13 @@ int
main(int argc, char **argv) {
int iter_count;
if(argc < 2) iter_count = 100;
else sscanf(argv[1], "%d", &iter_count);
else {
int scanned = sscanf(argv[1], "%d", &iter_count);
if(!scanned) {
printf("Usage: %s <iteration count>\n", argv[0]);
exit(1);
}
}
Swarm<2> swarm(f, iter_count);
Screen scr(80, 24);
@ -63,6 +69,10 @@ main(int argc, char **argv) {
auto update_and_draw = [&]() -> void {
scr.draw();
auto [y, x] = swarm.best();
printf("\033[30;40m\33[2K\r\033[48;2;%d;%d;%dm"
"Current best: (%f, %f) = %f\n",
255, 255, 255, x.x, x.y, y);
};
// We draw to the screen at a rate of `kFPS', but step()ing the swarm at
@ -85,6 +95,7 @@ main(int argc, char **argv) {
swarm.step();
++i;
printf("Current iteration: %d\nCurrent frame: %d\n", i * 4 / kFPS, i);
}
if(frame_step) {
@ -155,9 +166,11 @@ main(int argc, char **argv) {
update_and_draw();
break;
case 'p':
auto [y, x] = swarm.best();
printf("Current best: f(%.10f, %.10f) = %.10f", x.x, x.y, y);
case 'h':
printf(" movement zoom coloring pause step \n"
" W i I K SPC . \n"
" ASD o O L \n"
" quit: q \n");
break;
}
@ -171,7 +184,7 @@ cleanup:
enter_canonical_mode();
auto [y, x] = swarm.best();
printf("Best: f(" V2_FMT ") = %.3f", V2_ARG(x), y);
printf("Minimum found:\n f(%f, %f) = %f\n", x.x, x.y, y);
return 0;
}