Skip to content
This repository was archived by the owner on Jul 5, 2022. It is now read-only.

Commit d918217

Browse files
Coding Challenge 168: The mandelbulb! (#3563)
* mandelbulb challenge code * voxels example * starting markdown page * Fix title mandelbulb page * Add links and videos to mandelbulb * Update 168-mandelbulb.md * Fix spacing in 168 md * remove space line 36 mandelbulb Co-authored-by: Kobe Liesenborgs <[email protected]>
1 parent dbe4b6b commit d918217

File tree

11 files changed

+75365
-4
lines changed

11 files changed

+75365
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import peasy.*;
2+
3+
int DIM = 128;
4+
PeasyCam cam;
5+
ArrayList<PVector> mandelbulb = new ArrayList<PVector>();
6+
7+
void setup() {
8+
size(600, 600, P3D);
9+
cam = new PeasyCam(this, 600);
10+
11+
StringList points = new StringList();
12+
13+
for (int i = 0; i < DIM; i++) {
14+
for (int j = 0; j < DIM; j++) {
15+
boolean edge = false;
16+
for (int k = 0; k < DIM; k++) {
17+
float x = map(i, 0, DIM, -1, 1);
18+
float y = map(j, 0, DIM, -1, 1);
19+
float z = map(k, 0, DIM, -1, 1);
20+
PVector zeta = new PVector(0, 0, 0);
21+
int n = 8;
22+
int maxiterations = 20;
23+
int iteration = 0;
24+
while (true) {
25+
Spherical c = spherical(zeta.x, zeta.y, zeta.z);
26+
float newx = pow(c.r, n) * sin(c.theta*n) * cos(c.phi*n);
27+
float newy = pow(c.r, n) * sin(c.theta*n) * sin(c.phi*n);
28+
float newz = pow(c.r, n) * cos(c.theta*n);
29+
zeta.x = newx + x;
30+
zeta.y = newy + y;
31+
zeta.z = newz + z;
32+
iteration++;
33+
if (c.r > 2) {
34+
if (edge) {
35+
edge = false;
36+
}
37+
break;
38+
}
39+
if (iteration > maxiterations) {
40+
if (!edge) {
41+
edge = true;
42+
mandelbulb.add(new PVector(x, y, z));
43+
}
44+
break;
45+
}
46+
}
47+
}
48+
}
49+
}
50+
51+
String[] output = new String[mandelbulb.size()];
52+
for (int i = 0; i < output.length; i++) {
53+
PVector v = mandelbulb.get(i);
54+
output[i] = v.x + " " + v.y + " " + v.z;
55+
}
56+
saveStrings("mandelbulb.txt", output);
57+
58+
}
59+
60+
class Spherical {
61+
float r, theta, phi;
62+
Spherical(float r, float theta, float phi) {
63+
this.r = r;
64+
this.theta = theta;
65+
this.phi = phi;
66+
}
67+
}
68+
69+
Spherical spherical(float x, float y, float z) {
70+
float r = sqrt(x*x + y*y + z*z);
71+
float theta = atan2( sqrt(x*x+y*y), z);
72+
float phi = atan2(y, x);
73+
return new Spherical(r, theta, phi);
74+
}
75+
76+
void draw() {
77+
background(0);
78+
rotateX(PI/4);
79+
rotateY(-PI/3);
80+
for (PVector v : mandelbulb) {
81+
stroke(255);
82+
strokeWeight(1);
83+
point(v.x*200, v.y*200, v.z*200);
84+
}
85+
}

0 commit comments

Comments
 (0)