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
|
#include "fractals.h"
#include "grids.h"
#include "util.h"
/*
* Computes the number of iterations it takes for a point z0 to diverge
* if the return value is equal to max_iterations, the point lies within the mandelbrot set
* This is an implementation the escape algorithm
*/
size_t mandelbrot(const double complex z0, const size_t max_iterations) {
double complex z = z0;
size_t iteration = 0;
while (cabs(z) <= 2 && iteration < max_iterations) {
z = z * z + z0;
iteration++;
}
return iteration;
}
/*
* Fills a grid with the a complex sit
*/
void mandelbrot_grid(grid_t* grid, vec2 resolution, const size_t iterations){
if(!grid || !grid->data) return;
const size_t size = grid->size;
set_grid(grid, 0); //unnecessary step
size_t* data = grid->data;
for(size_t i = 0; i < size; i++){
data[i] = mandelbrot(lattice_to_complex(i, resolution), iterations);
}
}
/*
* Computes the number of iterations it takes for a point z0 to diverge
* if the return value is equal to max_iterations, the point lies within the multibrot set
* This is implementation closely matches mandelbrot
* Note, only positive integer powers are supported
*/
size_t multibrot(const double complex z0, const size_t max_iterations, const uintmax_t d){
double complex z = z0;
double complex ztemp;
size_t iteration = 0;
while(cabs(z) <= 2 && iteration < max_iterations){
ztemp = z;
for(size_t i = 0; i < d; i ++){
ztemp *= ztemp;
}
z = ztemp + z0;
iteration++;
}
return iteration;
}
/*
* Computes ????? for a julia set
* implementation of https://en.wikipedia.org/wiki/Julia_set#Pseudocode
*/
size_t julia(const double R, const double complex z0, const double complex c, const size_t max_iterations){
//FIXME: I'm notsure if this is currently implemented correctly
if(R*R - R >= cabs(z0)) return 0;
double complex z = z0;
size_t iteration = 0;
while(cabs(z) < R && iteration < max_iterations){
z = z * z + c;
iteration++;
}
return iteration;
}
|