graphcurses

Curses 2D graph generator
git clone git://git.christosmarg.xyz/graphcurses.git
Log | Files | Refs | README | LICENSE

commit 45636f05b9bbc7ac4f1d8a6c1fb0568ce4255f4d
parent 55fe7e2f3236deff73a17b1ce4919dd9e5c36a8d
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sat,  4 Apr 2020 12:26:00 +0300

cleaned up code a bit

Diffstat:
Mbin/graphcurses | 0
Mobj/main.o | 0
Msrc/main.cpp | 117++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
3 files changed, 65 insertions(+), 52 deletions(-)

diff --git a/bin/graphcurses b/bin/graphcurses Binary files differ. diff --git a/obj/main.o b/obj/main.o Binary files differ. diff --git a/src/main.cpp b/src/main.cpp @@ -11,18 +11,64 @@ #define XSCALE_PLANE 1.0f #define YSCALE_PLANE 1.0f -typedef struct +struct Plane { float ymin, ymax; float xmin, xmax; float xscale, yscale; -} Plane; +}; + +static void *f = nullptr; +static void *fd = nullptr; +static float evalf(float x) {return evaluator_evaluate_x(f, x);} + +static void init_curses(); +static void restore_zoom(Plane& plane); +static void getfunc(char *buffer, Plane& plane); +static float scale(float val, float omin, float omax, float nmin, float nmax); +static void getstep(Plane& plane, float &xstep, float &ystep); +static void draw_axes(Plane& plane); +static void plot(Plane& plane, float x, float y); +static void draw_graph(Plane& plane, const std::function<float(float)>& yfunc); +static void handle_zoom(Plane& plane, float factor); +static void shift(Plane& plane, float xshift = 0.0f, float yshift = 0.0f); +static void validate_expression(Plane& plane); +static void handle_key(int key, Plane& plane); -void *f = nullptr; -void *fd = nullptr; -float evalf(float x) {return evaluator_evaluate_x(f, x);} +int main(int argc, char **argv) +{ + Plane plane; + restore_zoom(plane); + init_curses(); + validate_expression(plane); + std::function<float(float)> yfunc = evalf; + int key = 0; + + while (key != 'q') + { + attron(COLOR_PAIR(1)); + handle_key(key, plane); + erase(); + attron(A_REVERSE); + attron(A_BOLD); + mvprintw(0, 0, "f(x) = %s", evaluator_get_string(f)); + attroff(A_REVERSE); + attroff(A_BOLD); + draw_axes(plane); + attroff(COLOR_PAIR(1)); + + draw_graph(plane, yfunc); + refresh(); + key = getch(); + } + + endwin(); + evaluator_destroy(f); -void init_curses() + return 0; +} + +static void init_curses() { initscr(); cbreak(); @@ -34,7 +80,7 @@ void init_curses() init_pair(2, COLOR_YELLOW, COLOR_BLACK); } -void restore_zoom(Plane &plane) +static void restore_zoom(Plane& plane) { plane.xmin = XMIN_PLANE; plane.xmax = XMAX_PLANE; @@ -44,7 +90,7 @@ void restore_zoom(Plane &plane) plane.yscale = YSCALE_PLANE; } -void getfunc(char *buffer, Plane &plane) +static void getfunc(char *buffer, Plane& plane) { move(0, 0); clrtoeol(); @@ -57,13 +103,13 @@ void getfunc(char *buffer, Plane &plane) noecho(); } -float scale(float val, float omin, float omax, float nmin, float nmax) +static float scale(float val, float omin, float omax, float nmin, float nmax) { float s = (val - omin) / (omax - omin); return s * (nmax - nmin) + nmin; } -void getstep(Plane &plane, float &xstep, float &ystep) +static void getstep(Plane& plane, float &xstep, float &ystep) { int ymax, xmax; getmaxyx(stdscr, ymax, xmax); @@ -71,7 +117,7 @@ void getstep(Plane &plane, float &xstep, float &ystep) if (ystep) ystep = (plane.ymax - plane.ymin) / (ymax + 1.0f); } -void draw_axes(Plane &plane) +static void draw_axes(Plane& plane) { int ymax, xmax; getmaxyx(stdscr, ymax, xmax); @@ -99,7 +145,7 @@ void draw_axes(Plane &plane) refresh(); } -void plot(Plane &plane, float x, float y) +static void plot(Plane& plane, float x, float y) { int ymax, xmax; getmaxyx(stdscr, ymax, xmax); @@ -108,7 +154,7 @@ void plot(Plane &plane, float x, float y) mvwaddch(stdscr, yp, xp, '.'); } -void draw_graph(Plane &plane, const std::function<float(float)>& yfunc) +static void draw_graph(Plane& plane, const std::function<float(float)>& yfunc) { float xstep; float ystep; @@ -122,17 +168,17 @@ void draw_graph(Plane &plane, const std::function<float(float)>& yfunc) attroff(COLOR_PAIR(2)); } -void handle_zoom(Plane &plane, float factor) +static void handle_zoom(Plane& plane, float factor) { - float centerX = (plane.xmin + plane.ymax) / 2; - float centerY = (plane.ymin + plane.ymax) / 2; + float centerX = (plane.xmin + plane.ymax) / 2.0f; + float centerY = (plane.ymin + plane.ymax) / 2.0f; plane.xmin = scale(factor, 1.0f, 0.0f, plane.xmin, centerX); plane.xmax = scale(factor, 1.0f, 0.0f, plane.xmax, centerX); plane.ymin = scale(factor, 1.0f, 0.0f, plane.ymin, centerY); plane.ymax = scale(factor, 1.0f, 0.0f, plane.ymax, centerY); } -void shift(Plane &plane, float xshift = 0.0f, float yshift = 0.0f) +static void shift(Plane& plane, float xshift, float yshift) { xshift *= (plane.xmax - plane.xmin) / 16.0f; yshift *= (plane.ymax - plane.ymin) / 16.0f; @@ -142,7 +188,7 @@ void shift(Plane &plane, float xshift = 0.0f, float yshift = 0.0f) plane.ymax += yshift; } -void validate_expression(Plane &plane) +static void validate_expression(Plane& plane) { char *buffer = new char[256]; getfunc(buffer, plane); @@ -155,7 +201,7 @@ void validate_expression(Plane &plane) delete[] buffer; } -void handle_key(int key, Plane &plane) +static void handle_key(int key, Plane& plane) { switch (key) { @@ -169,36 +215,3 @@ void handle_key(int key, Plane &plane) case 'f': validate_expression(plane); break; } } - -int main(int argc, char **argv) -{ - Plane plane; - restore_zoom(plane); - init_curses(); - validate_expression(plane); - std::function<float(float)> yfunc = evalf; - int key = 0; - - while (key != 'q') - { - attron(COLOR_PAIR(1)); - handle_key(key, plane); - erase(); - attron(A_REVERSE); - attron(A_BOLD); - mvprintw(0, 0, "f(x) = %s", evaluator_get_string(f)); - attroff(A_REVERSE); - attroff(A_BOLD); - draw_axes(plane); - attroff(COLOR_PAIR(1)); - - draw_graph(plane, yfunc); - refresh(); - key = getch(); - } - - endwin(); - evaluator_destroy(f); - - return 0; -}