5 #include "vstgui/lib/cpoint.h" 6 #include "vstgui/lib/dispatchlist.h" 15 using Complex = std::complex<double>;
21 virtual void modelChanged (
const Model& model) = 0;
27 using Ptr = std::shared_ptr<Model>;
32 const Point& getMax ()
const {
return max; }
33 const Point& getMin ()
const {
return min; }
34 uint32_t getIterations ()
const {
return iterations; }
36 void setIterations (uint32_t newIterations)
38 if (newIterations != iterations)
40 iterations = newIterations;
47 if (max == newMax && min == newMin)
57 listeners.forEach ([
this] (
auto& l) { l->modelChanged (*
this); });
62 Point min {-2.2, -1.7};
63 uint32_t iterations {50};
70 p.x = min.x + pixel.x / (size.x - 1.0) * (max.x - min.x);
71 p.y = min.y + pixel.y / (size.y - 1.0) * (max.y - min.y);
76 inline double hypot (
double x,
double y)
78 return std::sqrt (x * x + y * y);
82 inline Complex mulAdd (Complex z, Complex w, Complex v)
94 return Complex (x + v.real (), y + v.imag ());
98 template <
typename SetPixelProc>
99 inline void calculateLine (uint32_t line, Point size,
const Model& model, SetPixelProc setPixel)
101 Point sizeInv (size);
103 sizeInv.x = 1. / sizeInv.x;
104 sizeInv.y = 1. / sizeInv.y;
106 diff.x = model.getMax ().x - model.getMin ().x;
107 diff.y = model.getMax ().y - model.getMin ().y;
109 pos.y = model.getMin ().y + line * sizeInv.y * diff.y;
110 std::vector<uint32_t> iterationResult (size.x);
111 for (
auto x = 0u; x < size.x; ++x)
113 pos.x = model.getMin ().x + x * sizeInv.x * diff.x;
114 Complex c {pos.x, pos.y};
116 uint32_t iterations {};
118 for (; iterations < model.getIterations () && hypot (z.real (), z.imag ()) < 2.0;
120 z = mulAdd (z, z, c);
121 iterationResult[x] = iterations;
123 for (
auto x = 0u; x < size.x; ++x)
124 setPixel (x, iterationResult[x]);
128 template <
typename SetPixelProc>
129 inline void calculate (Point size,
const Model& model, SetPixelProc setPixel)
131 for (
auto y = 0u; y < size.y; ++y)
133 calculateLine (y, size, model, setPixel);
Definition: mandelbrot.h:19
Definition: dispatchlist.h:14
Definition: mandelbrot.h:25
Point structure.
Definition: cpoint.h:17
Definition: mandelbrot.h:12