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