Skip to content

Commit 86687cd

Browse files
committed
feat(graph): enhance plotting support for polar coordinates
1 parent 7cae8e0 commit 86687cd

File tree

1 file changed

+59
-21
lines changed

1 file changed

+59
-21
lines changed

src/core/Core/Application.cpp

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <backends/imgui_impl_sdlrenderer2.h>
66
#include <imgui.h>
77

8+
#include <cmath>
89
#include <memory>
910
#include <string>
1011
#include <vector>
@@ -212,33 +213,70 @@ ExitStatus App::Application::run() {
212213
}
213214

214215
if (!plotted) {
215-
// Fallback to y = f(x) plotting using variable x
216-
double x;
216+
std::string func_str(function);
217+
bool is_polar = func_str.find("theta") != std::string::npos;
217218

218-
exprtk::symbol_table<double> symbolTable;
219-
symbolTable.add_constants();
220-
addConstants(symbolTable);
221-
symbolTable.add_variable("x", x);
219+
if (is_polar) {
220+
double theta;
222221

223-
exprtk::expression<double> expression;
224-
expression.register_symbol_table(symbolTable);
222+
exprtk::symbol_table<double> symbolTable;
223+
symbolTable.add_constants();
224+
addConstants(symbolTable);
225+
symbolTable.add_variable("theta", theta);
225226

226-
exprtk::parser<double> parser;
227-
parser.compile(function, expression);
227+
exprtk::expression<double> expression;
228+
expression.register_symbol_table(symbolTable);
228229

229-
for (x = -canvas_sz.x / (2 * zoom); x < canvas_sz.x / (2 * zoom); x += 0.05) {
230-
const double y = expression.value();
230+
exprtk::parser<double> parser;
231+
if (parser.compile(function, expression)) {
232+
const double theta_min = 0.0;
233+
const double theta_max = 4.0 * M_PI;
234+
const double theta_step = 0.02;
231235

232-
233-
ImVec2 screen_pos(origin.x + x * zoom, origin.y - y * zoom);
234-
points.push_back(screen_pos);
235-
}
236+
for (theta = theta_min; theta <= theta_max; theta += theta_step) {
237+
const double r = expression.value();
238+
239+
const double x = r * cos(theta);
240+
const double y = r * sin(theta);
241+
242+
ImVec2 screen_pos(origin.x + static_cast<float>(x * zoom),
243+
origin.y - static_cast<float>(y * zoom));
244+
points.push_back(screen_pos);
245+
}
246+
247+
draw_list->AddPolyline(points.data(),
248+
points.size(),
249+
IM_COL32(128, 64, 199, 255),
250+
ImDrawFlags_None,
251+
lineThickness);
252+
}
253+
} else {
254+
double x;
255+
256+
exprtk::symbol_table<double> symbolTable;
257+
symbolTable.add_constants();
258+
addConstants(symbolTable);
259+
symbolTable.add_variable("x", x);
260+
261+
exprtk::expression<double> expression;
262+
expression.register_symbol_table(symbolTable);
263+
264+
exprtk::parser<double> parser;
265+
parser.compile(function, expression);
266+
267+
for (x = -canvas_sz.x / (2 * zoom); x < canvas_sz.x / (2 * zoom); x += 0.05) {
268+
const double y = expression.value();
236269

237-
draw_list->AddPolyline(points.data(),
238-
points.size(),
239-
IM_COL32(199, 68, 64, 255),
240-
ImDrawFlags_None,
241-
lineThickness);
270+
ImVec2 screen_pos(origin.x + x * zoom, origin.y - y * zoom);
271+
points.push_back(screen_pos);
272+
}
273+
274+
draw_list->AddPolyline(points.data(),
275+
points.size(),
276+
IM_COL32(199, 68, 64, 255),
277+
ImDrawFlags_None,
278+
lineThickness);
279+
}
242280
}
243281

244282
ImGui::End();

0 commit comments

Comments
 (0)