Skip to content

Commit

Permalink
fully working
Browse files Browse the repository at this point in the history
  • Loading branch information
hotbrightsunshine committed Jul 23, 2023
1 parent 644272b commit 9abb20b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
28 changes: 27 additions & 1 deletion libs/mandelbrot.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,30 @@ void zoom(double coefficient, C& min, C& max) {
min.imag(min.imag() * coefficient);
max.real(max.real() * coefficient);
max.imag(max.imag() * coefficient);
}
}

void translate(direction d, double coefficient, C& min, C& max) {
double deltaX = (max.real() - min.real()) * coefficient;
double deltaY = (max.imag() - min.imag()) * coefficient;

switch (d) {
case direction::DOWN:
min.imag(min.imag() + deltaY);
max.imag(max.imag() + deltaY);
break;
case direction::UP:
min.imag(min.imag() - deltaY);
max.imag(max.imag() - deltaY);
break;
case direction::RIGHT:
min.real(min.real() + deltaX);
max.real(max.real() + deltaX);
break;
case direction::LEFT:
min.real(min.real() - deltaX);
max.real(max.real() - deltaX);
break;
default:
break;
}
}
8 changes: 8 additions & 0 deletions libs/mandelbrot.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class record {
record();
};

enum direction {
UP,
DOWN,
LEFT,
RIGHT
};

// Synonyms
typedef std::complex<double> C;
typedef std::vector<std::vector<record>> Record2DArray;
Expand All @@ -24,5 +31,6 @@ unsigned int is_in_set(std::complex<double> c, unsigned int iterations, double t
Record2DArray get_map(unsigned int height, unsigned int width, C min, C max, unsigned int iterations, unsigned int threshold);
void paint(unsigned int height, unsigned int width, SDL_Renderer* renderer, Record2DArray& grid);
void zoom(double coefficient, C& min, C& max);
void translate(direction d, double coefficient, C&min, C&max);
void update_map(Record2DArray& grid, C min, C max, unsigned int iterations, unsigned int threshold);
void clear_map(Record2DArray& map);
15 changes: 13 additions & 2 deletions src/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const unsigned int ITERATIONS = 50;
const double THRESHOLD = 20;
const double ZOOMIN = 0.8;
const double ZOOMOUT = 1.2;
const double TRANSLCOEFF = 0.2;

C vmin = C(-2, -2);
C vmax = C(2, 2);
Expand Down Expand Up @@ -46,11 +47,21 @@ int main() {
switch (e.key.keysym.sym) {
case SDLK_o:
zoom(ZOOMOUT, vmin, vmax);
std::cout << "Redraw OUT: " << vmin << vmax << std::endl;
break;
case SDLK_i:
zoom(ZOOMIN, vmin, vmax);
std::cout << "Redraw IN" << vmin << vmax << std::endl;
break;
case SDLK_UP:
translate(direction::UP, TRANSLCOEFF, vmin, vmax);
break;
case SDLK_DOWN:
translate(direction::DOWN, TRANSLCOEFF, vmin, vmax);
break;
case SDLK_RIGHT:
translate(direction::RIGHT, TRANSLCOEFF, vmin, vmax);
break;
case SDLK_LEFT:
translate(direction::LEFT, TRANSLCOEFF, vmin, vmax);
break;
default:
break;
Expand Down

0 comments on commit 9abb20b

Please sign in to comment.