graphcurses

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

commit 27da57aa97cbb94f1a6836fba5db8cfff7ccdffb
parent 0bfa1defb4ef9f44a4948606072714b7d8d7e252
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sun,  8 Mar 2020 21:06:58 +0200

remove argv, added in program functions

Diffstat:
MREADME.md | 2--
Mbin/graphcurses | 0
Mobj/main.o | 0
Msrc/main.cpp | 98+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
4 files changed, 63 insertions(+), 37 deletions(-)

diff --git a/README.md b/README.md @@ -12,11 +12,9 @@ A simple ncurses graph generator. ## To Do -* Get rid of `matheval` * Improve key handling * Add slope, curvature etc. * Add an options window -* Add the option to give a function while in the program * Add coordinates using cursor pointing * *(Perhaps)* add derivative calculator * Fix axes plotting bug 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 @@ -1,8 +1,9 @@ #include <ncurses.h> -#include <cmath> -#include <matheval.h> #include <iostream> #include <string> +#include <cmath> +#include <cassert> +#include <matheval.h> #define XMIN_PLANE -2.0f*M_PI #define XMAX_PLANE 2.0f*M_PI @@ -19,9 +20,9 @@ typedef struct } Plane; typedef float (*YFunc)(float x); -void *eval = nullptr; -float default_func(float x) {return sin(x);} -float evalf(float x) {return evaluator_evaluate_x(eval, x);} +void *f = nullptr; +void *fd = nullptr; +float evalf(float x) {return evaluator_evaluate_x(f, x);} void init_curses() { @@ -35,6 +36,29 @@ void init_curses() init_pair(2, COLOR_YELLOW, COLOR_BLACK); } +void restore_zoom(Plane &plane) +{ + plane.xmin = XMIN_PLANE; + plane.xmax = XMAX_PLANE; + plane.ymin = YMIN_PLANE; + plane.ymax = YMAX_PLANE; + plane.xscale = XSCALE_PLANE; + plane.yscale = YSCALE_PLANE; +} + +void getfunc(char *buffer, Plane &plane) +{ + move(0, 0); + clrtoeol(); + printw("f(x) = "); + echo(); + refresh(); + getnstr(buffer, 256); + restore_zoom(plane); + refresh(); + noecho(); +} + float scale(float val, float omin, float omax, float nmin, float nmax) { float s = (val - omin) / (omax - omin); @@ -110,16 +134,6 @@ void handle_zoom(Plane &plane, float factor) plane.ymax = scale(factor, 1.0f, 0.0f, plane.ymax, centerY); } -void restore_zoom(Plane &plane) -{ - plane.xmin = XMIN_PLANE; - plane.xmax = XMAX_PLANE; - plane.ymin = YMIN_PLANE; - plane.ymax = YMAX_PLANE; - plane.xscale = XSCALE_PLANE; - plane.yscale = YSCALE_PLANE; -} - void shift(Plane &plane, float xshift = 0.0f, float yshift = 0.0f) { xshift *= (plane.xmax - plane.xmin) / 16.0f; @@ -130,17 +144,35 @@ void shift(Plane &plane, float xshift = 0.0f, float yshift = 0.0f) plane.ymax += yshift; } +void validate_expression(char *buffer, Plane &plane) +{ + getfunc(buffer, plane); + while (!(f = evaluator_create(buffer))) + { + printw("Error in expression! Try again"); + getfunc(buffer, plane); + refresh(); + } + +} + void handle_key(int key, Plane &plane) { switch (key) { - case 'k': case 'w': case KEY_UP: shift(plane, 0.0f, 1.0f); break; - case 'j': case 's': case KEY_DOWN: shift(plane, 0.0f, -1.0f); break; - case 'h': case 'a': case KEY_LEFT: shift(plane, -1.0f, 0.0f); break; - case 'l': case 'd': case KEY_RIGHT: shift(plane, 1.0f, 0.0f); break; + case 'k': case KEY_UP: shift(plane, 0.0f, 1.0f); break; + case 'j': case KEY_DOWN: shift(plane, 0.0f, -1.0f); break; + case 'h': case KEY_LEFT: shift(plane, -1.0f, 0.0f); break; + case 'l': case KEY_RIGHT: shift(plane, 1.0f, 0.0f); break; case '+': handle_zoom(plane, 1.0f/1.05f); break; case '-': handle_zoom(plane, 1.05f); break; case 'r': restore_zoom(plane); break; + case 'f': // don't repeat + { + char *buffer = new char[256]; + validate_expression(buffer, plane); + delete[] buffer; + } break; } } @@ -148,30 +180,25 @@ int main(int argc, char **argv) { Plane plane; restore_zoom(plane); - YFunc yfunc = default_func; int key = 0; - if (argc > 1) - { - eval = evaluator_create(argv[1]); - if (!eval) - { - std::cout << "Error in expression!" << std::endl; - return -1; - } - yfunc = evalf; - } - init_curses(); + + char *buffer = new char[256]; + validate_expression(buffer, plane); + delete[] buffer; + YFunc yfunc = evalf; + while (key != 'q') { - erase(); attron(COLOR_PAIR(1)); + handle_key(key, plane); + erase(); + attron(A_REVERSE); attron(A_BOLD); - if (argv[1] != nullptr) printw("f(x) = %s", ((std::string)argv[1]).c_str()); - else printw("f(x) = sin(x)"); + mvprintw(0, 0, "f(x) = %s", evaluator_get_string(f)); + attroff(A_REVERSE); attroff(A_BOLD); - handle_key(key, plane); draw_axes(plane); attroff(COLOR_PAIR(1)); @@ -181,6 +208,7 @@ int main(int argc, char **argv) } endwin(); + evaluator_destroy(f); return 0; }