-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandel.cpp
93 lines (73 loc) · 2.52 KB
/
mandel.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
#include "mandel.h"
int mandel::maxIterations = 80;
std::complex<double> mandel::originalPoint; // Originally declared value to be (0,0), removed (unnecessary)
double mandel::magnitude(std::complex<double> num) {
return sqrt( pow(std::real(num),2) + pow(std::imag(num),2) );
}
std::complex<double> mandel::pointVal(std::complex<double> num) {
originalPoint = num;
for (unsigned int i = 0; i<maxIterations; ++i) {
num = num * num + originalPoint;
if (magnitude(num)>2) {
break;
}
}
// Reset values of orig point
return num;
}
int mandel::colorValue(std::complex<double> num) {
double iterationCount = 0; // using double to avoid static cast later
originalPoint = num;
double mag; // local variable to store magnitude
for (unsigned int i = 0; i<maxIterations; ++i) {
++iterationCount;
num = num*num + originalPoint;
mag = magnitude(num);
if (mag>2){
break;
}
}
if (mag<=2) {
return 0; // Value 0 in HSV is black, represents value in Mandelbrot Set
}
else {
return 255*(iterationCount/maxIterations); // Solution by [CG]Maxime
// iteration count definitionally between 0 and max Iterations
// This means iterationCount/maxIterations is a number between 0 and 1
// Therefore this returns brightness values banded from black to white
}
}
void mandel::exportImg(double xMin, double xMax, double yMin, double yMax,
QColor color, int imgWidth,
int imgHeight, int numIterations, std::string path) {
// Implement export function here
int r, g, b;
int originalmandelIter = mandel::getMaxIterations();
mandel::setMaxIterations(numIterations);
double deltaX = std::abs(xMax - xMin)/imgWidth;
double deltaY = std::abs(yMax-yMin)/imgHeight;
double mandelVal;
std::complex<double> point(0.0,0.0);
std::ofstream img(path);
img << "P3" << std::endl;
img << imgWidth << " " << imgHeight << std::endl;
img << "255" << std::endl;
for (int y = 0; y<imgHeight; ++y) {
for (int x = 0; x<imgWidth; ++x) {
point = std::complex<double>(xMin + x*deltaX,yMax-y*deltaY);
color.setHsv(color.hue(),color.saturation(),colorValue(point));
color.getRgb(&r,&g,&b);
img << r << " " << g << " " << b << std::endl;
}
}
mandel::setMaxIterations(originalmandelIter);
}
void mandel::setMaxIterations(int n) {
maxIterations = n;
}
void mandel::printmaxIterations() {
std::cout << "maxIterations: " << maxIterations << std::endl;
}
int mandel::getMaxIterations() {
return maxIterations;
}