/* * plot.c - function plotting. Pass in an arbitrary function to plot. * To make the program easier, the function plotted is x=f(y) instead of * y=f(x). All function values and parameters are type double. */ #include #include #include "plot.h" void plot(double (*f)(double x), double size) { int i; extern int xvalue(int y); extern void plot_one(int x, int blankchar, int centrechar); for (i = 10; i >= -10; i--) { double y = size * i / 10; int x = (int)floor(10 / size * 2 * f(y) + 0.5); if (i == 0) plot_one(x, '-', '+'); else plot_one(x, ' ', '|'); } } /* * plot one row -- spaces, maybe axes, maybe an asterisk. * 'x' is a value from -20 to 20 which says where the asterisk goes, or * outside that range if there isn't one. blankchar says what goes where * there isn't anything; usually a space, but '-' for the x axis. * centrechar says what to plot for the y axis if there isn't an asterisk * there; usually '|', but '+' for the origin. */ void plot_one(int x, int blankchar, int centrechar) { int i; for (i = -20; i <= 20; i++) { if (i == x) putchar('*'); else if (i == 0) putchar(centrechar); else putchar(blankchar); } putchar('\n'); }