commit 4d34b6437d52bb2b46f7111a28f7f391de481756
parent 45636f05b9bbc7ac4f1d8a6c1fb0568ce4255f4d
Author: Christos Margiolis <christos@margiolis.net>
Date: Sat, 4 Apr 2020 13:31:58 +0300
made plane class
Diffstat:
6 files changed, 143 insertions(+), 135 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.cpp b/src/main.cpp
@@ -1,45 +1,19 @@
-#include <ncurses.h>
-#include <iostream>
-#include <functional>
-#include <cmath>
-#include <matheval.h>
-
-#define XMIN_PLANE -2.0f*M_PI
-#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
-
-struct Plane
-{
- float ymin, ymax;
- float xmin, xmax;
- float xscale, yscale;
-};
+#include "plane.h"
static void *f = nullptr;
-static void *fd = nullptr;
+static void *df = 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);
int main(int argc, char **argv)
{
- Plane plane;
- restore_zoom(plane);
init_curses();
+ Plane plane;
+ plane.restore_zoom();
validate_expression(plane);
std::function<float(float)> yfunc = evalf;
int key = 0;
@@ -54,10 +28,10 @@ int main(int argc, char **argv)
mvprintw(0, 0, "f(x) = %s", evaluator_get_string(f));
attroff(A_REVERSE);
attroff(A_BOLD);
- draw_axes(plane);
+ plane.draw_axes();
attroff(COLOR_PAIR(1));
- draw_graph(plane, yfunc);
+ plane.draw_graph(yfunc);
refresh();
key = getch();
}
@@ -80,16 +54,6 @@ static void init_curses()
init_pair(2, COLOR_YELLOW, COLOR_BLACK);
}
-static 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;
-}
-
static void getfunc(char *buffer, Plane& plane)
{
move(0, 0);
@@ -98,96 +62,11 @@ static void getfunc(char *buffer, Plane& plane)
echo();
refresh();
getnstr(buffer, 256);
- restore_zoom(plane);
+ plane.restore_zoom();
refresh();
noecho();
}
-static float scale(float val, float omin, float omax, float nmin, float nmax)
-{
- float s = (val - omin) / (omax - omin);
- return s * (nmax - nmin) + nmin;
-}
-
-static void getstep(Plane& plane, float &xstep, float &ystep)
-{
- int ymax, xmax;
- getmaxyx(stdscr, ymax, xmax);
- if (xstep) xstep = (plane.xmax - plane.xmin) / (xmax + 1.0f);
- if (ystep) ystep = (plane.ymax - plane.ymin) / (ymax + 1.0f);
-}
-
-static void draw_axes(Plane& plane)
-{
- int ymax, xmax;
- getmaxyx(stdscr, ymax, xmax);
- float x0 = scale(0.0f, plane.xmin, plane.xmax, 0.0f, xmax);
- float y0 = scale(0.0f, plane.ymin, plane.ymax, ymax, 0.0f);
- float xstep, ystep;
- getstep(plane, xstep, ystep);
-
- for (int i = 0; i < xmax; i++)
- {
- float plotx = plane.xmin + xstep * i;
- 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;
- int tick = fabs(fmod(ploty, plane.yscale)) < ystep;
- mvwaddch(stdscr, i, x0, tick ? ACS_PLUS : ACS_VLINE);
- // add numbering
- }
-
- refresh();
-}
-
-static void plot(Plane& plane, float x, float y)
-{
- int ymax, xmax;
- getmaxyx(stdscr, ymax, xmax);
- float xp = scale(x, plane.xmin, plane.xmax, 0.0f, xmax);
- float yp = scale(y, plane.ymin, plane.ymax, ymax, 0.0f);
- mvwaddch(stdscr, yp, xp, '.');
-}
-
-static void draw_graph(Plane& plane, const std::function<float(float)>& yfunc)
-{
- float xstep;
- float ystep;
- getstep(plane, xstep, ystep);
- attron(COLOR_PAIR(2));
- for (float x = plane.xmin; x <= plane.xmax; x += xstep)
- {
- float y = yfunc(x);
- plot(plane, x, y);
- }
- attroff(COLOR_PAIR(2));
-}
-
-static void handle_zoom(Plane& plane, float factor)
-{
- 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);
-}
-
-static void shift(Plane& plane, float xshift, float yshift)
-{
- xshift *= (plane.xmax - plane.xmin) / 16.0f;
- yshift *= (plane.ymax - plane.ymin) / 16.0f;
- plane.xmin += xshift;
- plane.xmax += xshift;
- plane.ymin += yshift;
- plane.ymax += yshift;
-}
-
static void validate_expression(Plane& plane)
{
char *buffer = new char[256];
@@ -205,13 +84,13 @@ static void handle_key(int key, Plane& plane)
{
switch (key)
{
- 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 'k': case KEY_UP: plane.shift(0.0f, 1.0f); break;
+ case 'j': case KEY_DOWN: plane.shift(0.0f, -1.0f); break;
+ case 'h': case KEY_LEFT: plane.shift(-1.0f, 0.0f); break;
+ case 'l': case KEY_RIGHT: plane.shift(1.0f, 0.0f); break;
+ case '+': plane.handle_zoom(1.0f/1.05f); break;
+ case '-': plane.handle_zoom(1.05f); break;
+ case 'r': plane.restore_zoom(); break;
case 'f': validate_expression(plane); break;
}
}
diff --git a/src/plane.cpp b/src/plane.cpp
@@ -0,0 +1,89 @@
+#include "plane.h"
+
+void Plane::restore_zoom()
+{
+ xmin = XMIN_PLANE;
+ xmax = XMAX_PLANE;
+ ymin = YMIN_PLANE;
+ ymax = YMAX_PLANE;
+ xscale = XSCALE_PLANE;
+ yscale = YSCALE_PLANE;
+}
+
+void Plane::draw_axes()
+{
+ float x0 = scale(0.0f, xmin, xmax, 0.0f, xmaxs);
+ float y0 = scale(0.0f, ymin, ymax, ymaxs, 0.0f);
+ float xstep, ystep;
+ getstep(xstep, ystep);
+
+ for (int i = 0; i < xmaxs; i++)
+ {
+ float plotx = xmin + xstep * i;
+ int tick = fabs(fmod(plotx, xscale)) < xstep;
+ mvaddch(y0, i, tick ? ACS_PLUS : ACS_HLINE);
+ // add numbering
+ }
+
+ for (int i = 0; i < ymaxs; i++)
+ {
+ float ploty = ymin + ystep * i;
+ int tick = fabs(fmod(ploty, yscale)) < ystep;
+ mvaddch(i, x0, tick ? ACS_PLUS : ACS_VLINE);
+ // add numbering
+ }
+}
+
+void Plane::draw_graph(const std::function<float(float)>& yfunc)
+{
+ float xstep;
+ float ystep;
+ getstep(xstep, ystep);
+ attron(COLOR_PAIR(2));
+ for (float x = xmin; x <= xmax; x += xstep)
+ {
+ float y = yfunc(x);
+ plot(x, y);
+ }
+ attroff(COLOR_PAIR(2));
+}
+
+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 Plane::getstep(float &xstep, float &ystep)
+{
+ if (xstep) xstep = (xmax - xmin) / (xmaxs + 1.0f);
+ if (ystep) ystep = (ymax - ymin) / (ymaxs + 1.0f);
+}
+
+void Plane::plot(float x, float y)
+{
+ float xp = scale(x, xmin, xmax, 0.0f, xmaxs);
+ float yp = scale(y, ymin, ymax, ymaxs, 0.0f);
+ mvaddch(yp, xp, '.');
+}
+
+
+void Plane::handle_zoom(float factor)
+{
+ float centerX = (xmin + ymax) / 2.0f;
+ float centerY = (ymin + ymax) / 2.0f;
+ xmin = scale(factor, 1.0f, 0.0f, xmin, centerX);
+ xmax = scale(factor, 1.0f, 0.0f, xmax, centerX);
+ ymin = scale(factor, 1.0f, 0.0f, ymin, centerY);
+ ymax = scale(factor, 1.0f, 0.0f, ymax, centerY);
+}
+
+void Plane::shift(float xshift, float yshift)
+{
+ xshift *= (xmax - xmin) / 16.0f;
+ yshift *= (ymax - ymin) / 16.0f;
+ xmin += xshift;
+ xmax += xshift;
+ ymin += yshift;
+ ymax += yshift;
+}
diff --git a/src/plane.h b/src/plane.h
@@ -0,0 +1,40 @@
+#ifndef PLANE_H
+#define PLANE_H
+
+#include <ncurses.h>
+#include <iostream>
+#include <functional>
+#include <cmath>
+#include <matheval.h>
+
+#define XMIN_PLANE -2.0f*M_PI
+#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
+
+class Plane
+{
+ private:
+ float ymin, ymax;
+ float xmin, xmax;
+ float xscale, yscale;
+ int ymaxs, xmaxs;
+
+ public:
+ Plane()
+ :ymin(YMIN_PLANE), ymax(YMAX_PLANE), xmin(XMIN_PLANE), xmax(XMAX_PLANE),
+ xscale(XSCALE_PLANE), yscale(YSCALE_PLANE), ymaxs(getmaxy(stdscr)), xmaxs(getmaxx(stdscr)) {}
+
+ void restore_zoom();
+ void draw_axes();
+ void draw_graph(const std::function<float(float)>& yfunc);
+ float scale(float val, float omin, float omax, float nmin, float nmax);
+ void getstep(float &xstep, float &ystep);
+ void plot(float x, float y);
+ void handle_zoom(float factor);
+ void shift(float xshift = 0.0f, float yshift = 0.0f);
+};
+
+#endif /* PLANE_H */