Mandlebrot任务类

时间:2018-11-22 22:53:12

标签: java complex-numbers

我正在尝试实现mandelbrot,但是在我的主类中,它绘制了一种颜色,因为所有迭代计数都保持为零。我一直在寻找导致这种情况的原因,但无法确定是什么原因导致了这种情况。我寻找其他来源,但是他们的方法完全不同。所有绘图都在主类中完成,幅度加法和乘法方法在第三类中创建。

package edu.ycp.cs201.mandelbrot;

public class MandelbrotTask implements Runnable {
private double x1, y1, x2, y2;
private int startCol, endCol, startRow, endRow;
private int[][] iterCounts;

public MandelbrotTask(double x1, double y1, double x2, double y2,
                      int startCol, int endCol, int startRow, int endRow,
                      int[][] iterCounts) {
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    this.startCol = startCol;
    this.endCol = endCol;
    this.startRow = startRow;
    this.endRow = endRow;
    this.iterCounts = iterCounts;
}

public void run() {
    for (int i = startRow; i < endRow; i++) {
        for (int j = startCol; j < endCol; j++) {
            Complex c = getComplex(i, j);
            int iterCount = computeIterCount(c);
            iterCounts[i][j] = iterCount;
        }
    }
}

// TODO: implement getComplex and computeIterCount methods
public Complex getComplex(int x, int y) {
    //return new Complex (x, y);
    double col = (double) endCol - (double) startCol;
    double row = (double) endRow - (double) startRow;
    double dx = x2 - x1;
    double dy = y2 - y1;
    return new Complex(x1 + dx / col * x, y1 + dy / row * y);
}

public int computeIterCount(Complex complex) {
    //Zo is initially 0+0i
    Complex z = new Complex(0,0);
    //# of iterations
    int count = 0;
    //while z has magnitude of less than 2 and iteration counts have not 
    exceeded MAX
    for (int k = 0; k < 100; k++) {
        //iterate complex number
        z = z.multiply(z).add(complex);
        //if magnitude of complex number is >2 stop
        if (z.getMagnitude() > 2) {
            break;
        }
        //increment count
        count++;
    }
    return count;
}
}

0 个答案:

没有答案