jansky / PlusGraph

An application and statically-linked library that can be used to display 4-quadrant graphs of mathematical functions on the command line

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PlusGraph is an application and statically-linked library that can be used to display 4-quadrant graphs of mathematical functions on the command line. It does not use curses or any similar library. 

The PlusGraph library (libplusgraph) is licensed under the GNU Lesser General Public License v3. The PlusGraph application (plusgraph) is licensed under the GNU General Public License v3. For more information see the file 'LICENSE'.

This Readme file contains information about using the PlusGraph application and library. For installation procedures see the file 'INSTALL'.

Using the PlusGraph Application
-------------------------------

The PlusGraph application can be used to draw graphs of functions of type y=x on the command line. The PlusGraph uses the PlusCalc library to parse and evaluate equations passed to it.

PlusGraph application usage:

plusgraph [width] [height] [scale] [equation]

For example, running 'plusgraph 20 20 1 2*x+2' produces the following output:

..........|...*......
..........|...*......
..........|..*.......
..........|..*.......
..........|.*........
..........|.*........
..........|*.........
..........|*.........
..........*..........
.........*|..........
---------*+----------
........*.|..........
........*.|..........
.......*..|..........
.......*..|..........
......*...|..........
......*...|..........
.....*....|..........
.....*....|..........
....*.....|..........
....*.....|..........


NOTE: There is currently a bug that prevents PlusGraph from displaying graphs that have a different width and height correctly.

The scale argument controls the value of each point. A 20x20 graph with a scale of 1 ranges from -10 to +10, but with a scale of 2 it ranges from -20 to +20 and with a scale of 3 it ranges from -30 to +30.

An expression that can be evaluated by PlusCalc can be used as an equation. For example, running 'plusgraph 20 20 1 sqrt x' produces:

..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|......****
..........|..****....
..........|**........
----------*----------
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........

Similarly, polynomial equations can be entered, like 'plusgraph 20 20 1 x^3+x^2+1*x+1':

..........|..........
..........|..........
..........|.*........
..........|..........
..........|..........
..........|..........
..........|*.........
..........|..........
..........|*.........
.........**..........
---------*+----------
..........|..........
........*.|..........
..........|..........
..........|..........
........*.|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........


NOTE: 2x and 2(3x) are not legal. You must use '2*x' and '2*(3*x)' instead.

For license and warranty information, you can run 'plusgraph --license'.

Using the PlusGraph Library
---------------------------

In order to display a graph using PlusGraph, you need to have a PGGraph struct and a PGDisplayInfo struct. The PGGraph struct contains the height and width of the graph as well as a vector containing the list of points to display. The PGDisplayInfo struct contains information about which characters to use to show points, shaded regions, and axes.

To create a PGGraph struct, call the function pg_graph_create:

//pg_graph_create(int width, int height)
PGGraph graph = pg_graph_create(20,20);

Since PlusGraph graphs span four quadrants, x and y values will range from -10 to +10 in the above example.

NOTE: There is currently a bug that prevents graphs with different widths and heights from displaying properly.

Points can be created with the pg_point_create function:

//pg_point_create(double x, double y, PGPointType type)
//Point Types: PGPT_POINT, PGPT_SHADED, PGPT_NOPOINT
PGPoint p = pg_point_create(3.3, 5, PGPT_POINT);

Once points have been added, they can be added to the points vector in the PGGraph struct:

graph.points.push_back(p);

Note that the x and y coordinates are stored as doubles. The rounding takes place during graph display. Also note that any values within the range of the double type may be used for x and y. Points that are not within the graph's range will simply be ignored.

To generate a PGDisplayInfo struct, use the pg_display_info_create_function:

//pg_display_info_create(char xaxis, char yaxis, char origin, char nopoint, char point, char shaded)
//xaxis = char when x=0,yaxis=char when y=0, origin= char when x and y = 0, nopoint= char when there is no point or axis at that location, point= char when there is a point, shaded= char when there is a shaded region
PGDisplayInfo dinfo = pg_display_info_create('|', '-', '+', '.', '*', '#');

Finally, to display the graph, call the pg_graph_draw function:

//pg_graph_draw(PGGraph graph, PGDisplayInfo dinfo,int scale=1);
pg_graph_draw(graph, dinfo, 1);

The pg_graph_draw function returns a boolean value indicating the success of the operation. The scale argument indicates the value of each point. For example, a 20x20 graph with a scale of one has a range of -10 to +10, while a 20x20 graph with a scale of two has a range of -20 to +20, and a 20x20 graph with a scale of three has a range of -30 to +30.

To put it all together:

PGGraph graph = pg_graph_create(20,20);

PGPoint p = pg_point_create(3.3, 5, PGPT_POINT);
graph.points.push_back(p);

PGDisplayInfo dinfo = pg_display_info_create('|', '-', '+', '.', '*', '#');

pg_graph_draw(graph, dinfo, 1);

The above code generates a graph that looks like:

..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..*.......
..........|..........
..........|..........
..........|..........
..........|..........
----------+----------
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........
..........|..........

To use the PlusGraph library in an application, use the following flags: -lplusgraph -lm


About

An application and statically-linked library that can be used to display 4-quadrant graphs of mathematical functions on the command line

License:Other


Languages

Language:C++ 95.1%Language:Makefile 4.9%