Visualizing many points in a bounding box
in
Programming Questions
•
1 year ago
I have a bounding box (using P3D) and inside of it I test every pixel for a certain condition. If after some number of iterations it never reaches the condition then I put a point there, otherwise I put nothing. The bounding box is very small, it goes from -2 to 2 in the x, y, and z directions.
The problem is that the resulting image looks terrible. How would I go about setting this up so that I get an object that can receive lighting? Here is the code so far:
- float xmin = -2;
- float ymin = -2;
- float zmin = -2;
- float wh = 4;
- int boxLength = 200;
- int maxIterations = 1;
- int screenSize = 400;
- void setup() {
- size(screenSize, screenSize, P3D);
- noLoop();
- }
- void draw() {
- background(255);
- translate((screenSize-boxLength)/2, (screenSize-boxLength)/2);
- float xmax = xmin+wh;
- float ymax = ymin+wh;
- float zmax = zmin+wh;
- float dx = (xmax-xmin)/boxLength;
- float dy = (ymax-ymin)/boxLength;
- float dz = (zmax-zmin)/boxLength;
- float x = xmin;
- for (int i = 0; i < boxLength; i++) {
- float y = ymin;
- for (int j = 0; j < boxLength; j++) {
- float z = zmin;
- for (int k = 0; k < boxLength; k++) {
- float rad = sqrt(x*x+y*y+z*z);
- float phi = atan2(z, x);
- float phiLength = sqrt(x*x+z*z);
- float theta = atan2(phiLength, y);
- float nx = x;
- float ny = y;
- float nz = z;
- int n = 0;
- while (n < maxIterations) {
- float nRad = sqrt(nx*nx+ny*ny+nz*nz);
- float nPhi = atan2(nz, nx);
- float nPhiLength = sqrt(nx*nx+nz*nz);
- float nTheta = atan2(nPhiLength, ny);
- nx = cos(nPhi*2)*sin(nTheta*2)*nRad*nRad;
- ny = cos(nTheta*2)*nRad*nRad;
- nz = sin(nPhi*2)*sin(nTheta*2)*nRad*nRad;
- nx += cos(phi)*sin(theta)*rad;
- ny += cos(theta)*rad;
- nz += sin(phi)*sin(theta)*rad;
- if (nx*nx+ny*ny+nz*nz > 4) break;
- n++;
- }
- if (n == maxIterations) point(i, j, k);
- z += dz;
- }
- y += dy;
- }
- x += dx;
- }
- println("Done");
- }
Scroll down past the 2D Mandelbrot to see the 3D generalization
1