曼德尔布罗特集的视觉表示

时间:2019-06-23 13:45:49

标签: java recursion mandelbrot

我想使用Java生成Mandelbrot集的PNG照片,输出应该可以在Google图片搜索中轻松找到。

该集合定义为以下顺序:

z_n+1 = z_n^2 + c

其中cz是复数,而z的模数始终小于2。

我首先为复数定义一个类,该类还包含所需的基本复数运算。

public class ComplexNumber {
    private double real;
    private double imaginary;

    public ComplexNumber(double real, double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }

    public ComplexNumber add(ComplexNumber z1, ComplexNumber z2) {
        ComplexNumber sum = new ComplexNumber(0, 0);
        sum.real = z1.real + z2.real;
        sum.imaginary = z1.imaginary + z2.imaginary;
        return sum;
    }

    public ComplexNumber square(ComplexNumber z) {
        ComplexNumber squared = new ComplexNumber(0, 0);
        squared.real = Math.pow(z.real, 2) - Math.pow(z.imaginary, 2);
        squared.imaginary = 2 * z.real * z.imaginary;
        return squared;
    }

    public double abs() {
        double absolute = Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imaginary, 2));
        return absolute;
    }
}

然后我定义了Mandelbrot类,该类获取多个复数c(基于像素),并使用mandelbrot方法检查这些数字是否在mandelbrot集中,并将该方法的输出转换为要显示。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Mandelbrot {

    public static int mandelbrot(ComplexNumber c, ComplexNumber z, int i, int n) {
        if (i < n) {
            if (c.abs() > 2.0) {
                return i;
            } else
                return 0;
        }
        return mandelbrot(c, z.square(z).add(z, c), i, n);
    }

    // Create the Mandelbrot image, fill it and save it as PNG file.
    public static void createMandelbrotImage(int tileSize, int maxRecurse) throws IOException {
        int height = 2 * tileSize;
        int width = 3 * tileSize;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        ComplexNumber z0 = new ComplexNumber(0, 0);
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // Construct a complex number from the pixel coordinates
                float xPos = (x + 0.5f - 2 * tileSize) / tileSize;
                float yPos = (y + 0.5f - tileSize) / tileSize;
                ComplexNumber c = new ComplexNumber(xPos, yPos);

                // Check the Mandelbrot condition for this complex number
                int mb = mandelbrot(c, z0, 0, maxRecurse);

                // Translate the result to number in a reasonable range and use it as color.
                double mbl = mb > 0 ? Math.log(mb) / Math.log(maxRecurse) : 0;
                image.setRGB(x, y, (int) (mbl * 255));
            }
        }

        // Save the image as PNG
        String OS = System.getProperty("os.name").toLowerCase(); // different for win and unix
        String filePath = System.getProperty("user.dir") + (OS.indexOf("win") >= 0 ? "\\" : "/") + "mandelbrot.png";
        System.out.println("Writing mandelbrot image to: " + filePath);
        ImageIO.write(image, "png", new File(filePath));
    }

    public static void main(String[] args) throws IOException {

        createMandelbrotImage(500, 2 ^ 24);

    }
}

问题在于此代码始终输出黑色空白图像,而且我似乎没有找到错误。

1 个答案:

答案 0 :(得分:2)

您的递归mandelbrot函数似乎具有不正确的终止条件。

您希望mandelbrot函数在任一情况下返回

  • z的绝对值超过2或
  • 达到最大递归深度。

此外,您永远不会增加i。

所以更新后的功能看起来像:

public static int mandelbrot(ComplexNumber c, ComplexNumber z, int i, int n) {
    if (i >= n) {
        // mandelbrot function does not diverge after n iterations.
        // Returning -1 as a magic value to indicate that the point c is in the mandelbrot set.
        // Values may already be outside of the mandelbrot set in the 0th iteration, so returning -1 makes more sense.
        return -1;
    } else if (z.abs() >= 2.0) {
        // mandelbrot function is diverging after i iterations.
        return i;
    } else {
        // recursively call mandelbrot function with an updated z and an incremented i.
        return mandelbrot(c, z.squared().add(c), i + 1, n);
    }
}

最后,如果您选择在mandelbrot集中的某个点返回-1,则必须更新颜色计算以将这些点设置为黑色。

int mb = mandelbrot(c, z0, 0, maxRecurse);

if (mb == -1) {
    image.setRGB(x, y, 0);
} else {
    // set the color of your image as usual
}