Source code: com/arranger/jarl/test/frac/MandelbrotCalculator.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // MandelbrotCalculator Class //////////////////////////////////////////////////
3 ////////////////////////////////////////////////////////////////////////////////
4 // Copyright 2000 - David Leberknight - Anyone may use this code for any reason
5 // at any time, provided that they give an appropriate reference to this source,
6 // and send David some email ( david@leberknight.com ).
7 //
8 // MandelbrotCalculators compute Mandelbrot Set images.
9
10 package com.arranger.jarl.test.frac;
11
12 import com.arranger.jarl.test.FracTest;
13
14 public class MandelbrotCalculator extends FractalCalculator {
15 public MandelbrotCalculator(FracTest fractal, Drawing newDrawing) {
16 super(fractal, newDrawing);
17 }
18
19 protected int testPoint(double cR, double cI, int maxIterations) {
20 // Is the given complex point, (cR, cI), in the Mandelbrot set?
21 // Use the formula: z <= z*z + c, where z is initially equal to c.
22 // If |z| >= 2, then the point is not in the set.
23 // Return 0 if the point is in the set; else return the number of
24 // iterations it took to decide that the point is not in the set.
25 double zR = cR;
26 double zI = cI;
27
28 for (int i = 1; i <= maxIterations; i++) {
29 // To square a complex number: (a+bi)(a+bi) = a*a - b*b + 2abi
30 double zROld = zR;
31 zR = zR * zR - zI * zI + cR;
32 zI = 2 * zROld * zI + cI;
33
34 // We know that if the distance from z to the origin is >= 2
35 // then the point is out of the set. To avoid a square root,
36 // we'll instead check if the distance squared >= 4.
37 double distSquared = zR * zR + zI * zI;
38 if (distSquared >= 4) {
39 return i;
40 }
41 }
42 return 0;
43 }
44 }