philsupertramp/game-math
Loading...
Searching...
No Matches
Fractals.h
Go to the documentation of this file.
1
110#pragma once
111
112#include "lin_alg/newton.h"
113
114
119{
120public:
122 double detail;
124 double xMin = -1.0, xMax = 1.0;
126 double yMin = -1.0, yMax = 1.0;
128 int maxIter = 100;
130 double tol = 1e-5;
131
140 NewtonFractal(double detailFactor = 1, double _min = -1.0, double _max = 1.0, int _maxIter = 100, double _tol = 1e-5)
141 : detail(detailFactor)
142 , xMin(_min)
143 , xMax(_max)
144 , yMin(_min)
145 , yMax(_max)
146 , maxIter(_maxIter)
147 , tol(_tol) { }
148
155 size_t n = fabs(xMax - xMin) / (1 / (detail > 10 ? detail : 10));
156 auto x_i = linspace(xMin, xMax, n).Transpose();
157 auto y_i = linspace(yMin, yMax, n).Transpose();
158
159 auto M = zeros(n, n);
160 std::vector<Matrix<double>> roots;
161
162 for(size_t i = 0; i < n; ++i) {
163 for(size_t j = 0; j < n; ++j) {
164 auto res = newton(fun, jac, { { x_i(i, 0) }, { y_i(j, 0) } }, tol, maxIter);
165 if(res.second == maxIter) {
166 M(i, j) = 0;
167 } else {
168 size_t index = 0;
169 for(size_t k = 0; k < roots.size(); ++k) {
170 if(norm(res.first - roots[k]) <= tol) { index = k; }
171 }
172 if(index == 0) {
173 index = roots.size();
174 roots.push_back(res.first);
175 }
176 M(i, j) = (double)index;
177 }
178 }
179 }
180 return M;
181 }
182
183private:
197 return Matrix<double>({ { pow(x(0, 0), 3) - 3 * x(0, 0) * x(1, 0) * x(1, 0) - 1 },
198 { -pow(x(1, 0), 3) + 3 * x(0, 0) * x(0, 0) * x(1, 0) } });
199 }
213 return Matrix<double>({ { 3 * (x(0, 0) * x(0, 0) - x(1, 0) * x(1, 0)), -6 * x(0, 0) * x(1, 0) },
214 { 6 * x(0, 0) * x(1, 0), 3 * x(0, 0) * x(0, 0) - 3 * x(1, 0) * x(1, 0) } });
215 }
216};
217
222{
223public:
225 size_t maxIters = 125;
227 double startX = -2.5, endX = 1.0;
229 double startY = -1.0, endY = 1.0;
231 size_t detail = 500;
232
233
238
239
245 auto stepWidthX = (endX - startX) / (double)detail;
246 auto stepWidthY = (endY - startY) / (double)detail;
247
249 for(size_t x = 0; x < detail; ++x) {
250 for(size_t y = 0; y < detail; ++y) {
251 M(x, y) = (double)fun((startX + stepWidthX * (double)x), (startY + stepWidthY * (double)y));
252 if(M(x, y) == (double)maxIters) { M(x, y) = 0; }
253 }
254 }
255 return M;
256 }
257
258private:
265 size_t fun(const double& real_c, const double& img_c) {
266 size_t iters = 0;
267 double real = 0;
268 double img = 0;
269
270 while(++iters < maxIters && real * real + img * img < 4) {
271 double temp = real;
272 real = real * real + real_c - img * img;
273 img = 2 * img * temp + img_c;
274 }
275
276 return iters;
277 }
278};
double pow(double x, int exponent)
Definition: Fractals.h:222
size_t maxIters
max number iterations for approximation of single value
Definition: Fractals.h:225
size_t detail
detail factor for approximation
Definition: Fractals.h:231
size_t fun(const double &real_c, const double &img_c)
Definition: Fractals.h:265
double endY
Definition: Fractals.h:229
Matrix< double > operator()()
Definition: Fractals.h:244
double startX
start and end value on x-axis
Definition: Fractals.h:227
Mandelbrot()
Definition: Fractals.h:237
double startY
start and end value on y-axis
Definition: Fractals.h:229
double endX
Definition: Fractals.h:227
Definition: Matrix.h:42
constexpr Matrix< T > Transpose() const
Definition: Matrix.h:256
Definition: Fractals.h:119
double yMax
Definition: Fractals.h:126
int maxIter
max number iterations for newton algorithm
Definition: Fractals.h:128
NewtonFractal(double detailFactor=1, double _min=-1.0, double _max=1.0, int _maxIter=100, double _tol=1e-5)
Definition: Fractals.h:140
static Matrix< double > jac(const Matrix< double > &x)
Definition: Fractals.h:212
double tol
tolerance used for newton algorithm
Definition: Fractals.h:130
Matrix< double > operator()() const
Definition: Fractals.h:154
static Matrix< double > fun(const Matrix< double > &x)
Definition: Fractals.h:196
double xMin
start and end value on x-axis
Definition: Fractals.h:124
double xMax
Definition: Fractals.h:124
double detail
detail factor for approximation
Definition: Fractals.h:122
double yMin
start and end value on y-axis
Definition: Fractals.h:126
std::pair< Matrix< double >, int > newton(const LinearEquation &f, const Jacobian &Df, const Matrix< double > &x0, double TOL, int maxIter)
Definition: newton.h:32
double norm(const Matrix< double > &in)
Matrix< double > zeros(size_t rows, size_t columns, size_t elements=1)
Matrix< double > linspace(double start, double end, unsigned long num_elements)