A real-time 2D mathematical function plotter built with C and SDL2. This program renders mathematical expressions as graphs with interactive panning and zooming capabilities.
This program takes a mathematical expression as input and plots it on a 2D coordinate system. It evaluates the expression across the visible x-axis range and draws the resulting curve on screen. The plotter includes:
- Real-time rendering of mathematical functions
- Interactive pan and zoom controls
- Dynamic grid with adaptive tick marks
- A resizable fullscreen window
Expression Evaluation
The program uses the TinyExpr library to parse mathematical strings and compile them.
Coordinate Mapping
The plotter maintains a View structure with mathematical boundaries (
Adaptive Grid
The grid calculates tick spacing using:
This formula ensures the grid remains readable at any zoom level. It works by finding what "size class" the current range falls into (ones, tens, hundreds, etc.) using
For example, if your view spans 0-50, the range is 50, so ticks appear every 1 unit. If you zoom out to 0-500, ticks jump to every 10 units.
Rendering Pipeline
Each frame iterates through every horizontal pixel, maps it to a mathematical
Prerequisites: You need SDL2 and SDL2_ttf libraries installed on your system.
Compile the program with:
gcc -Iinclude src/main.c src/core/*.c vendor/tinyexpr/tinyexpr.c -lSDL2 -lSDL2_ttf -lm -o mainExecute the program with the -e flag for the expression and -v flag for the variable:
./main -e 'sin(x)*cos(3*x)*x^2' -v xCommand-line arguments:
-e: The mathematical expression to plot-v: The variable name used in the expression
Example expressions:
./main -e 'x^2' -v x
./main -e 'sin(x)' -v x
./main -e '1/x' -v x
./main -e 'exp(-x^2)' -v x- Left Click + Drag: Pan the view
- Mouse Wheel: Zoom in/out (centered on cursor)
- Close Window: Exit the program