GraphTUI/README.md
2026-10-24 18:40:19 +03:00

168 lines
8.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# GraphTUI
A clean, fast, intuitive, and feature-packed mathematical graphing TUI (Terminal User Interface) application written in **C# and .NET 10** with **zero external packages or NuGet dependencies**.
![GraphTUI Demo](https://raw.githubusercontent.com/antigravity/assets/main/graphtui-banner.png)
---
## Highlights
- **Zero Dependencies**: 100% standalone standard .NET 10. Runs anywhere without restoring external packages.
- **Unicode Braille Subpixel Canvas**: Leverages $2 \times 4$ Braille dot matrices (`U+2800`–`U+28FF`) for an $8\times$ resolution boost per terminal cell, rendering continuous, crisp curves with almost square 1:1 subpixels.
- **Double-Buffered Differential Rendering**: In-memory virtual screen diffs consecutive frames and outputs only changed cells via ANSI truecolor (24-bit RGB) sequences for 60+ FPS flicker-free performance.
- **100% Keyboard Parity & Full Mouse Support**: Every feature is accessible via keyboard alone (Vim keys `h/j/k/l`, Arrows, Tab, Hotkeys, Command Bar), plus full mouse support (scroll wheel zoom, click-drag pan, direct clicking).
- **Handcrafted Scientific Math Engine**: Recursive-descent Pratt parser supporting implicit multiplication (`2x`, `3sin(x)`, `(x+1)(x-1)`), standard and hyperbolic trigonometry, logarithms, roots, powers, constants ($\pi, e, \tau, \phi$).
- **Live Numerical Calculus Overlays**:
- Tangent line tracing with real-time slope ($m = dy/dx$) calculation.
- Derivative curve overlay $f'(x)$.
- Definite numerical integration ($\int_a^b f(x) dx$) via Simpson's rule with visual area shading.
- Root finder ($f(x) = 0$) and local extrema detector (minima and maxima).
- **Dynamic Parameter Sliders**: Tweak variables ($a, b, c$) in real time with immediate graphical curve morphing.
- **Mathematical Presets Gallery**: One-key access to Fourier series approximations, Lissajous figures, Butterfly curves, damped harmonic oscillators, cardioids, and polynomials.
- **Session & Export**: Save/load sessions as JSON, and export graphs to vector SVG or Unicode Braille text files.
---
## Getting Started
### Prerequisites
- [.NET 10 SDK](https://dotnet.microsoft.com/)
### Run GraphTUI
```bash
# Launch interactive application
dotnet run --project src/GraphTUI/GraphTUI.csproj
# Launch directly with custom mathematical functions
dotnet run --project src/GraphTUI/GraphTUI.csproj -- "sin(x)" "cos(2x)"
# Launch by loading an existing saved session
dotnet run --project src/GraphTUI/GraphTUI.csproj -- --load session.json
```
### Run Tests
```bash
# Zero-dependency test runner
dotnet run --project tests/GraphTUI.Tests/GraphTUI.Tests.csproj
```
---
## Controls Reference (100% Keyboard Parity)
| Feature | Keyboard Shortcut | Mouse Action |
| :--- | :--- | :--- |
| **Pan Canvas** | Arrow Keys or `h, j, k, l` | Click & Drag canvas |
| **Zoom In / Out** | `+` (or `=`) / `-` (or `_`), `[` / `]` | Mouse Scroll Wheel (zooms at cursor) |
| **Reset View** | `Home` or `0` | Double-click canvas |
| **Cycle Focus (Pane)** | `Tab` / `Shift+Tab` | Click target pane |
| **Toggle Sidebar** | `F2` or `\` | Click `[ < ]` / `[ > ]` toggle icon |
| **Trace Mode** | `t` (toggle trace), `Left`/`Right` to glide along curve | Click directly on graph |
| **Function List** | `Up`/`Down` / `j`/`k` to select | Click row |
| **Toggle Visibility** | `Space` | Click `[x]` checkbox |
| **Cycle Color** | `c` | Click color bullet `●` |
| **Edit Formula** | `Enter` or `e` | Click formula text |
| **Delete Function** | `Delete` or `d` | - |
| **Add Function** | `a` | Click `[+ Add]` button |
| **Parameter Sliders** | `Left`/`Right` or `,`/`.` or `[`/`]` | Click/Drag slider track |
| **Calculus Tools** | `c` / `F3` (Roots, Extrema, Integral, Tangent) | Click Calculus button |
| **Presets Gallery** | `p` / `F4` (Fourier, Lissajous, Butterfly, etc.) | Click Presets button |
| **Command Bar** | `:` (`:save`, `:load`, `:export`, `:quit`) | - |
| **Quick Help** | `?` or `F1` | Click Help |
| **Quit** | `q` or `Ctrl+C` or `:quit` | Click `[X]` exit |
---
## Mathematical Syntax
### Expressions & Double-Sided Equations
GraphTUI automatically parses and classifies all single-sided and double-sided definitions:
- **$y = f(x)$ or $f(x) = y$**: Standard Cartesian curves, e.g. `y = sin(x)`, `y = x^2 - 4`, `sin(x) = y`
- **$f(y) = x$ or $x = f(y)$**: Inverse and vertical curves, e.g. `y^2 = x`, `sin(y) = x`, `x = y^2`, `x = cos(y)`
- **$f(y) = g(x)$ or $F(x, y) = 0$**: Double-sided implicit curves & relations, e.g. `y^2 = sin(x)`, `sin(y) = cos(x)`, `y^2 = x^3 - 3x` (elliptic curve), `x^2 + y^2 = 25` (circle), `y^2 - x^2 = 1` (hyperbola)
- **Parametric curves**: $x(t) = \cos(3t)$, $y(t) = \sin(4t)$
- **Polar curves**: $r = f(\theta)$, e.g. `2*(1 - sin(theta))`
- **Implicit multiplication**: `2x`, `3sin(x)`, `(x+1)(x-1)`, `2pi`, `x y`
### Calculus, Summation & Product Operations
- **Summation**: `sum(n=start, end, f(n))` or `sum(n, start, end, f(n))`
Example: `sum(n=1, 5, sin(n*x)/n)` (Fourier harmonic synthesis), `sum(k=0, 8, x^k / fact(k))` (Taylor expansion of $e^x$)
- **Product**: `prod(n=start, end, f(n))` or `prod(n, start, end, f(n))`
Example: `prod(n=1, 4, 1 + x/n)` (Weierstrass product)
- **Definite Integrals in Formulas**: `int(t=a, b, f(t))` or `integral(t=a, b, f(t))`
Example: `y = int(t=0, x, sin(t^2))` (Fresnel sine integral), `y = int(t=1, x, 1/t)` (definition of $\ln(x)$)
- **Numerical Derivative Operator**: `diff(t=x, f(t))` or `diff(f(x), x)`
Example: `y = diff(t=x, sin(t^3))` (evaluates $\frac{d}{dx}[\sin(x^3)] = 3x^2 \cos(x^3)$)
- **Factorials, Double & Super Factorials**:
- **Single Factorial**: `x!` or `fact(x)` (e.g. `5! = 120`, `(x+1)!`)
- **Double Factorial**: `x!!` or `dfact(x)` (e.g. `5!! = 15`, `6!! = 48`, with full analytic continuation)
- **Super Factorial**: `x$` or `sfact(x)` (e.g. `4$ = 1!*2!*3!*4! = 288`)
- **Gamma function**: `gamma(x)` (Lanczos approximation for $\Gamma(x)$, accurate to 14 decimal digits)
- **Zero-Lag Line Clipping & Caching**: Powered by Cohen-Sutherland line clipping and static lookup tables ($0! \dots 170!$, $0!! \dots 300!!$, $0\$ \dots 27\$$). Steep factorial growth cannot hang the terminal or trigger off-screen rasterization.
### Built-in Functions & Constants
- **Trigonometric**: `sin`, `cos`, `tan`, `cot`, `sec`, `csc`
- **Inverse Trig**: `asin`, `acos`, `atan`, `atan2(y, x)`
- **Hyperbolic**: `sinh`, `cosh`, `tanh`
- **Logarithms & Powers**: `ln`, `log` (base 10), `log2`, `exp`, `sqrt`, `cbrt`, `^`
- **Rounding & Utilities**: `abs`, `floor`, `ceil`, `round`, `sgn`, `min(a, b)`, `max(a, b)`, `clamp(x, min, max)`
- **Constants**: `pi` ($\pi$), `e`, `tau` ($2\pi$), `phi` ($\approx 1.618$)
### Function Cheatsheet (`F5` or `:cheat`)
Press **`F5`** at any time to open the built-in, categorized **Math Cheatsheet**:
- **Tab / 1-5**: Switch categories (Calculus/Sum/Prod, Double-Sided Equations, Trig, Exponentials, Utilities).
- **Up / Down**: Browse formulas, syntax, descriptions, and real-world examples.
- **Enter**: Instantly loads and graphs the selected example!
---
## Architecture
```
GrapherTUI/
├── GraphTUI.sln
├── src/GraphTUI/
│ ├── Program.cs # CLI entry point
│ ├── App.cs # Main event loop, layout orchestration, state
│ ├── Core/
│ │ ├── Math/ # Zero-dependency lexer, Pratt parser, AST, calculus
│ │ │ ├── Token.cs
│ │ │ ├── Lexer.cs
│ │ │ ├── AstNodes.cs
│ │ │ ├── Parser.cs
│ │ │ ├── Evaluator.cs
│ │ │ └── Calculus.cs
│ │ └── Models/ # Viewport, functions, parameters, session DTO
│ │ ├── FunctionType.cs
│ │ ├── GraphFunction.cs
│ │ ├── Parameter.cs
│ │ ├── Viewport.cs
│ │ └── SessionData.cs
│ ├── Rendering/ # Braille subpixel canvas, plot renderer, SVG & text exporter
│ │ ├── ColorRgb.cs
│ │ ├── BrailleCanvas.cs
│ │ ├── PlotRenderer.cs
│ │ ├── SvgExporter.cs
│ │ └── TextExporter.cs
│ ├── Terminal/ # VT/ANSI codes, SGR mouse & keyboard input, virtual screen
│ │ ├── AnsiCodes.cs
│ │ ├── KeyAction.cs
│ │ ├── InputReader.cs
│ │ └── VirtualScreen.cs
│ └── UI/ # Split-pane layout, views, modals, command prompt
│ ├── LayoutManager.cs
│ └── Views/
│ ├── FunctionListView.cs
│ ├── ParameterSlidersView.cs
│ ├── PresetsGalleryModal.cs
│ ├── CalculusModal.cs
│ ├── HelpModal.cs
│ └── CommandBarView.cs
└── tests/GraphTUI.Tests/ # Zero-dependency unit test suite (21 unit tests)
```
---
## License
MIT