View Javadoc
1   package org.woehlke.computer.kurzweil.mandelbrot.model.fractal;
2   
3   import java.util.Objects;
4   
5   /**
6    * Mandelbrot Set drawn by a Turing Machine.
7    *
8    * (C) 2006 - 2015 Thomas Woehlke.
9    * https://thomas-woehlke.blogspot.com/2016/01/mandelbrot-set-drawn-by-turing-machine.html
10   * @author Thomas Woehlke
11   *
12   * Created by tw on 18.08.15.
13   */
14  public class ComplexNumber {
15  
16      private volatile double real;
17      private volatile double img;
18  
19      public final static int MAX_ITERATIONS = 64;
20      private final static double DIVERGENCE_THRESHOLD = 4.0d;
21  
22      public double getReal() {
23          return real;
24      }
25  
26      public double getImg() {
27          return img;
28      }
29  
30      public ComplexNumber() {
31          this.real = 0.0d;
32          this.img = 0.0d;
33          this.iterations=0;
34          this.inMandelbrotSet=false;
35          this.inJuliaSet=false;
36      }
37  
38      public ComplexNumberf="../../../../../../../org/woehlke/computer/kurzweil/mandelbrot/model/fractal/ComplexNumber.html#ComplexNumber">ComplexNumber(ComplexNumber complexNumber) {
39          this.real = complexNumber.real;
40          this.img = complexNumber.img;
41          this.iterations=complexNumber.iterations;
42          this.inMandelbrotSet=complexNumber.inMandelbrotSet;
43          this.inJuliaSet=complexNumber.inJuliaSet;
44      }
45  
46      public ComplexNumber(double real, double img) {
47          this.real = real;
48          this.img = img;
49          this.iterations=0;
50          this.inMandelbrotSet=false;
51          this.inJuliaSet=false;
52      }
53  
54      public ComplexNumber/../../../../../../org/woehlke/computer/kurzweil/mandelbrot/model/fractal/ComplexNumber.html#ComplexNumber">ComplexNumber plus(ComplexNumber complexNumber){
55          double newRealZ = this.real + complexNumber.real;
56          double newImgZ = this.img + complexNumber.img;
57          return new ComplexNumber(newRealZ,newImgZ);
58      }
59  
60      public ComplexNumber square(){
61          double realZ=real;
62          double imgZ=img;
63          double newRealZ=realZ*realZ-imgZ*imgZ;
64          double newImgZ=2*realZ*imgZ;
65          return new ComplexNumber(newRealZ,newImgZ);
66      }
67  
68      private volatile int iterations;
69      private volatile boolean inMandelbrotSet;
70      private volatile boolean inJuliaSet;
71  
72      public synchronized int computeMandelbrotSet() {
73          int iterationsTmp = 0;
74          ComplexNumberkurzweil/mandelbrot/model/fractal/ComplexNumber.html#ComplexNumber">ComplexNumber z = new ComplexNumber();
75          do {
76              iterationsTmp++;
77              z = z.square().plus(this);
78          } while (z.isNotDivergent() && (iterationsTmp < MAX_ITERATIONS));
79          this.inMandelbrotSet = z.isNotDivergent();
80          this.iterations = this.inMandelbrotSet?0:iterationsTmp;
81          return this.iterations;
82      }
83  
84      public synchronized int computeJuliaSet(ComplexNumber c) {
85          int iterationsTmp = 0;
86          ComplexNumberkurzweil/mandelbrot/model/fractal/ComplexNumber.html#ComplexNumber">ComplexNumber z = new ComplexNumber(this);
87          do {
88              iterationsTmp++;
89              z = z.square().plus(c);
90          } while (z.isNotDivergent() && (iterationsTmp < MAX_ITERATIONS));
91          this.inJuliaSet = z.isNotDivergent();
92          this.iterations = this.inJuliaSet?0:iterationsTmp;
93          return this.iterations;
94      }
95  
96      public synchronized boolean isInMandelbrotSet() {
97          return inMandelbrotSet;
98      }
99  
100     public synchronized boolean isInJuliaSet() {
101         return inJuliaSet;
102     }
103 
104     public synchronized boolean isNotDivergent(){
105         return (( real*real + img*img ) < DIVERGENCE_THRESHOLD);
106     }
107 
108     @Override
109     public boolean equals(Object o) {
110         if (this == o) return true;
111         if (!(o instanceof ComplexNumber)) return false;
112         ComplexNumber/../../../../../org/woehlke/computer/kurzweil/mandelbrot/model/fractal/ComplexNumber.html#ComplexNumber">ComplexNumber that = (ComplexNumber) o;
113         return Double.compare(that.getReal(), getReal()) == 0 &&
114             Double.compare(that.getImg(), getImg()) == 0 &&
115             iterations == that.iterations &&
116             isInMandelbrotSet() == that.isInMandelbrotSet() &&
117             isInJuliaSet() == that.isInJuliaSet();
118     }
119 
120     @Override
121     public int hashCode() {
122         return Objects.hash(getReal(), getImg(), iterations, isInMandelbrotSet(), isInJuliaSet());
123     }
124 
125     @Override
126     public String toString() {
127         return "ComplexNumber{" +
128             "real=" + real +
129             ", img=" + img +
130             ", iterations=" + iterations +
131             ", inMandelbrotSet=" + inMandelbrotSet +
132             ", inJuliaSet=" + inJuliaSet +
133             '}';
134     }
135 }