Skip to content

Commit 45c13fa

Browse files
committed
feat: implement graphing explicit functions
of the form y = f(x)
1 parent 6b4c6fe commit 45c13fa

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

src/core/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ add_library(${NAME} STATIC
66
Core/Log.cpp Core/Log.hpp Core/Debug/Instrumentor.hpp
77
Core/Application.cpp Core/Application.hpp Core/Window.cpp Core/Window.hpp
88
Core/Resources.hpp Core/Resources.cpp
9-
Core/DPIHandler.hpp)
9+
Core/DPIHandler.hpp
10+
Core/funcs.hpp)
1011

1112
# Define set of OS specific files to include
1213
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
@@ -24,6 +25,6 @@ target_include_directories(${NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
2425
target_compile_features(${NAME} PRIVATE cxx_std_20)
2526
target_link_libraries(${NAME}
2627
PRIVATE project_warnings
27-
PUBLIC fmt spdlog SDL2::SDL2 imgui Settings)
28+
PUBLIC fmt spdlog exprtk SDL2::SDL2 imgui Settings)
2829

2930
add_subdirectory(Tests)

src/core/Core/Application.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "Core/Resources.hpp"
1616
#include "Core/Window.hpp"
1717
#include "Settings/Project.hpp"
18+
#include "exprtk.hpp"
19+
#include "funcs.hpp"
1820

1921
namespace 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();

src/core/Core/funcs.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Created by nishant on 09/10/25.
3+
//
4+
5+
#ifndef IMGRAPH_FUNCS_HPP
6+
#define IMGRAPH_FUNCS_HPP
7+
#include <numbers>
8+
9+
#include "exprtk.hpp"
10+
11+
inline void addConstants(exprtk::symbol_table<double> &symbolTable) {
12+
symbolTable.add_constant("e", std::numbers::e);
13+
symbolTable.add_constant("π", std::numbers::pi); // "pi" is already added by add_constants()
14+
symbolTable.add_constant("phi", std::numbers::phi);
15+
symbolTable.add_constant("ϕ", std::numbers::phi);
16+
symbolTable.add_constant("φ", std::numbers::phi);
17+
symbolTable.add_constant("gamma", std::numbers::egamma);
18+
symbolTable.add_constant("γ", std::numbers::egamma);
19+
}
20+
21+
#endif // IMGRAPH_FUNCS_HPP

vendor/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ FetchContent_Declare(
3535
FetchContent_Declare(
3636
exprtk
3737
GIT_REPOSITORY "https://github.com/ArashPartow/exprtk.git"
38-
GIT_TAG 0.0.3
38+
GIT_TAG 0.0.3-cmake
3939
)
4040

4141
# Settings

0 commit comments

Comments
 (0)