-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMandelBulb.pde
85 lines (76 loc) · 2.12 KB
/
MandelBulb.pde
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
import peasy.*;
int DIM = 128;
PeasyCam cam;
ArrayList<PVector> mandelbulb = new ArrayList<PVector>();
void setup() {
size(600, 600, P3D);
cam = new PeasyCam(this, 600);
StringList points = new StringList();
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
boolean edge = false;
for (int k = 0; k < DIM; k++) {
float x = map(i, 0, DIM, -1, 1);
float y = map(j, 0, DIM, -1, 1);
float z = map(k, 0, DIM, -1, 1);
PVector zeta = new PVector(0, 0, 0);
int n = 8;
int maxiterations = 20;
int iteration = 0;
while (true) {
Spherical c = spherical(zeta.x, zeta.y, zeta.z);
float newx = pow(c.r, n) * sin(c.theta*n) * cos(c.phi*n);
float newy = pow(c.r, n) * sin(c.theta*n) * sin(c.phi*n);
float newz = pow(c.r, n) * cos(c.theta*n);
zeta.x = newx + x;
zeta.y = newy + y;
zeta.z = newz + z;
iteration++;
if (c.r > 2) {
if (edge) {
edge = false;
}
break;
}
if (iteration > maxiterations) {
if (!edge) {
edge = true;
mandelbulb.add(new PVector(x, y, z));
}
break;
}
}
}
}
}
String[] output = new String[mandelbulb.size()];
for (int i = 0; i < output.length; i++) {
PVector v = mandelbulb.get(i);
output[i] = v.x + " " + v.y + " " + v.z;
}
saveStrings("mandelbulb.txt", output);
}
class Spherical {
float r, theta, phi;
Spherical(float r, float theta, float phi) {
this.r = r;
this.theta = theta;
this.phi = phi;
}
}
Spherical spherical(float x, float y, float z) {
float r = sqrt(x*x + y*y + z*z);
float theta = atan2( sqrt(x*x+y*y), z);
float phi = atan2(y, x);
return new Spherical(r, theta, phi);
}
void draw() {
background(0);
rotateX(PI/4);
rotateY(-PI/3);
for (PVector v : mandelbulb) {
stroke(#ed9fe8);
strokeWeight(1);
point(v.x*200, v.y*200, v.z*200);
}
}