graphcurses

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

commit 155dfb66a092a77584dcced14296b538bbee5110
parent 978dfa3124014dfe6d5924e65f5ef43eef9ac322
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sat,  7 Mar 2020 17:12:38 +0200

added make install, pending fixes

Diffstat:
MMakefile | 4++++
MREADME.md | 4+++-
Mbin/graphcurses | 0
Mobj/main.o | 0
Msrc/main.cpp | 85+++++++++++++++++++++++++++++++++++++++----------------------------------------
5 files changed, 49 insertions(+), 44 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,5 @@ TARGET = graphcurses +INSTALL_PATH = /usr/local/bin SRC_DIR = src OBJ_DIR = obj @@ -31,6 +32,9 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp run: ./$(BIN_DIR)/$(TARGET) + +install: $(TARGET) + cp $(BIN_DIR)/$(TARGET) $(INSTALL_PATH) clean: $(RM) $(OBJ) $(BIN_DIR)/$(TARGET) diff --git a/README.md b/README.md @@ -3,7 +3,7 @@ **UNDER CONSTRUCTION** A simple ncurses graph generator. -![Screenshot](https://user-images.githubusercontent.com/54286563/76137975-82b5fd00-604b-11ea-8c34-f1adc98c0224.png) +![Screenshot_20200307_171133](https://user-images.githubusercontent.com/54286563/76145933-fcbfa380-6096-11ea-8175-db2e0d626f13.png) ## Dependencies @@ -20,3 +20,5 @@ A simple ncurses graph generator. * Add coordinates using cursor pointing * *(Perhaps)* add derivative calculator * Display expression +* Fix axes plotting bug +* Add point numbering on axes 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 @@ -8,11 +8,14 @@ #define XMAX_PLANE 2.0f*M_PI #define YMIN_PLANE -M_PI #define YMAX_PLANE M_PI +#define XSCALE_PLANE 1.0f +#define YSCALE_PLANE 1.0f typedef struct { float ymin, ymax; float xmin, xmax; + float xscale, yscale; } Plane; typedef float (*YFunc)(float x); @@ -58,13 +61,17 @@ void draw_axes(Plane &plane) for (int i = 0; i < xmax; i++) { float plotx = plane.xmin + xstep * i; - mvwaddch(stdscr, y0, i, ACS_HLINE); + int tick = fabs(fmod(plotx, plane.xscale)) < xstep; + mvwaddch(stdscr, y0, i, tick ? ACS_PLUS : ACS_HLINE); + // add numbering } for (int i = 0; i < ymax; i++) { float ploty = plane.ymin + ystep * i; - mvwaddch(stdscr, i, x0, ACS_VLINE); + int tick = fabs(fmod(ploty, plane.yscale)) < ystep; + mvwaddch(stdscr, i, x0, tick ? ACS_PLUS : ACS_VLINE); + // add numbering } refresh(); @@ -93,46 +100,28 @@ void draw_graph(Plane &plane, YFunc yfunc) attroff(COLOR_PAIR(2)); } -void handle_zoom(int key, Plane &plane) +void handle_zoom(Plane &plane, float factor) { - // improve - if (key == '+') - { - plane.xmin += 1.5f; - plane.xmax -= 1.5f; - plane.ymin += 1.5f; - plane.ymax -= 1.5f; - } - else if (key == '-') - { - plane.xmin -= 1.5f; - plane.xmax += 1.5f; - plane.ymin -= 1.5f; - plane.ymax += 1.5f; - } - else if (key == 'r') - { - plane.xmin = XMIN_PLANE; - plane.xmax = XMAX_PLANE; - plane.ymin = YMIN_PLANE; - plane.ymax = YMAX_PLANE; - } - + float centerX = (plane.xmin + plane.ymax) / 2; + float centerY = (plane.ymin + plane.ymax) / 2; + 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 handle_key(int key, Plane &plane) +void restore_zoom(Plane &plane) { - float xshift = 0.0f, yshift = 0.0f; - - switch (key) - { - case 'k': case 'w': case KEY_UP: yshift = 1; break; - case 'j': case 's': case KEY_DOWN: yshift = -1; break; - case 'h': case 'a': case KEY_LEFT: xshift = -1; break; - case 'l': case 'd': case KEY_RIGHT: xshift = 1; break; - case '+': case '-': case 'r': handle_zoom(key, plane); break; - } + 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; yshift *= (plane.ymax - plane.ymin) / 16.0f; plane.xmin += xshift; @@ -141,14 +130,24 @@ void handle_key(int key, Plane &plane) plane.ymax += yshift; } +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 '+': handle_zoom(plane, 1.0f/1.05f); break; + case '-': handle_zoom(plane, 1.05f); break; + case 'r': restore_zoom(plane); break; + } +} + int main(int argc, char **argv) { Plane plane; - plane.xmin = XMIN_PLANE; - plane.xmax = XMAX_PLANE; - plane.ymin = YMIN_PLANE; - plane.ymax = YMAX_PLANE; - + restore_zoom(plane); YFunc yfunc = default_func; int key = 0; @@ -166,9 +165,9 @@ int main(int argc, char **argv) init_curses(); while (key != 'q') { - handle_key(key, plane); erase(); attron(COLOR_PAIR(1)); + handle_key(key, plane); draw_axes(plane); attroff(COLOR_PAIR(1));