0

The equation for generating a Mandelbrot fractal is Zn+1 = Zn^2+C. The issue is that, in a computer program, C is used for zoom/resolution and location on screen. My question is, how can I make it so that I can get a fractal like this:

Wolfram

(equation f(z) = sin(z/c), z0 = c )

cassinioval

My code (from Rosetta Code):

public class MandelbrotSet extends JFrame {
    private static final long serialVersionUID = 5513426498262284949L;
    private final int MAX_ITER = 570;
    private final double ZOOM = 150;
    private BufferedImage image;
    private double zx, zy, cX, cY, tmp;

    public MandelbrotSet() {
        super("Mandelbrot Set");
        setBounds(100, 100, 800, 600);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y < getHeight(); y++) {
            for (int x = 0; x < getWidth(); x++) {
                zx = zy = 0;
                cX = (x - 400) / ZOOM;
                cY = (y - 300) / ZOOM;
                int iter = MAX_ITER;

                while (zx * zx + zy * zy < 4 && iter > 0) {
                    tmp = zx * zx - zy * zy + cX;
                    zy = 2.0 * zx * zy + cY;
                    zx = tmp;
                    iter--;
                }

                image.setRGB(x, y, iter | (iter << 8));
            }
        }
    }

        @Override
        public void paint(Graphics g) {
            g.drawImage(image, 0, 0, this);
        }

    public static void main(String[] args) {
        new MandelbrotSet().setVisible(true);;
    }
}
coder
  • 12,832
  • 5
  • 39
  • 53
ack
  • 1,181
  • 1
  • 17
  • 36
  • 1
    What exactly is the question? How to get local variables that cover the zoom window? Or how to implement the sine for complex arguments? Or something else? – Lutz Lehmann Dec 10 '16 at 17:59

1 Answers1

1

By trigonometric theorems

sin(A+i*B)=sin(A)*cos(i*B)+  cos(A)*sin(i*B) 
          =sin(A)*cosh(B )+i*cos(A)*sinh(B )

and for the quotient using z=x+i*y and c=a+i*b

(x+i*y)/(a+i*b)=(x+i*y)*(a-i*b)/(a*a+b*b)

so that for the sine expression above

A = (a*x+b*y)/(a*a+b*b)
B = (a*y-b*x)/(a*a+b*b)

In javascript a small script to generate this fractal can look like this:

function cosh(x) { return 0.5*(Math.exp(x)+Math.exp(-x)); }
function sinh(x) { return 0.5*(Math.exp(x)-Math.exp(-x)); }

function rgb(r,g,b) { return "rgb("+r+","+g+","+b+")"; }
var colors = new Array(24);
for(var k=0; k<8; k++) {
    colors[   k] = rgb(k*64,(7-k)*64,(7-k)*64);
    colors[ 8+k] = rgb((7-k)*64,k*64,(7-k)*64);
    colors[16+k] = rgb((7-k)*64,(7-k)*64,k*64);
}

var cvs = document.getElementById('sine-fractal');
var ctx = cvs.getContext('2d');
var cx = 0.0, cy = 0.0;
var dx = 1.0;

var tiles = 100;
var scale = Math.min(cvs.width, cvs.height) / tiles;
ctx.scale(scale, scale);

function localx(i) { return cx-dx + 2*i*dx/tiles; }
function localy(j) { return cy-dx + 2*j*dx/tiles; }

for (var i = 0; i < tiles; i++) {
  var a = localx(i);
  for (var j = 0; j < tiles; j++) {
    var b = localy(j);
    var r2 = a*a + b*b;
    var x = a, y = b;
    var rounds = 0;
    var max = 500;
    while (x * x + y * y < 4 && rounds < max) {
      var u = (a*x + b*y) / r2, v = (a*y - b*x) / r2;
      x = Math.sin(u) * cosh(v);
      y = Math.cos(u) * sinh(v);
      rounds++;
    }
    ctx.fillStyle = colors[rounds % 24];
    ctx.fillRect(i, j, 1, 1);
  }
}
<canvas id='sine-fractal' width=200 height=200></canvas>
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51