Я пытаюсь реализовать 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;
}
}