【问题标题】:Mandelbrot very slow to refresh, any way to make it faster?Mandelbrot 刷新很慢,有什么办法让它更快?
【发布时间】:2013-03-21 17:53:25
【问题描述】:

我最近一直在研究分形生成器,并且一直在专门研究 Mandelbrot 集。不幸的是,缩放和移动似乎非常低效,需要很长时间才能刷新。每次缩放时我都会生成它,我知道这可能不是最有效的方法,而且我似乎找不到使用我理解的另一种方法的代码。 这些是我使用的以下方法,第一个是初始生成,第二个是刷新方法。

    private void genMandelbrot(Dimension size) {
    for(int x=0;x<size.width;x++) {
        for(int y=0;y<size.height;y++) {
            double moveX=globalx;
            double moveY=globalx;
            //zoom and x/y offset.
            double real = 1.5 * (x - size.width / 2) / (0.5 * zoom * size.width) + moveX;
            double imaginary=(y - size.height / 2) / (0.5 * zoom * size.height) + moveY;
            double newRe=0,newIm=0,oldRe=0,oldIm=0;

            int i;
            for(i=0;i<8000;i++) {
                oldRe = newRe;
                oldIm = newIm;
                newRe = oldRe * oldRe - oldIm * oldIm + real;
                newIm = 2 * oldRe * oldIm + imaginary;
                if((newRe * newRe + newIm * newIm) > 4) break;
            }

            Cell c = new Cell(Color.getHSBColor(i % 256, i % 255, 255 * ((i<20)? 1:0)), new Dimension(1,1), new Point(x,y));
            cells.add(c);
        }
    }
}
public void refreshMandelbrot(Dimension size) {
    for(Cell c : cells) {
            double moveX=globalx;
            double moveY=globalx;
            int x=c.x;
            int y=c.y;
            //zoom and x/y offset.
            double real = 1.5 * (x - size.width / 2) / (0.5 * zoom * size.width) + moveX;
            double imaginary=(y - size.height / 2) / (0.5 * zoom * size.height) + moveY;
            double newRe=0,newIm=0,oldRe=0,oldIm=0;

            int i;
            for(i=0;i<8000;i++) {
                oldRe = newRe;
                oldIm = newIm;
                newRe = oldRe * oldRe - oldIm * oldIm + real;
                newIm = 2 * oldRe * oldIm + imaginary;
                if((newRe * newRe + newIm * newIm) > 4) break;
            }

            cells.set(cells.indexOf(c), new Cell(Color.getHSBColor(i % 256, i % 255, 255 * ((i<20)? 1:0)), new Dimension(1,1), new Point(x,y)));
    }
    System.out.println("Set refreshed.");
}

【问题讨论】:

  • 配置文件,然后再尝试优化。哪里花的时间最多?

标签: java math complex-numbers fractals mandelbrot


【解决方案1】:

我想cells 是某种List 实现?

在这种情况下,刷新方法的大部分时间都花在了这一行:

cells.set(cells.indexOf(c), new Cell(Color.getHSBColor(i % 256, i % 255, 255 * ((i&lt;20)? 1:0)), new Dimension(1,1), new Point(x,y)));

更准确地说是在cells.indexOf(c) 中,整个列表被迭代以找到c 的正确索引。

由于您只是更改每个单元格的颜色,因此最简单的解决方法是更改​​您当前正在使用的单元格的颜色。我不知道你的Cell 类的实际实现,但如果它有一个方法setColor(...),你可以用

替换上面的行

c.setColor(Color.getHSBColor(i % 256, i % 255, 255 * ((i&lt;20)? 1:0)));

这将refreshMandelbrot 方法的运行时间减少到与genMandelbrot 方法相同。

我不知道 Cell 类的用途,但如果您只是将它用作颜色的包装器,如果您将每个像素的计算颜色存储在两个 -维数组或直接写入 GraphicsRaster 对象,而不是处理单元格包装器的平面列表。

【讨论】:

    【解决方案2】:

    您很可能需要细分分形并计算不那么有趣的图块。 8000次重复很多。您还可以稍微简化计算。

    【讨论】:

      猜你喜欢
      • 2013-11-28
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 2012-03-21
      • 2012-12-08
      • 2023-02-16
      • 1970-01-01
      • 2016-08-05
      相关资源
      最近更新 更多