-
Notifications
You must be signed in to change notification settings - Fork 1
/
mandelframe.cpp
80 lines (57 loc) · 1.94 KB
/
mandelframe.cpp
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
#include "mandelframe.h"
#include "consts.h"
MandelFrame::MandelFrame(long double x_left, long double x_right, long double y_top, long double y_bottom)
: _x_left(x_left), _x_right(x_right), _y_top(y_top), _y_bottom(y_bottom),
x_delta(x_right - x_left), y_delta(y_top - y_bottom),
x_tick(x_delta/SIZE_X), y_tick(y_delta/SIZE_Y)
{
z = new ZMatrix;
this->setPlaneValues();
}
MandelFrame::~MandelFrame() {
delete z;
z = nullptr;
}
// set the complex numbers in ZMatrix as interpolated grid based on MandelFrame dimensions and SIZE_X, SIZE_Y
void MandelFrame::setPlaneValues() {
int index;
for (int x = 0; x < SIZE_X; x++) {
for (int y = 0; y < SIZE_Y; y++) {
index = z->getIndex(x, y);
z->setElement(index, (_x_left + (x_tick * x)), (_y_bottom + (y_tick * y)));
// compute iteration cutoff point for each z in ZMatrix->plane[], store result in ZMatrix->count[]
z->setCount(index, mandelIterate(index, iterations, MAX_ABS));
}
}
}
// reset dimensions, deltas and ticks values upon initialisation and after zoom
void MandelFrame::setDeltasAndTicks(long double x_left, long double x_right, long double y_top, long double y_bottom) {
_x_left = x_left;
_x_right = x_right;
_y_top = y_top;
_y_bottom = y_bottom;
x_delta = _x_right - _x_left;
y_delta = _y_top - _y_bottom;
x_tick = x_delta / SIZE_X;
y_tick = y_delta / SIZE_Y;
}
// calculate iteration cutoff point - z is excluded from Mandelbrot set as soon as magnitude > 2, iteration count is recorded
int MandelFrame::mandelIterate(int index, int max_iterations, double max_abs) {
Complex c = z->getElement(index);
Complex temp;
int counter = 0;
bool escaped{ false };
for (int i = 0; i < max_iterations; i++) {
counter++;
temp *= temp;
temp += c;
if (temp.abs() > max_abs) {
escaped = true;
break;
}
}
// record -1 if max_iterations reached and z is still in set - assumed member of Mandelbrot set
if (!escaped)
counter = -1;
return counter;
}