From ea724749ba9aae1163bf7e15645ca17905404e1d Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 28 Aug 2020 14:54:42 +0200 Subject: [PATCH] Added Plotting functionality --- ComplexPlotting/PlotWindow.cpp | 50 +++++++++++++++++++-------- ComplexPlotting/PlotWindow.hpp | 6 +++- ComplexPlotting/PlotWindowManager.hpp | 7 ++-- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/ComplexPlotting/PlotWindow.cpp b/ComplexPlotting/PlotWindow.cpp index 8bbf92a..6013de9 100644 --- a/ComplexPlotting/PlotWindow.cpp +++ b/ComplexPlotting/PlotWindow.cpp @@ -5,6 +5,8 @@ #define PI 3.1415926535f +#define MAP(x, A, B, a, b) ((x - A) * (b - a) / (B - A) + a) + SDL_Color HSVtoRGB(float H, float S, float V) { float s = S / 100; @@ -38,15 +40,17 @@ SDL_Color HSVtoRGB(float H, float S, float V) return SDL_Color{ R, G, B, 255 }; } -PlotWindow::PlotWindow(Uint32 id, std::string title) : +PlotWindow::PlotWindow(Uint32 id, std::string title, float rLow, float rHigh, float iLow, float iHigh) : IWindow::IWindow( - UnitVector2u * 400, - UnitVector2i * SDL_WINDOWPOS_UNDEFINED, + UnitVector2u * 400, + UnitVector2i* SDL_WINDOWPOS_UNDEFINED, "Plot " + std::to_string(id) + " [" + title + "]", NULL), - id(id), texture(nullptr) + id(id), texture(nullptr), + low({ rLow, iLow }), high({rHigh, iHigh}), + w(0), h(0) { - + } void PlotWindow::SetCallback(CmplxFunc callback) @@ -54,9 +58,34 @@ void PlotWindow::SetCallback(CmplxFunc callback) this->callback = callback; } +void PlotWindow::DrawTexture() +{ + SDL_SetRenderTarget(m_pRenderer, texture); + for (int y = 0; y < h; y++) + { + for (int x = 0; x < h; x++) + { + float a = std::arg(callback(std::complex + { + MAP((float)x, 0, w, low.real(), high.real()), + MAP((float)y, 0, h, low.real(), high.real()) + } + )); + a = a / PI * 180; + if (a < 0) + a = 360 + a; + + SDL_Color c = HSVtoRGB(a, 100, 100); + + SDL_SetRenderDrawColor(m_pRenderer, c.r, c.g, c.b, c.a); + SDL_RenderDrawPoint(m_pRenderer, x, y); + } + } + SDL_SetRenderTarget(m_pRenderer, NULL); +} + bool PlotWindow::OnCreate() { - int w = 0, h = 0; SDL_GetWindowSize(m_pWindow, &w, &h); texture = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h); if (texture == nullptr) @@ -90,15 +119,8 @@ bool PlotWindow::OnUpdate(double frametime) void PlotWindow::OnRender(SDL_Renderer* renderer) { - float a = std::arg(callback(std::complex{0.f, 0.f})); - a = a / PI * 180; - if (a < 0) - a = 360 + a; - - SDL_Color c = HSVtoRGB(a, 100, 100); - - SDL_SetRenderDrawColor(m_pRenderer, c.r, c.g, c.b, c.a); SDL_RenderClear(m_pRenderer); + SDL_RenderCopy(m_pRenderer, texture, NULL, NULL); SDL_RenderPresent(m_pRenderer); diff --git a/ComplexPlotting/PlotWindow.hpp b/ComplexPlotting/PlotWindow.hpp index 0ce015f..7eebe8d 100644 --- a/ComplexPlotting/PlotWindow.hpp +++ b/ComplexPlotting/PlotWindow.hpp @@ -15,9 +15,10 @@ class PlotWindow : public IWindow { public: - PlotWindow(Uint32 id, std::string title); + PlotWindow(Uint32 id, std::string title, float rLow, float rHigh, float iLow, float iHigh); void SetCallback(CmplxFunc callback); + void DrawTexture(); bool OnCreate() override; bool OnEvent(const SDL_Event& e) override; @@ -29,4 +30,7 @@ private: CmplxFunc callback; SDL_Texture* texture; + int w, h; + + std::complex low, high; }; \ No newline at end of file diff --git a/ComplexPlotting/PlotWindowManager.hpp b/ComplexPlotting/PlotWindowManager.hpp index 7ccd60f..fdfb1e8 100644 --- a/ComplexPlotting/PlotWindowManager.hpp +++ b/ComplexPlotting/PlotWindowManager.hpp @@ -14,16 +14,19 @@ public: static PlotWindow* MakeNew(std::string title) { - PlotWindow* plt = new PlotWindow(PlotWindowCount, title); + PlotWindow* plt = new PlotWindow(PlotWindowCount, title, -2.f, 2.f, -2.f, 2.f); plt->Open(); + // TODO: remove random values float a = (float)(rand() % 200 - 100) / 100.f; float b = (float)(rand() % 200 - 100) / 100.f; plt->SetCallback(std::bind([a, b](std::complex c) { - return std::complex{ a, b }; + return std::complex{1, 1} / (c + std::complex{1, 0}) + + std::complex{2, 1} / (c + std::complex{-1, 0}); }, std::placeholders::_1)); + plt->DrawTexture(); PlotWindows.insert({ PlotWindowCount, plt }); PlotWindowCount++;