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