gpt4 book ai didi

java - Mandelbrot刷新速度很慢,有什么办法可以让它更快吗?

转载 作者:太空宇宙 更新时间:2023-11-04 07:44:21 26 4
gpt4 key购买 nike

我最近一直在研究分形生成器,并且一直在专门研究 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.");
}

最佳答案

我想cells是某种List实现?

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

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)));

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

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

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

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

我不知道 Cell 的用途类,但如果您仅将其用作颜色的包装器,则如果将每个像素的计算颜色存储在二维数组中或直接写入 Graphics ,您可能会获得更多性能。或Raster对象而不是处理单元格包装器的平面列表。

关于java - Mandelbrot刷新速度很慢,有什么办法可以让它更快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15554705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com