While implementing the mandelbrot set using java, I found that the rendering time of the program is ridiculously slower than other programs.
public class FractalFormula {
private Complex[] referenceOrbit = new Complex[0];
private int skippedIterations;
private Complex calcBn;
private void approximationData(double dcMax) {
List<Complex> calcBnList = new ArrayList<>();
double limit = Math.sqrt(0.5 / dcMax);
int skipCount = 0;
Complex cbn = Complex.ZERO;
for (int i = 0; i<referenceOrbit.length; i++) {
Complex complex = referenceOrbit[i];
Complex doubledComplex = complex.multiply(2);
Complex pbn = cbn;
cbn = cbn.multiply(doubledComplex).add(Complex.ONE);
double rb = cbn.radius2();
if (rb >= limit * limit) {
calcBnList.add(pbn);
skippedIterations = skipCount;
calcBn = pbn;
return;
}else{
skipCount++;
}
}
}
// Runs only once before "iterate()"
public void calculateReferenceOrbit(BigComplex c, MathContext floorMode, int maxIteration, double dcMax) {
List<Complex> referenceOrbit = new ArrayList<>();
int iteration = 0;
BigComplex z = BigComplex.ZERO;
referenceOrbit.add(z.normalValue());
while (z.normalValue().radius2() < 4 && iteration < maxIteration) {
z = z.multiply(z, floorMode).add(c, floorMode);
Complex nz = z.normalValue();
iteration++;
referenceOrbit.add(nz);
}
this.referenceOrbit = referenceOrbit.toArray(Complex[]::new);
approximationData(dcMax);
}
// it returns the double value of iteration
// Performs the corresponding action on all pixels
public double iterate(Complex[] referenceOrbit, double maxIteration, Complex dc) {
int iteration = skippedIterations;
int refIteration = skippedIterations;
int maxRefIteration = referenceOrbit.length - 1;
Complex dz = calcBn.multiply(dc);
Complex z = referenceOrbit[iteration];
while (iteration < maxIteration) {
dz = referenceOrbit[refIteration].multiply(2).add(dz).multiply(dz).add(dc);
refIteration++;
z = referenceOrbit[refIteration].add(dz);
if (z.radius2() > 4) {
break;
}
iteration++;
if (refIteration == maxRefIteration || z.radius2() < dz.radius2()) {
refIteration = 0;
dz = z;
}
}
if (iteration == maxIteration) {
return maxIteration;
}
return iteration + 1 - (z.radius() - 2) / 4;
}
public Complex[] getReferenceOrbit() {
return referenceOrbit;
}
}
I checked other documents and organized them in my notes to derive the best expressions and codes for approximation, but they are still slow.
Even with 100,000 iterations, i want a way to render in a short period of time if the shape is simple.
I’m very curious if it’s an optimization problem for code or something else. here is my approximation code.
(I found the problem that there was a glitch when the coordinates were very close to the pattern, but i decided not to consider this)
7