Skip to content

Polar-404/CMathgraphics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CMathGraphics

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.


How it works

Mathematical Model

The engine evaluates the input as a function of $x$: $$y = f(x)$$ Where $x$ is sampled across the domain (e.g., $\in[-10, 10]$) to produce the vertices of the graph.

Computation pipeline (C)

The expression you type goes through four stages before any pixel is drawn:

StringTokens (Infix)Tokens (RPN)Node Treeeval_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 + 4 \times x \implies 3 \ 4 \ x \ \times \ + $$

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. Evaluationeval_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.

Rendering pipeline (C++ / OpenGL)

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.

Main loop

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
}

Supported syntax

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)

Visualizations

cos(x * 2) sin(x) / x
cos(x * 2) sin(x) / x
floor(x) * 0.5 abs(exp(0.5 * x)) * sin(10 * x)
floor(x) * 0.5 abs(exp(0.5 * x)) * sin(10 * x)

Building

Dependencies: OpenGL, GLFW, GLAD

# placeholder
cmake -B build && cmake --build build

Licenses

Distributed under the MIT License. Feel free to study, modify, and use this code in your own experiments.

About

C-based low-level program for plotting mathematical functions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors