From dd1a608edaf3dfd848861fc55398668f4b2dab54 Mon Sep 17 00:00:00 2001 From: koma Date: Sat, 9 Jul 2022 20:58:17 +0200 Subject: [PATCH] implement zooming --- graphics.cpp | 99 +++++++++++++++++++++++++++++++------------------- graphics.h | 6 +++ mandelbrot.cpp | 5 +-- 3 files changed, 70 insertions(+), 40 deletions(-) diff --git a/graphics.cpp b/graphics.cpp index a62fe00..dd8fd75 100644 --- a/graphics.cpp +++ b/graphics.cpp @@ -29,8 +29,10 @@ using namespace Mandelbrot; using namespace mpfr; Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title), - from(Vec2mp(0.0, 0.0)), to(Vec2mp(0.0, 0.0)) + from(Vec2mp(0.0, 0.0)), to(Vec2mp(0.0, 0.0)), m_start(Vec2i(0, 0)), + m_end(Vec2i(0, 0)), mouseDown(false) { + SDL_Window *window = SDL_CreateWindow(this->title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, @@ -208,6 +210,55 @@ void Graphics::plot(Mandelbrotc const &m) } } + +void Graphics::drawSelectionRectangle() +{ + if (this->mouseDown) { + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + if (m_start.x < m_end.x) { + for (int i = m_start.x; i < m_end.x; i++) { + SDL_RenderDrawPoint(renderer, i, m_start.y); + SDL_RenderDrawPoint(renderer, i, m_end.y); + } + } else { + for (int i = m_end.x; i < m_start.x; i++) { + SDL_RenderDrawPoint(renderer, i, m_start.y); + SDL_RenderDrawPoint(renderer, i, m_end.y); + } + } + if (m_start.y < m_end.y) { + for (int i = m_start.y; i < m_end.y; i++) { + SDL_RenderDrawPoint(renderer, m_start.x, i); + SDL_RenderDrawPoint(renderer, m_end.x, i); + } + } else { + for (int i = m_end.y; i < m_start.y; i++) { + SDL_RenderDrawPoint(renderer, m_start.x, i); + SDL_RenderDrawPoint(renderer, m_end.x, i); + } + } + } +} + +void Graphics::zoom(Mandelbrotc &m) +{ + if (abs(m_end.x-m_start.x) < 10 || abs(m_end.y-m_start.y) < 10) + return; + + mpreal s_x = (mpfr::abs(m.f.x - m.t.x) * + ((double)this->m_start.x / (double)this->w)) + m.f.x; + mpreal s_y = (mpfr::abs(m.f.y - m.t.y) * + ((double)this->m_start.y / (double)this->h)) + m.f.y; + + mpreal e_x = (mpfr::abs(m.f.x - m.t.x) * + ((double)this->m_end.x / (double)this->w)) + m.f.x; + mpreal e_y = (mpfr::abs(m.f.y - m.t.y) * + ((double)this->m_end.y / (double)this->h)) + m.f.y; + + m.f = Vec2mp(s_x, s_y); + m.t = Vec2mp(e_x, e_y); +} + void Graphics::mainLoop() { mpreal fx = mpreal(-2); @@ -217,7 +268,6 @@ void Graphics::mainLoop() this->from = Vec2mp(fx, fy); this->to = Vec2mp(tx, ty); - bool mouseDown = false; Vec2i m_start = Vec2i(0, 0); Vec2i m_end = Vec2i(0, 0); @@ -253,48 +303,23 @@ void Graphics::mainLoop() } break; case SDL_MOUSEBUTTONDOWN: - if (mouseDown) + if (this->mouseDown) break; - mouseDown = true; - m_start.x = event.button.x; - m_start.y = event.button.y; + this->mouseDown = true; + this->m_start.x = event.button.x; + this->m_start.y = event.button.y; break; case SDL_MOUSEBUTTONUP: - mouseDown = false; + this->mouseDown = false; + this->zoom(m); break; case SDL_MOUSEMOTION: - m_end.x = event.motion.x; - m_end.y = event.motion.y; + this->m_end.x = event.motion.x; + this->m_end.y = event.motion.y; break; } } - if (mouseDown) { - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - if (m_start.x < m_end.x) { - for (int i = m_start.x; i < m_end.x; i++) { - SDL_RenderDrawPoint(renderer, i, m_start.y); - SDL_RenderDrawPoint(renderer, i, m_end.y); - } - } else { - for (int i = m_end.x; i < m_start.x; i++) { - SDL_RenderDrawPoint(renderer, i, m_start.y); - SDL_RenderDrawPoint(renderer, i, m_end.y); - } - } - if (m_start.y < m_end.y) { - for (int i = m_start.y; i < m_end.y; i++) { - SDL_RenderDrawPoint(renderer, m_start.x, i); - SDL_RenderDrawPoint(renderer, m_end.x, i); - } - } else { - for (int i = m_end.y; i < m_start.y; i++) { - SDL_RenderDrawPoint(renderer, m_start.x, i); - SDL_RenderDrawPoint(renderer, m_end.x, i); - } - } - } - // std::cout << "mainloop range " << this->from.x << std::endl; // std::cout << "mainloop range " << this->from.y << std::endl; // std::cout << "mainloop range " << this->to.x << std::endl; @@ -304,13 +329,13 @@ void Graphics::mainLoop() this->plot(m); m.stop_threads(); - // std::cout << "done" << std::endl; + this->drawSelectionRectangle(); SDL_RenderPresent(renderer); SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255); SDL_RenderClear(renderer); - SDL_Delay(10); + //SDL_Delay(10); } m.stop_threads(); } diff --git a/graphics.h b/graphics.h index 100ed97..b7d756b 100644 --- a/graphics.h +++ b/graphics.h @@ -39,9 +39,15 @@ namespace Mandelbrot { Vec2mp to; bool running; + bool mouseDown; + Vec2i m_start; + Vec2i m_end; + Graphics(int w, int h, std::string title); void plot(Mandelbrotc const &m); void mainLoop(); void drawPoint(const Vec2i &pos, const rgb &col); + void drawSelectionRectangle(); + void zoom(Mandelbrotc &m); }; } // namespace Mandelbrot diff --git a/mandelbrot.cpp b/mandelbrot.cpp index e80a997..3bb097d 100644 --- a/mandelbrot.cpp +++ b/mandelbrot.cpp @@ -44,9 +44,8 @@ void Mandelbrotc::start_threads(bool first) for (int i = 0; i < this->thread_count; i++) { std::thread t = std::thread(&Mandelbrotc::calc, this, i); this->threads.push_back(std::move(t)); - std::cout << "thread created" << std::endl; + //std::cout << "thread created" << std::endl; } - this->done = false; } @@ -71,7 +70,7 @@ void Mandelbrotc::calc(const uint8_t tid) } } - std::cout << "thread " << tid << " done" << std::endl; + //std::cout << "thread " << tid << " done" << std::endl; if (tid == thread_count-1) this->done = true; }