Last active
May 20, 2024 20:25
-
-
Save nickgs/50f8c417d7d3eab4d51a9eb94c398592 to your computer and use it in GitHub Desktop.
Mandelbrot Vex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Base Functions | |
function int Mandel(float x0, y0, z0; int imax) { | |
float x, y, z, xnew, ynew, znew; | |
int i; | |
x = x0; | |
y = y0; | |
z = z0; | |
for(i = 0; i < imax; i++) { | |
xnew = x*x - y*y + x0; | |
ynew = 2*x * y + y0; | |
if(xnew*xnew + ynew*ynew > 8) { | |
return(i); | |
} | |
x = xnew; | |
y = ynew; | |
} | |
return imax; | |
} | |
/* Mandelbulb | |
r = sqrt(x*x + y*y + z*z ) | |
theta = atan2(sqrt(x*x + y*y) , z) | |
phi = atan2(y,x) | |
newx = r^n * sin(theta*n) * cos(phi*n) | |
newy = r^n * sin(theta*n) * sin(phi*n) | |
newz = r^n * cos(theta*n) | |
*/ | |
function int MandelBulb(float x0, y0, z0; int imax) { | |
float x, y, z, xnew, ynew, znew, n = ch('n'), r, theta, phi; | |
int i; | |
x = x0; | |
y = y0; | |
z = z0; | |
for(i = 0; i < imax; i++) { | |
//xnew = x*x - y*y + x0; | |
//ynew = 2*x * y + y0; | |
r = sqrt(x*x + y*y + z*z ); | |
theta = atan2(sqrt(x*x + y*y) , z); | |
phi = atan2(y,x); | |
xnew = pow(r,n) * sin(theta*n) * cos(phi*n) + x0; | |
ynew = pow(r,n) * sin(theta*n) * sin(phi*n) + y0; | |
znew = pow(r,n) * cos(theta*n) + z0; | |
if(xnew*xnew + ynew*ynew + znew*znew > 8) { | |
return(i); | |
} | |
x = xnew; | |
y = ynew; | |
z = znew; | |
} | |
return imax; | |
} | |
// 3D Density | |
int maxiter = 8; | |
if(MandelBulb(@P.x, @P.y, @P.z, maxiter) < maxiter) { | |
@density = 0.0; | |
} | |
else { | |
@density = 1.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment