commit 693b037a4aae008ccda487f808c03cb6279c120d
parent a642dc564b7ebeab205cb7e502d6a57271bf81d9
Author: Christos Margiolis <christos@margiolis.net>
Date: Sat, 30 May 2020 02:46:33 +0300
minor changes
Diffstat:
7 files changed, 27 insertions(+), 29 deletions(-)
diff --git a/Makefile b/Makefile
@@ -13,7 +13,7 @@ MKDIR_P = mkdir -p
CC = gcc
CPPFLAGS += -Iinclude -pedantic -U__STRICT_ANSI__
-CFLAGS += -Wall -std=c99
+CFLAGS += -Wall -std=c99 -O3
LDFLAGS += -Llib
LDLIBS += -lm -lmatheval -lncurses
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
@@ -1,5 +1,7 @@
#include "plane.h"
+#define BUFFSIZE 256
+
static void *f = NULL;
static void init_curses(void);
@@ -17,8 +19,8 @@ main(int argc, char **argv)
restore_zoom(&p);
validate_expression(&p);
p.yfunc = eval;
- int key = 0;
+ int key = 0;
while (key != 'q')
{
attron(COLOR_PAIR(1));
@@ -31,7 +33,6 @@ main(int argc, char **argv)
attroff(A_BOLD);
draw_axes(&p);
attroff(COLOR_PAIR(1));
-
draw_graph(&p);
refresh();
key = getch();
@@ -39,11 +40,10 @@ main(int argc, char **argv)
endwin();
evaluator_destroy(f);
-
- return 0;
+ return EXIT_SUCCESS;
}
-static void
+void
init_curses(void)
{
initscr();
@@ -56,7 +56,7 @@ init_curses(void)
init_pair(2, COLOR_YELLOW, COLOR_BLACK);
}
-static void
+void
getfunc(Plane *p, char *buf)
{
move(0, 0);
@@ -64,16 +64,16 @@ getfunc(Plane *p, char *buf)
printw("f(x) = ");
echo();
refresh();
- getnstr(buf, 256);
+ getnstr(buf, BUFFSIZE);
restore_zoom(p);
refresh();
noecho();
}
-static void
+void
validate_expression(Plane *p)
{
- char *buf = (char *)malloc(256 + sizeof(char));
+ char *buf = (char *)malloc(BUFFSIZE + sizeof(char));
getfunc(p, buf);
while (!(f = evaluator_create(buf)))
{
@@ -84,13 +84,13 @@ validate_expression(Plane *p)
free(buf);
}
-static float
+float
eval(float x)
{
return evaluator_evaluate_x(f, x);
}
-static void
+void
handle_key(Plane *p, int key)
{
switch (key)
diff --git a/src/plane.c b/src/plane.c
@@ -50,9 +50,7 @@ draw_axes(const Plane *p)
void
draw_graph(const Plane *p)
{
- float x;
- float xstep;
- float ystep;
+ float x, xstep, ystep;
getstep(p, &xstep, &ystep);
attron(COLOR_PAIR(2));
for (x = p->xmin; x <= p->xmax; x += xstep)
@@ -73,8 +71,8 @@ scale(float val, float omin, float omax, float nmin, float nmax)
void
getstep(const Plane *p, float *xstep, float *ystep)
{
- if (*xstep) *xstep = (p->xmax - p->xmin) / (p->xmaxs + 1.0f);
- if (*ystep) *ystep = (p->ymax - p->ymin) / (p->ymaxs + 1.0f);
+ *xstep = (p->xmax - p->xmin) / (p->xmaxs + 1.0f);
+ *ystep = (p->ymax - p->ymin) / (p->ymaxs + 1.0f);
}
void
@@ -89,12 +87,12 @@ plot(const Plane *p, float x, float y)
void
handle_zoom(Plane *p, float factor)
{
- float centerX = (p->xmin + p->ymax) / 2.0f;
- float centerY = (p->ymin + p->ymax) / 2.0f;
- p->xmin = scale(factor, 1.0f, 0.0f, p->xmin, centerX);
- p->xmax = scale(factor, 1.0f, 0.0f, p->xmax, centerX);
- p->ymin = scale(factor, 1.0f, 0.0f, p->ymin, centerY);
- p->ymax = scale(factor, 1.0f, 0.0f, p->ymax, centerY);
+ 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
diff --git a/src/plane.h b/src/plane.h
@@ -7,12 +7,12 @@
#include <stdio.h>
#include <stdlib.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;
+#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;
typedef struct {
float (*yfunc)(float x);