diff --git a/src/core/Core/Application.cpp b/src/core/Core/Application.cpp index 75c44b2..6a5495d 100644 --- a/src/core/Core/Application.cpp +++ b/src/core/Core/Application.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -112,7 +113,7 @@ ExitStatus App::Application::run() { const ImVec2 base_pos = viewport->Pos; const ImVec2 base_size = viewport->Size; - static char function[1024] = "tanh(x)"; + static char function[1024] = "r = 1 + 0.5*cos(theta)"; static float zoom = 100.0f; // Left Pane (expression) @@ -212,33 +213,81 @@ ExitStatus App::Application::run() { } if (!plotted) { - // Fallback to y = f(x) plotting using variable x - double x; + std::string func_str(function); + bool is_polar = func_str.find("r=") != std::string::npos || func_str.find("r =") != std::string::npos; - exprtk::symbol_table symbolTable; - symbolTable.add_constants(); - addConstants(symbolTable); - symbolTable.add_variable("x", x); + if (is_polar) { + double theta; - exprtk::expression expression; - expression.register_symbol_table(symbolTable); + exprtk::symbol_table symbolTable; + symbolTable.add_constants(); + addConstants(symbolTable); + symbolTable.add_variable("theta", theta); - exprtk::parser parser; - parser.compile(function, expression); + exprtk::expression expression; + expression.register_symbol_table(symbolTable); - for (x = -canvas_sz.x / (2 * zoom); x < canvas_sz.x / (2 * zoom); x += 0.05) { - const double y = expression.value(); + std::string polar_function = func_str; + size_t eq_pos = func_str.find("r="); + if (eq_pos == std::string::npos) { + eq_pos = func_str.find("r ="); + } + if (eq_pos != std::string::npos) { + size_t start_pos = func_str.find("=", eq_pos) + 1; + polar_function = func_str.substr(start_pos); + polar_function.erase(0, polar_function.find_first_not_of(" \t")); + } - - ImVec2 screen_pos(origin.x + x * zoom, origin.y - y * zoom); - points.push_back(screen_pos); - } + exprtk::parser parser; + if (parser.compile(polar_function, expression)) { + const double theta_min = 0.0; + const double theta_max = 4.0 * M_PI; + const double theta_step = 0.02; + + for (theta = theta_min; theta <= theta_max; theta += theta_step) { + const double r = expression.value(); + + const double x = r * cos(theta); + const double y = r * sin(theta); - draw_list->AddPolyline(points.data(), - points.size(), - IM_COL32(199, 68, 64, 255), - ImDrawFlags_None, - lineThickness); + ImVec2 screen_pos(origin.x + static_cast(x * zoom), + origin.y - static_cast(y * zoom)); + points.push_back(screen_pos); + } + + draw_list->AddPolyline(points.data(), + points.size(), + IM_COL32(128, 64, 199, 255), + ImDrawFlags_None, + lineThickness); + } + } else { + double x; + + exprtk::symbol_table symbolTable; + symbolTable.add_constants(); + addConstants(symbolTable); + symbolTable.add_variable("x", x); + + exprtk::expression expression; + expression.register_symbol_table(symbolTable); + + exprtk::parser parser; + parser.compile(function, expression); + + for (x = -canvas_sz.x / (2 * zoom); x < canvas_sz.x / (2 * zoom); x += 0.05) { + const double y = expression.value(); + + ImVec2 screen_pos(origin.x + x * zoom, origin.y - y * zoom); + points.push_back(screen_pos); + } + + draw_list->AddPolyline(points.data(), + points.size(), + IM_COL32(199, 68, 64, 255), + ImDrawFlags_None, + lineThickness); + } } ImGui::End();