1515#include " Core/Resources.hpp"
1616#include " Core/Window.hpp"
1717#include " Settings/Project.hpp"
18+ #include " exprtk.hpp"
19+ #include " funcs.hpp"
1820
1921namespace App {
2022
@@ -110,6 +112,8 @@ ExitStatus App::Application::run() {
110112 const ImVec2 base_pos = viewport->Pos ;
111113 const ImVec2 base_size = viewport->Size ;
112114
115+ static char function[1024 ] = " sin(x)" ;
116+
113117 // Left Pane (expression)
114118 {
115119 ImGui::SetNextWindowPos (base_pos);
@@ -119,9 +123,8 @@ ExitStatus App::Application::run() {
119123 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse |
120124 ImGuiWindowFlags_NoTitleBar);
121125
122- static char text[1024 ] = " y = sin(x)" ;
123126 ImGui::InputTextMultiline (
124- " ##search" , text , sizeof (text ), ImVec2 (-FLT_MIN, ImGui::GetTextLineHeight () * 4 ));
127+ " ##search" , function , sizeof (function ), ImVec2 (-FLT_MIN, ImGui::GetTextLineHeight () * 4 ));
125128
126129 ImGui::End ();
127130 }
@@ -147,7 +150,7 @@ ExitStatus App::Application::run() {
147150 // Define your coordinate system origin (e.g., center of the canvas)
148151 const ImVec2 origin (canvas_p0.x + canvas_sz.x * 0 .5f , canvas_p0.y + canvas_sz.y * 0 .5f );
149152
150- float lineThickness = 4 .0f ;
153+ float lineThickness = 6 .0f ;
151154
152155 // 1. Draw Axes
153156 draw_list->AddLine (ImVec2 (canvas_p0.x , origin.y ),
@@ -162,18 +165,33 @@ ExitStatus App::Application::run() {
162165 // 2. Plot a function (e.g., y = sin(x))
163166 const float zoom = 100 .0f ; // Pixels per unit
164167 std::vector<ImVec2> points;
165- for (float x = -canvas_sz.x / (2 * zoom); x < canvas_sz.x / (2 * zoom); x += 0 .1f ) {
166- float y = sin (x);
168+
169+ double x;
170+
171+ exprtk::symbol_table<double > symbolTable;
172+ symbolTable.add_constants ();
173+ addConstants (symbolTable);
174+ symbolTable.add_variable (" x" , x);
175+
176+ exprtk::expression<double > expression;
177+ expression.register_symbol_table (symbolTable);
178+
179+ exprtk::parser<double > parser;
180+ parser.compile (function, expression);
181+
182+ for (x = -canvas_sz.x / (2 * zoom); x < canvas_sz.x / (2 * zoom); x += 0.1 ) {
183+ // This loop uses the *mathematical* values of x. This is later converted to the pixel
184+ // values below
185+ const double y = expression.value ();
167186
168187 // Convert graph coordinates to screen coordinates
169- // Y is inverted because screen coordinates go down
170188 ImVec2 screen_pos (origin.x + x * zoom, origin.y - y * zoom);
171189 points.push_back (screen_pos);
172190 }
173191
174192 // Draw the function as a polyline
175193 draw_list->AddPolyline (
176- points.data (), points.size (), IM_COL32 (255 , 0 , 0 , 255 ), ImDrawFlags_None, lineThickness);
194+ points.data (), points.size (), IM_COL32 (199 , 68 , 64 , 255 ), ImDrawFlags_None, lineThickness);
177195
178196 ImGui::End ();
179197 ImGui::PopStyleColor ();
0 commit comments