Added Plotting functionality

This commit is contained in:
Robert 2020-08-28 14:54:42 +02:00
parent 147a3e61f8
commit ea724749ba
3 changed files with 46 additions and 17 deletions

View file

@ -5,6 +5,8 @@
#define PI 3.1415926535f #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) SDL_Color HSVtoRGB(float H, float S, float V)
{ {
float s = S / 100; float s = S / 100;
@ -38,13 +40,15 @@ SDL_Color HSVtoRGB(float H, float S, float V)
return SDL_Color{ R, G, B, 255 }; 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( IWindow::IWindow(
UnitVector2u * 400, UnitVector2u * 400,
UnitVector2i * SDL_WINDOWPOS_UNDEFINED, UnitVector2i* SDL_WINDOWPOS_UNDEFINED,
"Plot " + std::to_string(id) + " [" + title + "]", "Plot " + std::to_string(id) + " [" + title + "]",
NULL), NULL),
id(id), texture(nullptr) id(id), texture(nullptr),
low({ rLow, iLow }), high({rHigh, iHigh}),
w(0), h(0)
{ {
} }
@ -54,9 +58,34 @@ void PlotWindow::SetCallback(CmplxFunc callback)
this->callback = 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<float>
{
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() bool PlotWindow::OnCreate()
{ {
int w = 0, h = 0;
SDL_GetWindowSize(m_pWindow, &w, &h); SDL_GetWindowSize(m_pWindow, &w, &h);
texture = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h); texture = SDL_CreateTexture(m_pRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
if (texture == nullptr) if (texture == nullptr)
@ -90,15 +119,8 @@ bool PlotWindow::OnUpdate(double frametime)
void PlotWindow::OnRender(SDL_Renderer* renderer) void PlotWindow::OnRender(SDL_Renderer* renderer)
{ {
float a = std::arg(callback(std::complex<float>{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_RenderClear(m_pRenderer);
SDL_RenderCopy(m_pRenderer, texture, NULL, NULL);
SDL_RenderPresent(m_pRenderer); SDL_RenderPresent(m_pRenderer);

View file

@ -15,9 +15,10 @@ class PlotWindow :
public IWindow public IWindow
{ {
public: 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 SetCallback(CmplxFunc callback);
void DrawTexture();
bool OnCreate() override; bool OnCreate() override;
bool OnEvent(const SDL_Event& e) override; bool OnEvent(const SDL_Event& e) override;
@ -29,4 +30,7 @@ private:
CmplxFunc callback; CmplxFunc callback;
SDL_Texture* texture; SDL_Texture* texture;
int w, h;
std::complex<float> low, high;
}; };

View file

@ -14,16 +14,19 @@ public:
static PlotWindow* MakeNew(std::string title) 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(); plt->Open();
// TODO: remove random values
float a = (float)(rand() % 200 - 100) / 100.f; float a = (float)(rand() % 200 - 100) / 100.f;
float b = (float)(rand() % 200 - 100) / 100.f; float b = (float)(rand() % 200 - 100) / 100.f;
plt->SetCallback(std::bind([a, b](std::complex<float> c) plt->SetCallback(std::bind([a, b](std::complex<float> c)
{ {
return std::complex<float>{ a, b }; return std::complex<float>{1, 1} / (c + std::complex<float>{1, 0})
+ std::complex<float>{2, 1} / (c + std::complex<float>{-1, 0});
}, },
std::placeholders::_1)); std::placeholders::_1));
plt->DrawTexture();
PlotWindows.insert({ PlotWindowCount, plt }); PlotWindows.insert({ PlotWindowCount, plt });
PlotWindowCount++; PlotWindowCount++;