-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractal.cpp
More file actions
224 lines (188 loc) · 6.51 KB
/
fractal.cpp
File metadata and controls
224 lines (188 loc) · 6.51 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "fractal.h"
#include "sierpinski.h"
#include <math.h>
#include <QColor>
// code: https://lodev.org/cgtutor/juliamandelbrot.html
typedef enum {
K_JULIA_SET = 0,
K_MANDELBROT = 1
} FractalKind;
struct FractalParams {
double zoom;
double moveX;
double moveY;
int maxIterations;
};
class Fractal::Priv
{
public:
explicit Priv (int _w, int _h) : w(_w), h(_h), kind(K_JULIA_SET) {
initParams();
}
void initParams() {
params.zoom = 1.0;
params.moveX = 0.0;
params.moveY = 0.0;
params.maxIterations = 300;
}
void drawJuliaSet(QPainter *p) {
double cRe, cIm; //real and imaginary part of the constant c, determinate shape of the Julia Set
double newRe, newIm, oldRe, oldIm; //real and imaginary parts of new and old
QColor color; //the RGB color value for the pixel
int maxIterations = 300; //after how much iterations the function should stop
//pick some values for the constant c, this determines the shape of the Julia Set
cRe = -0.7;
cIm = 0.27015;
//loop through every pixel
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
//calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
newRe = 1.5 * (x - w / 2) / (0.5 * params.zoom * w) + params.moveX;
newIm = (y - h / 2) / (0.5 * params.zoom * h) + params.moveY;
//i will represent the number of iterations
int i;
//start the iteration process
for (i = 0; i < maxIterations; i++) {
//remember value of previous iteration
oldRe = newRe;
oldIm = newIm;
//the actual iteration, the real and imaginary part are calculated
newRe = oldRe * oldRe - oldIm * oldIm + cRe;
newIm = 2 * oldRe * oldIm + cIm;
//if the point is outside the circle with radius 2: stop
if ((newRe * newRe + newIm * newIm) > 4) break;
}
//use color model conversion to get rainbow palette, make brightness black if maxIterations reached
color.setHsv(i % 256, 255, 255 * (i < maxIterations));
//draw the pixel
p->setPen(color);
p->drawPoint(x, y);
}
}
}
void drawMandelbrot(QPainter *p) {
//each iteration, it calculates: newz = oldz*oldz + p, where p is the current pixel, and oldz stars at the origin
double pr, pi; //real and imaginary part of the pixel p
double newRe, newIm, oldRe, oldIm; //real and imaginary parts of new and old z
QColor color; //the RGB color value for the pixel
int maxIterations = 300;//after how much iterations the function should stop
//loop through every pixel
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
//calculate the initial real and imaginary part of z, based on the pixel location and zoom and position values
pr = 1.5 * (x - w / 2) / (0.5 * params.zoom * w) + params.moveX;
pi = (y - h / 2) / (0.5 * params.zoom * h) + params.moveY;
newRe = newIm = oldRe = oldIm = 0; //these should start at 0,0
//"i" will represent the number of iterations
int i;
//start the iteration process
for (i = 0; i < maxIterations; i++) {
//remember value of previous iteration
oldRe = newRe;
oldIm = newIm;
//the actual iteration, the real and imaginary part are calculated
newRe = oldRe * oldRe - oldIm * oldIm + pr;
newIm = 2 * oldRe * oldIm + pi;
//if the point is outside the circle with radius 2: stop
if((newRe * newRe + newIm * newIm) > 4) break;
}
//use color model conversion to get rainbow palette, make brightness black if maxIterations reached
color.setHsv(i % 256, 255, 255 * (i < maxIterations));
//draw the pixel
p->setPen(color);
p->drawPoint(x, y);
}
}
}
QVector<QRgb> palette;
int w, h;
FractalKind kind;
struct FractalParams params;
};
Fractal::Fractal(int w, int h) : Effect (w, h)
{
d = new Priv(w, h);
}
Fractal::~Fractal()
{
delete d;
}
void Fractal::create()
{
d->initParams();
}
void Fractal::destroy()
{
}
void Fractal::update()
{
}
int Fractal::defaultRefreshRate()
{
return 0;
}
const QVector<QRgb>& Fractal::palette() const
{
return d->palette;
}
bool Fractal::paint(QPainter *painter) const
{
if (!painter)
return false;
switch (d->kind) {
case K_JULIA_SET:
d->drawJuliaSet(painter);
break;
case K_MANDELBROT:
d->drawMandelbrot(painter);
break;
}
return true;
}
QPair<int, QVector<QString>> Fractal::fxKindList() const
{
QVector<QString> v;
v.append("Julia Set");
v.append("Mandelbrot");
return QPair<int, QVector<QString>>(d->kind, v);
}
void Fractal::setFxKind(int kind)
{
switch (kind) {
case K_JULIA_SET:
case K_MANDELBROT:
d->kind = FractalKind(kind);
break;
}
}
//#include <QDebug>
bool Fractal::keyPressed(FxKey key)
{
switch (key) {
case KEY_MINUS:
d->params.zoom /= qMax(0.005, pow(1.015, d->params.zoom));
//qDebug() << "New zoom: " << d->params.zoom;
break;
case KEY_PLUS:
d->params.zoom *= qMax(0.005, pow(1.015, d->params.zoom));
//qDebug() << "New zoom: " << d->params.zoom;
break;
case KEY_LEFT:
d->params.moveX -= qMax(0.001, 0.0003 * d->params.moveX / d->params.zoom);
//qDebug() << "New X: " << d->params.moveX;
break;
case KEY_RIGHT:
d->params.moveX += qMax(0.001, 0.0003 * d->params.moveX / d->params.zoom);
//qDebug() << "New X: " << d->params.moveX;
break;
case KEY_DOWN:
d->params.moveY += qMax(0.001, 0.0003 * d->params.moveY / d->params.zoom);
//qDebug() << "New Y: " << d->params.moveY;
break;
case KEY_UP:
d->params.moveY -= qMax(0.001, 0.0003 * d->params.moveY / d->params.zoom);
//qDebug() << "New Y: " << d->params.moveY;
break;
}
return true;
}