A low-level math function plotter written in C (computation) and C++ with OpenGL (rendering). Type any expression and see it's graph plotted in real time.
The engine evaluates the input as a function of
The expression you type goes through four stages before any pixel is drawn:
String → Tokens (Infix) → Tokens (RPN) → Node Tree → eval_node(x)
1. Tokenization — the raw string is scanned character by character and broken into typed tokens: numbers, variables, operators, functions, and parentheses.
2. Infix → RPN (Shunting-Yard) — the token list is rearranged into Reverse Polish Notation, which eliminates the need for parentheses and encodes operator precedence explicitly. For example:
3. AST construction — the RPN token stream is consumed to build a binary expression tree (AST), where each node is either an operator, a function call, or a leaf value.
4. Evaluation — eval_node(root, x) walks the tree recursively for each value of x in the domain [-10, 10], producing a stream of (x, f(x)) pairs.
Points are uploaded to the GPU as a VBO and drawn with GL_LINE_STRIP, connecting samples in order to form the curve. The coordinate range is scaled so that the full domain fits the viewport:
points.push_back(static_cast<float>(x * 0.1)); // X: maps [-10, 10] → [-1, 1]
points.push_back(static_cast<float>(result * 0.1)); // Y
points.push_back(0.0f); // Z (2D)The grid is drawn separately via a fullscreen quad — a fragment shader runs over every pixel and decides whether it falls on a grid line, using the window dimensions passed as uniforms.
The program runs two threads concurrently: one handles console input, the other runs the render loop. An atomic flag new_expr signals when a new expression is ready.
init_opengl();
std::thread input_thread(user_interface);
while (true) {
if (new_expr) {
new_expr = false;
current_expr = calc_expr_tree(math_expr); // parse + upload to GPU
}
render_frame(); // draw grid + curve
}| Token type | Examples |
|---|---|
| Numbers | 3, 1.5, .7 |
| Variable | x |
| Operators | +, -, *, /, ^ |
| Functions | sin, cos, tan, log, sqrt, ... |
| Grouping | (, ) |
Example expressions:
sin(x)
x^2 - 3*x + 2
sin(x) / x
sqrt(1 - x^2)
Dependencies: OpenGL, GLFW, GLAD
# placeholder
cmake -B build && cmake --build buildDistributed under the MIT License. Feel free to study, modify, and use this code in your own experiments.



