Normand Briere
2019-09-10 a7e7618fb914f8b919a17daf6c020b860c5d11c9
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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 *
 * @author nbriere
 */
public class MandelBulb extends Object3D
{
    public boolean inside(double x, double y, double z, boolean transform) // , double d)
    {
        double d = 1; // Oops not so easy
        
        double posX = x;
        double posY = y;
        double posZ = z;
 
        double dr = 1.0;
        double r = 0.0;
 
        for (int i = 0; i < 10; i++)
        {
            double dist2xy = x*x + y*y;
            r = Math.sqrt(dist2xy + z*z);
            if (r > 10000)
            {
                break;
            }
 
            // convert to polar coordinates
            //double theta = Math.acos(z / r);
            double theta = Math.atan2(-z, Math.sqrt(dist2xy));
            double phi = Math.atan2(y, x);
            double zr = Math.pow(r, 8);
            dr = zr / r * 8 * dr + 1.0;
 
            // scale and rotate the point
            theta = theta * 8;
            phi = phi * 8;
            
            // convert back to cartesian coordinates
            double sintheta = zr * Math.sin(theta);
            x = sintheta * Math.cos(phi);
            y = Math.sin(phi) * sintheta;
            z = zr * Math.cos(theta);
 
            x += posX;
            y += posY;
            z += posZ;
        }
        
        return ((0.5 * Math.log(r) * r / dr) < d);
    }
}