KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > chaos > MandelbrotSet


1 package JSci.maths.chaos;
2
3 import JSci.maths.Complex;
4
5 /**
6 * The MandelbrotSet class provides an object that encapsulates the Mandelbrot set.
7 * @version 1.0
8 * @author Mark Hale
9 */

10 public final class MandelbrotSet extends Object JavaDoc {
11         private final MandelbrotMap mbrot=new MandelbrotMap(Complex.ZERO);
12         /**
13         * Constructs a Mandelbrot set.
14         */

15         public MandelbrotSet() {}
16         /**
17         * Returns 0 if z is a member of this set,
18         * else the number of iterations it took for z to diverge to infinity.
19         */

20         public int isMember(final Complex z,final int maxIter) {
21                 mbrot.setConstant(z);
22                 Complex w=Complex.ZERO;
23                 for(int i=0;i<maxIter;i++) {
24                         w=mbrot.map(w);
25                         if(w.mod()>MandelbrotMap.CONVERGENT_BOUND)
26                                 return i+1;
27                 }
28                 return 0;
29         }
30         public int isMember(final double zRe,final double zIm,final int maxIter) {
31                 double re=0.0,im=0.0;
32                 double tmp;
33                 for(int i=0;i<maxIter;i++) {
34                         tmp=2.0*re*im+zIm;
35                         re=re*re-im*im+zRe;
36                         im=tmp;
37                         if(re*re+im*im>MandelbrotMap.CONVERGENT_BOUND*MandelbrotMap.CONVERGENT_BOUND)
38                                 return i+1;
39                 }
40                 return 0;
41         }
42 }
43
44
Popular Tags