commit dc4bb23a2d906fab1acb1159269cfc719c834fa5
parent 693b037a4aae008ccda487f808c03cb6279c120d
Author: Christos Margiolis <christos@margiolis.net>
Date: Tue, 16 Jun 2020 00:46:00 +0300
changed some names
Diffstat:
6 files changed, 99 insertions(+), 100 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/obj/plane.o b/obj/plane.o
Binary files differ.
diff --git a/src/main.c b/src/main.c
@@ -2,38 +2,38 @@
#define BUFFSIZE 256
-static void *f = NULL;
+static void *f = NULL;
-static void init_curses(void);
-static void getfunc(Plane *p, char *buf);
-static void validate_expression(Plane *p);
-static float eval(float x);
-static void handle_key(Plane *p, int key);
+static void curses_init(void);
+static void func_get(Plane *p, char *buf);
+static void expression_validate(Plane *p);
+static float expression_evaluate(float x);
+static void keys_handle(Plane *p, int key);
int
main(int argc, char **argv)
{
- init_curses();
+ curses_init();
Plane p;
plane_init(&p);
- restore_zoom(&p);
- validate_expression(&p);
- p.yfunc = eval;
+ zoom_restore(&p);
+ expression_validate(&p);
+ p.yfunc = expression_evaluate;
int key = 0;
while (key != 'q')
{
attron(COLOR_PAIR(1));
- handle_key(&p, key);
+ keys_handle(&p, key);
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(&p);
+ axes_draw(&p);
attroff(COLOR_PAIR(1));
- draw_graph(&p);
+ graph_draw(&p);
refresh();
key = getch();
}
@@ -44,7 +44,7 @@ main(int argc, char **argv)
}
void
-init_curses(void)
+curses_init(void)
{
initscr();
cbreak();
@@ -57,7 +57,7 @@ init_curses(void)
}
void
-getfunc(Plane *p, char *buf)
+func_get(Plane *p, char *buf)
{
move(0, 0);
clrtoeol();
@@ -65,43 +65,43 @@ getfunc(Plane *p, char *buf)
echo();
refresh();
getnstr(buf, BUFFSIZE);
- restore_zoom(p);
+ zoom_restore(p);
refresh();
noecho();
}
void
-validate_expression(Plane *p)
+expression_validate(Plane *p)
{
char *buf = (char *)malloc(BUFFSIZE + sizeof(char));
- getfunc(p, buf);
+ func_get(p, buf);
while (!(f = evaluator_create(buf)))
{
printw("Error in expression! Try again");
- getfunc(p, buf);
+ func_get(p, buf);
refresh();
}
free(buf);
}
float
-eval(float x)
+expression_evaluate(float x)
{
return evaluator_evaluate_x(f, x);
}
void
-handle_key(Plane *p, int key)
+keys_handle(Plane *p, int key)
{
switch (key)
{
- case 'k': case KEY_UP: shift(p, 0.0f, 1.0f); break;
- case 'j': case KEY_DOWN: shift(p, 0.0f, -1.0f); break;
- case 'h': case KEY_LEFT: shift(p, -1.0f, 0.0f); break;
- case 'l': case KEY_RIGHT: shift(p, 1.0f, 0.0f); break;
- case '+': handle_zoom(p, 1.0f/1.05f); break;
- case '-': handle_zoom(p, 1.05f); break;
- case 'r': restore_zoom(p); break;
- case 'f': validate_expression(p); break;
+ case 'k': case KEY_UP: plane_shift(p, 0.0f, 1.0f); break;
+ case 'j': case KEY_DOWN: plane_shift(p, 0.0f, -1.0f); break;
+ case 'h': case KEY_LEFT: plane_shift(p, -1.0f, 0.0f); break;
+ case 'l': case KEY_RIGHT: plane_shift(p, 1.0f, 0.0f); break;
+ case '+': zoom_handle(p, 1.0f/1.05f); break;
+ case '-': zoom_handle(p, 1.05f); break;
+ case 'r': zoom_restore(p); break;
+ case 'f': expression_validate(p); break;
}
}
diff --git a/src/plane.c b/src/plane.c
@@ -3,35 +3,71 @@
void
plane_init(Plane *p)
{
- p->xmin = XMIN_PLANE;
- p->xmax = XMAX_PLANE;
- p->ymin = YMIN_PLANE;
- p->ymax = YMAX_PLANE;
- p->xscale = XSCALE_PLANE;
- p->yscale = YSCALE_PLANE;
- p->xmaxs = getmaxx(stdscr);
- p->ymaxs = getmaxy(stdscr);
+ p->xmin = XMIN_PLANE;
+ p->xmax = XMAX_PLANE;
+ p->ymin = YMIN_PLANE;
+ p->ymax = YMAX_PLANE;
+ p->xscale = XSCALE_PLANE;
+ p->yscale = YSCALE_PLANE;
+ p->xmaxs = getmaxx(stdscr);
+ p->ymaxs = getmaxy(stdscr);
}
void
-restore_zoom(Plane *p)
+plane_shift(Plane *p, float xshift, float yshift)
{
- p->xmin = XMIN_PLANE;
- p->xmax = XMAX_PLANE;
- p->ymin = YMIN_PLANE;
- p->ymax = YMAX_PLANE;
- p->xscale = XSCALE_PLANE;
- p->yscale = YSCALE_PLANE;
+ xshift *= (p->xmax - p->xmin) / 16.0f;
+ yshift *= (p->ymax - p->ymin) / 16.0f;
+ p->xmin += xshift;
+ p->xmax += xshift;
+ p->ymin += yshift;
+ p->ymax += yshift;
+}
+
+float
+plane_scale(float val, float omin, float omax, float nmin, float nmax)
+{
+ float s = (val - omin) / (omax - omin);
+ return s * (nmax - nmin) + nmin;
+}
+
+void
+zoom_restore(Plane *p)
+{
+ p->xmin = XMIN_PLANE;
+ p->xmax = XMAX_PLANE;
+ p->ymin = YMIN_PLANE;
+ p->ymax = YMAX_PLANE;
+ p->xscale = XSCALE_PLANE;
+ p->yscale = YSCALE_PLANE;
}
void
-draw_axes(const Plane *p)
+zoom_handle(Plane *p, float factor)
+{
+ float xctr = (p->xmin + p->ymax) / 2.0f;
+ float yctr = (p->ymin + p->ymax) / 2.0f;
+ p->xmin = plane_scale(factor, 1.0f, 0.0f, p->xmin, xctr);
+ p->xmax = plane_scale(factor, 1.0f, 0.0f, p->xmax, xctr);
+ p->ymin = plane_scale(factor, 1.0f, 0.0f, p->ymin, yctr);
+ p->ymax = plane_scale(factor, 1.0f, 0.0f, p->ymax, yctr);
+}
+
+void
+get_step(const Plane *p, float *xstep, float *ystep)
+{
+ *xstep = (p->xmax - p->xmin) / (p->xmaxs + 1.0f);
+ *ystep = (p->ymax - p->ymin) / (p->ymaxs + 1.0f);
+}
+
+void
+axes_draw(const Plane *p)
{
int i;
- float x0 = scale(0.0f, p->xmin, p->xmax, 0.0f, p->xmaxs);
- float y0 = scale(0.0f, p->ymin, p->ymax, p->ymaxs, 0.0f);
+ float x0 = plane_scale(0.0f, p->xmin, p->xmax, 0.0f, p->xmaxs);
+ float y0 = plane_scale(0.0f, p->ymin, p->ymax, p->ymaxs, 0.0f);
float xstep, ystep;
- getstep(p, &xstep, &ystep);
+ get_step(p, &xstep, &ystep);
for (i = 0; i < p->xmaxs; i++)
{
float plotx = p->xmin + xstep * i;
@@ -48,60 +84,23 @@ draw_axes(const Plane *p)
}
void
-draw_graph(const Plane *p)
+graph_draw(const Plane *p)
{
float x, xstep, ystep;
- getstep(p, &xstep, &ystep);
+ get_step(p, &xstep, &ystep);
attron(COLOR_PAIR(2));
for (x = p->xmin; x <= p->xmax; x += xstep)
{
float y = p->yfunc(x);
- plot(p, x, y);
+ graph_plot(p, x, y);
}
attroff(COLOR_PAIR(2));
}
-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(const Plane *p, float *xstep, float *ystep)
+graph_plot(const Plane *p, float x, float y)
{
- *xstep = (p->xmax - p->xmin) / (p->xmaxs + 1.0f);
- *ystep = (p->ymax - p->ymin) / (p->ymaxs + 1.0f);
-}
-
-void
-plot(const Plane *p, float x, float y)
-{
- float xp = scale(x, p->xmin, p->xmax, 0.0f, p->xmaxs);
- float yp = scale(y, p->ymin, p->ymax, p->ymaxs, 0.0f);
+ float xp = plane_scale(x, p->xmin, p->xmax, 0.0f, p->xmaxs);
+ float yp = plane_scale(y, p->ymin, p->ymax, p->ymaxs, 0.0f);
mvaddch(yp, xp, '.');
}
-
-
-void
-handle_zoom(Plane *p, float factor)
-{
- float xctr = (p->xmin + p->ymax) / 2.0f;
- float yctr = (p->ymin + p->ymax) / 2.0f;
- p->xmin = scale(factor, 1.0f, 0.0f, p->xmin, xctr);
- p->xmax = scale(factor, 1.0f, 0.0f, p->xmax, xctr);
- p->ymin = scale(factor, 1.0f, 0.0f, p->ymin, yctr);
- p->ymax = scale(factor, 1.0f, 0.0f, p->ymax, yctr);
-}
-
-void
-shift(Plane *p, float xshift, float yshift)
-{
- xshift *= (p->xmax - p->xmin) / 16.0f;
- yshift *= (p->ymax - p->ymin) / 16.0f;
- p->xmin += xshift;
- p->xmax += xshift;
- p->ymin += yshift;
- p->ymax += yshift;
-}
diff --git a/src/plane.h b/src/plane.h
@@ -24,14 +24,14 @@ typedef struct {
extern Plane p;
-void plane_init(Plane *p);
-void restore_zoom(Plane *p);
-void draw_axes(const Plane *p);
-void draw_graph(const Plane *p);
-float scale(float val, float omin, float omax, float nmin, float nmax);
-void getstep(const Plane *p, float *xstep, float *ystep);
-void plot(const Plane *p, float x, float y);
-void handle_zoom(Plane *p, float factor);
-void shift(Plane *p, float xshift, float yshift);
+void plane_init(Plane *p);
+float plane_scale(float val, float omin, float omax, float nmin, float nmax);
+void plane_shift(Plane *p, float xshift, float yshift);
+void zoom_restore(Plane *p);
+void zoom_handle(Plane *p, float factor);
+void get_step(const Plane *p, float *xstep, float *ystep);
+void axes_draw(const Plane *p);
+void graph_draw(const Plane *p);
+void graph_plot(const Plane *p, float x, float y);
#endif /* PLANE_H */