Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 71 additions & 22 deletions src/core/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <backends/imgui_impl_sdlrenderer2.h>
#include <imgui.h>

#include <cmath>
#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<double> symbolTable;
symbolTable.add_constants();
addConstants(symbolTable);
symbolTable.add_variable("x", x);
if (is_polar) {
double theta;

exprtk::expression<double> expression;
expression.register_symbol_table(symbolTable);
exprtk::symbol_table<double> symbolTable;
symbolTable.add_constants();
addConstants(symbolTable);
symbolTable.add_variable("theta", theta);

exprtk::parser<double> parser;
parser.compile(function, expression);
exprtk::expression<double> 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<double> 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<float>(x * zoom),
origin.y - static_cast<float>(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<double> symbolTable;
symbolTable.add_constants();
addConstants(symbolTable);
symbolTable.add_variable("x", x);

exprtk::expression<double> expression;
expression.register_symbol_table(symbolTable);

exprtk::parser<double> 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();
Expand Down
Loading