-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrotfractal.cpp
198 lines (175 loc) · 6.69 KB
/
mandelbrotfractal.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "mandelbrotfractal.h"
MandelbrotFractal::MandelbrotFractal(double minReal, double maxReal, double minImag,
double maxImag, int iter, int ix, int iy,
int width, int height)
: AbstractFractal(ix, iy, width, height),
m_minReal(minReal), m_maxReal(maxReal),
m_minImag(minImag), m_maxImag(maxImag),
// m_zMinReal(minReal), m_zMaxReal(maxReal),
// m_zMinImag(minImag), m_zMaxImag(maxImag),
m_iter(iter) {
m_realFactor = (m_maxReal-m_minReal)/(getWidth()-1);
m_imagFactor = (m_maxImag-m_minImag)/(getHeight()-1);
setZoomRect(QRect(QPoint(), QPoint(getWidth(), getHeight())));
}
MandelbrotFractal::MandelbrotFractal()
: AbstractFractal(0, 0, 0, 0),
m_minReal(-2.2), m_maxReal(1.2),
m_minImag(-1.0), m_maxImag(1.0),
// m_zMinReal(0), m_zMaxReal(0),
// m_zMinImag(0), m_zMaxImag(0),
m_iter(100) {
}
void MandelbrotFractal::drawMe(QPainter *painter) {
QColor color = Qt::white;
for (int y = 0; y < getHeight(); y++) {
double c_imag = m_maxImag - (y*m_imagFactor);
for (int x = 0; x < getWidth(); x++) {
double c_real = m_minReal + (x*m_realFactor);
double z_imag = c_imag;
double z_real = c_real;
bool isInside = true;
int k;
for (k = 0; k < m_iter; k++) {
double z_real2 = z_real*z_real;
double z_imag2 = z_imag*z_imag;
if (z_real2 + z_imag2 > 4) {
isInside = false;
break;
}
z_imag = 2*z_real*z_imag + c_imag;
z_real = z_real2 - z_imag2 + c_real;
}
if (isInside) {
painter->setPen(Qt::black);
painter->drawPoint(x, y);
} else {
double halfIter = (double)m_iter/2.0;
if (k < (m_iter/2)) {
double cFactor = (k/halfIter)*255.0;
color.setRgb(cFactor, 0, 0);
painter->setPen(color);
painter->drawPoint(x,y);
} else {
double cFactor = ((k-halfIter)/halfIter)*255.0;
color.setRgb(255, cFactor, cFactor);
painter->setPen(color);
painter->drawPoint(x,y);
}
}
}
}
m_redraw = false;
}
void MandelbrotFractal::setMinReal(double minReal)
{
// m_minReal = minReal;
// if (m_zMinReal == 0) m_zMinReal = m_minReal;
// m_zMinReal = ((getZoomRect().x()/(double)getWidth())*(m_zMaxReal-m_zMinReal))+m_zMinReal;
// m_realFactor = (m_zMaxReal-m_zMinReal)/(getWidth()-1);
// m_redraw = true;
// if (m_zMinReal == 0) m_zMinReal = minReal;
// if (m_minReal == minReal) return;
m_minReal = minReal;
m_realFactor = (m_maxReal-m_minReal)/(getWidth()-1);
m_redraw = true;
// resetZoom();
}
void MandelbrotFractal::setMaxReal(double maxReal)
{
// m_maxReal = maxReal;
// if (m_zMaxReal == 0) m_zMaxReal = m_maxReal;
// m_zMaxReal = (((getZoomRect().x()+getZoomRect().width())/(double)getWidth()) *
// (m_zMaxReal-m_zMinReal))+m_zMinReal;
// m_realFactor = (m_zMaxReal-m_zMinReal)/(getWidth()-1);
// m_redraw = true;
// if (m_zMaxReal == 0) m_zMaxReal = maxReal;
// if (m_maxReal == maxReal) return;
m_maxReal = maxReal;
m_realFactor = (m_maxReal-m_minReal)/(getWidth()-1);
m_redraw = true;
// resetZoom();
}
void MandelbrotFractal::setMinImag(double minImag)
{
// m_minImag = minImag;
// if (m_zMinImag == 0) m_zMinImag = m_minImag;
// m_zMinImag = m_zMaxImag - ((getZoomRect().y()+getZoomRect().height())/(double)getHeight()*
// (m_zMaxImag-m_zMinImag));
// m_imagFactor = (m_zMaxImag-m_zMinImag)/(getHeight()-1);
// m_redraw = true;
// if (m_zMinImag == 0) m_zMinImag = minImag;
// if (m_minImag == minImag) return;
m_minImag = minImag;
m_imagFactor = (m_maxImag-m_minImag)/(getHeight()-1);
m_redraw = true;
// resetZoom();
}
void MandelbrotFractal::setMaxImag(double maxImag)
{
// m_maxImag = maxImag;
// if (m_zMaxImag == 0) m_zMaxImag = m_maxImag;
// m_zMaxImag = m_zMaxImag - ((getZoomRect().y()/(double)getHeight())*(m_zMaxImag-m_zMinImag));
// m_imagFactor = (m_maxImag-m_minImag)/(getHeight()-1);
// m_redraw = true;
// if (m_zMaxImag == 0) m_zMaxImag = maxImag;
// if (m_maxImag == maxImag) return;
m_maxImag = maxImag;
m_imagFactor = (m_maxImag-m_minImag)/(getHeight()-1);
m_redraw = true;
// resetZoom();
}
void MandelbrotFractal::setHeight(int height)
{
AbstractFractal::setHeight(height);
// m_imagFactor = (m_zMaxImag-m_zMinImag)/(getHeight()-1);
m_imagFactor = (m_maxImag-m_minImag)/(getHeight()-1);
m_redraw = true;
}
void MandelbrotFractal::setWidth(int width)
{
AbstractFractal::setWidth(width);
// m_realFactor = (m_zMaxReal-m_zMinReal)/(getWidth()-1);
m_realFactor = (m_maxReal-m_minReal)/(getWidth()-1);
m_redraw = true;
}
void MandelbrotFractal::setZoomRect(QRect rect)
{
// AbstractFractal::setZoomRect(rect);
// double totalImag = (m_zMaxImag-m_zMinImag);
// double totalReal = (m_zMaxReal-m_zMinReal);
// m_zMaxReal = (((rect.x()+rect.width())/(double)getWidth()) *
// totalReal)+m_zMinReal;
// m_zMinReal = ((rect.x()/(double)getWidth())*totalReal)+m_zMinReal;
// m_zMinImag = m_zMaxImag - ((rect.y()+rect.height())/(double)getHeight()*
// totalImag);
// m_zMaxImag = m_zMaxImag - ((rect.y()/(double)getHeight())*totalImag);
// m_imagFactor = (m_zMaxImag-m_zMinImag)/(getHeight()-1);
// m_realFactor = (m_zMaxReal-m_zMinReal)/(getWidth()-1);
// m_redraw = true;
AbstractFractal::setZoomRect(rect);
double totalImag = (m_maxImag-m_minImag);
double totalReal = (m_maxReal-m_minReal);
m_maxReal = (((rect.x()+rect.width())/(double)getWidth()) *
totalReal)+m_minReal;
m_minReal = ((rect.x()/(double)getWidth())*totalReal)+m_minReal;
m_minImag = m_maxImag - ((rect.y()+rect.height())/(double)getHeight()*
totalImag);
m_maxImag = m_maxImag - ((rect.y()/(double)getHeight())*totalImag);
m_imagFactor = (m_maxImag-m_minImag)/(getHeight()-1);
m_realFactor = (m_maxReal-m_minReal)/(getWidth()-1);
emit zoomed();
m_redraw = true;
}
void MandelbrotFractal::resetZoom()
{
// m_zMinReal = m_minReal;
// m_zMaxReal = m_maxReal;
// m_zMinImag = m_minImag;
// m_zMaxImag = m_maxImag;
m_minReal = -2.2;
m_maxReal = 1.2;
m_minImag = -1.0;
m_maxImag = 1.0;
setZoomRect(QRect(QPoint(), QPoint(getWidth(), getHeight())));
}