vin / doeke-m4

Doeke's fractal generator

This URL has Read+Write access

doeke-m4 / plotter.h
100644 97 lines (82 sloc) 1.823 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef PLOTTER_H
#define PLOTTER_H
 
#include "misc.h"
#include "gfx.h"
#include "palette.h"
#include "input.h"
 
const int square_size = 128;
 
struct Square {
  float pix[square_size][square_size];
  int step, fase, x_offset,
  y_offset, x_max, y_max;
 
  Square() {}
  Square(int a, int b, int c, int d):
         x_offset(a), y_offset(b),
         x_max(c), y_max(d) {
    reset();
  }
  bool advance() {
    if (--fase < 1) {
      fase = 3;
      step >>= 1;
    }
    return (step > 0);
  }
  void reset() {
    step = 8;
    fase = 4;
  }
};
 
class Thread {
  private:
    bool running;
 
  public:
    Thread();
    int run();
    void stop();
};
 
class Plotter {
  private:
    Square *squares;
    SDL_mutex *queue_mutex;
    std::deque<Square*> queue;
    Thread *threads;
    SDL_Thread **sdl_threads;
    
    double magni, r_step, i_step,
           min_r, min_i, max_r, max_i;
 
  public:
    int max_iter, n_squares, n_threads;
 
    Plotter();
    void init();
    double iterate(double, double);
    void resize(int, int);
    void plot();
    void end_plotting();
    void print_pos();
    void zoom(double);
    void center();
    Square *get_square(Square*);
    double x_to_r(int);
    double y_to_i(int);
};
 
inline double Plotter::x_to_r(int x) {
  return min_r + x * r_step;
}
inline double Plotter::y_to_i(int y) {
  return min_i + y * i_step;
}
 
int thread_launcher(void*);
 
inline double Plotter::iterate(double cr, double ci) {
  double zr = cr, zi = ci, zr2 = zr*zr, zi2 = zi*zi;
  int iter;
  for (iter = 0; iter < max_iter && zr2 + zi2 < 40.; iter++) {
    zi = (zr + zr) * zi + ci;
    zr = zr2 - zi2 + cr;
    zr2 = zr * zr;
    zi2 = zi * zi;
  }
  if (iter >= max_iter) return -1;
  double smooth_iter = iter - log(log(sqrt(zr2+zi2))) / 0.6931;
  return std::max(0., smooth_iter);
}
 
#endif