在 Java 中以图形方式表示 Mandelbrot 和 Julia 集

Representing Mandelbrot and Julia Sets Graphically in Java

我正在解决一个问题,我需要使用 OpenCL 以图形方式表示 Mandelbrot 集并首先处理我的顺序代码。但是,它生成的图像不是很好,我不确定我是否遗漏了某些地方,或者这是否只是缺乏分辨率的问题(可以这么说)。我已经发布了下面的代码及其生成的屏幕截图 - 这是我应该期待的还是我在某处搞砸了?

public class SequentialMandelbrot {

    private static int[] colorMap;
    private static int xSize = 200, ySize = 200;
    private static float yMin = -2f, yMax = 2f;
    private static float xMin = -2f, xMax = 2f;
    private static float xStep =  (xMax - xMin) / (float)xSize;
    private static float yStep =  (yMax - yMin) / (float)ySize;
    private static final int maxIter = 250;
    private static BufferedImage image;
    private static JComponent imageComponent;   

    public static void main(String[] args) {

        // Create the image and the component that will paint the image
        initColorMap(32, Color.RED, Color.GREEN, Color.BLUE);
        image = new BufferedImage(xSize, ySize, BufferedImage.TYPE_INT_RGB);
        imageComponent = new JPanel()
        {
            private static final long serialVersionUID = 1L;
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.drawImage(image, 0,0,this);
            }   
        };

        for (int j = 0; j < xSize; j++) {
            for (int k = 0; k < ySize; k++) {
                int iter = mandelbrot(j, k);
                if (iter == maxIter) {
                    image.setRGB(j, k, 0);                  
                } else {
                    int local_rgb = colorMap[iter%64];
                    image.setRGB(j, k, local_rgb);
                }
            }
        }

        JFrame frame = new JFrame("JOCL Simple Mandelbrot");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        imageComponent.setPreferredSize(new Dimension(xSize, ySize));
        frame.add(imageComponent, BorderLayout.CENTER);
        frame.pack();

        frame.setVisible(true);
    }

    private static int mandelbrot(float j, float k) {
        int t = 0;
        float norm = 0;
        float x = 0;
        float y = 0;
        float r = xMin + (j * xStep);
        float i = yMin + (k * yStep);
        while (t < maxIter && norm < 4) {
            x = (x*x) - (y*y) + r;
            y = (2*x*y) + i;
            norm = (x*x) + (y*y);
            t++;
        }
        return t;
    }

我还更改了 Julia 集的代码(从数字 0.45 + 0.1428i 开始),它产生了同样有问题的东西:

这是你的迭代循环,这是不正确的。

while (t < maxIter && norm < 4) {
    x = (x*x) - (y*y) + r;
    y = (2*x*y) + i;
    norm = (x*x) + (y*y);
    t++;
}

您正在覆盖 x,然后重新使用它来计算 y。我建议使用临时变量,例如

while (t < maxIter && norm < 4) {
    tempx = (x*x) - (y*y) + r;
    y = (2*x*y) + i;
    x = tempx;
    norm = (x*x) + (y*y);
    t++;
}

另外:还有提高效率的空间,因为您正在计算 x*xy*y 两次。