【问题标题】:Representing Mandelbrot and Julia Sets Graphically in Java在 Java 中以图形方式表示 Mandelbrot 和 Julia 集
【发布时间】:2016-05-24 15:28:04
【问题描述】:

我正在解决一个问题,我需要使用 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),它产生了同样有问题的东西:

【问题讨论】:

    标签: java mandelbrot


    【解决方案1】:

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

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

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

    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

    【讨论】:

    • 哦,谢谢你的第二双眼睛。天啊,真不敢相信我错过了那个白痴。另外,是的,我知道有提高效率的空间,因为我打算清理它。目标是使其并发但想要工作的顺序代码:)
    • 想检查一下。这太棒了:)
    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多