forked from runezor/PiArtFrame
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mandelbrot.cpp
213 lines (184 loc) · 6.42 KB
/
mandelbrot.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "mandelbrot.hpp"
#include "GUI_Paint.h"
#include <algorithm>
#include <cmath>
#include <vector>
#include <tuple>
#include <random>
using namespace std;
void MandelbrotSet::InitMandelbrotSet()
{
w = 4;
h = 2;
x = -1;
y = 0;
rendered = NULL;
renderedResX = 0;
renderedResY = 0;
}
void MandelbrotSet::SetRender(UBYTE* image)
{
rendered = image;
}
void MandelbrotSet::Render(UWORD xResolution, UWORD yResolution)
{
// Approximation for number of iterations
int iter = (50 + max(0.0, -log10(w)) * 100 );
vector<vector<bool>> columns;
for(int i = yResolution-1; i>=0; --i)
{
vector<bool> rows;
for(int j = 0; j < xResolution; ++j)
{
double p_x = this->x - this->w / 2.0 + (double)j / (double)xResolution * this->w;
double p_y = this->y - this->h / 2.0 + (double)(i+1) / (double)yResolution * this->h;
rows.emplace_back(IsMandelPoint(p_x, p_y, iter));
}
columns.emplace_back(rows);
}
renderedResX = xResolution;
renderedResY = yResolution;
// Update rendered image
for(unsigned int y = 0; y < columns.size(); ++y)
{
auto row = columns[y];
for(unsigned int x = 0; x < row.size(); ++x)
{
auto bitSet = row[x];
if(bitSet)
{
Paint_SetPixel(x, y, WHITE);
}
else
{
Paint_SetPixel(x, y, BLACK);
}
}
}
}
bool MandelbrotSet::IsMandelPoint(double fX, double fY, int iterations)
{
double z_x = fX;
double z_y = fY;
for(int i = 0; i < iterations; ++i)
{
double z_x_old = z_x;
z_x = z_x * z_x - z_y * z_y + fX;
z_y = 2.0 * z_x_old * z_y + fY;
auto sumSquared = pow(z_x, 2) + pow(z_y, 2);
if (sumSquared > 4)
{
return true;
}
}
return false;
}
unsigned long long MandelbrotSet::GetUniformnessOfArea(double fW, double fH, int xOffset, int yOffset, int wDiv, int hDiv)
{
unsigned long long uniformness = 0;
for(int wStart = 0; wStart < wDiv; ++wStart)
{
for(int hStart = 0; hStart < hDiv; ++hStart)
{
if(IsAreaUniform(xOffset, yOffset, fW, fH, wDiv, hDiv, wStart, hStart))
{
++uniformness;
}
}
}
return uniformness;
}
bool MandelbrotSet::IsAreaUniform(int xOffset, int yOffset, double fW, double fH, int wDiv, int hDiv, double wStart, double hStart)
{
int yInit = yOffset + static_cast<int>(fH / hDiv) * hStart;
int xInit = xOffset + static_cast<int>(fW / wDiv) * wStart;
auto firstPoint = Paint_GetPixel(xInit , yInit);
for(unsigned int i = 0; i < static_cast<unsigned int>(fW / wDiv); ++i )
{
for(unsigned int j = 0; j < static_cast<unsigned int>(fH / hDiv); ++j )
{
int yTest = yOffset + static_cast<int>(fH / hDiv) * hStart + j;
int xTest = xOffset + static_cast<int>(fW / wDiv) * wStart + i;
auto testPoint = Paint_GetPixel(xTest , yTest);
if(testPoint != firstPoint)
return false;
}
}
return true;
}
double MandelbrotSet::GetImprovedUniformnessOfArea(double fW, double fH, int xOffset, int yOffset, int wDiv, int hDiv)
{
unsigned long long numWhite = 0;
unsigned long long numBlack = 0;
double totalPixels = fW*fH;
for(int wStart = 0; wStart < fW; ++wStart)
{
for(int hStart = 0; hStart < fH; ++hStart)
{
int xPointIndex = xOffset+wStart;
int yPointIndex = yOffset+hStart;
if(Paint_GetPixel(xPointIndex , yPointIndex) == WHITE)
numWhite++;
else
numBlack++;
}
}
return max((double)numWhite / totalPixels, double(numBlack) / totalPixels);
}
void MandelbrotSet::ZoomOnInterestingArea()
{
tuple<double, double, double> choice;
vector<tuple<double, double, double>> choices;
auto uniformness = GetImprovedUniformnessOfArea(this->renderedResX / 2, this->renderedResY / 2, 0, 0, 2, 2);
choice = {this->x - this->w/4, this->y + this->h/4, uniformness};
choices.emplace_back(choice);
uniformness = GetImprovedUniformnessOfArea(this->renderedResX / 2, this->renderedResY / 2, this->renderedResX / 2, 0, 2, 2);
choice = {this->x + this->w/4, this->y + this->h/4, uniformness};
choices.emplace_back(choice);
uniformness = GetImprovedUniformnessOfArea(this->renderedResX / 2, this->renderedResY / 2, 0, this->renderedResY / 2, 2, 2);
choice = {this->x - this->w/4, this->y - this->h/4, uniformness};
choices.emplace_back(choice);
uniformness = GetImprovedUniformnessOfArea(this->renderedResX / 2, this->renderedResY / 2, this->renderedResX / 2, this->renderedResY / 2, 2, 2);
choice = {this->x + this->w/4, this->y - this->h/4, uniformness};
choices.emplace_back(choice);
w = w / 2.0;
h = h / 2.0;
auto lessUniformChoices = choices;
lessUniformChoices.erase(std::remove_if(
lessUniformChoices.begin(),
lessUniformChoices.end(),
[](const tuple<double, double, double>& x) {
return (std::get<2>(x) >= 0.85);
}), lessUniformChoices.end());
auto topTierChoices = choices;
topTierChoices.erase(std::remove_if(
topTierChoices.begin(),
topTierChoices.end(),
[](const tuple<double, double, double>& x) {
return (std::get<2>(x) >= 0.75);
}), topTierChoices.end());
// Seed
random_device rd;
mt19937 g(rd());
if(topTierChoices.size() > 0)
{
shuffle(topTierChoices.begin(), topTierChoices.end(), g);
auto selection = topTierChoices[0];
this->x = get<0>(selection);
this->y = get<1>(selection);
}
else if (lessUniformChoices.size() > 0)
{
shuffle(lessUniformChoices.begin(), lessUniformChoices.end(), g);
auto selection = lessUniformChoices[0];
this->x = get<0>(selection);
this->y = get<1>(selection);
}
else
{
shuffle(choices.begin(), choices.end(), g);
auto selection = choices[0];
this->x = get<0>(selection);
this->y = get<1>(selection);
}
}