-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplex.java
53 lines (44 loc) · 990 Bytes
/
Complex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class Complex {
public double re;
public double im;
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
// Returns fraction of maxInterations needed to determine divergance
public double converge(int maxIterations) {
Complex startNum = new Complex(re, im);
Complex num = new Complex(re, im);
int i;
for (i = 0; i < maxIterations; i++) {
if (num.getAbsSq() > 4) {
break;
}
num.square();
num.add(startNum);
}
return (double) i / maxIterations;
}
public double getAbs() {
return Math.sqrt(re * re + im * im);
}
public double getAbsSq() {
return re * re + im * im;
}
public void square() {
double oldRe = re;
re = re * re - im * im;
im = 2 * oldRe * im;
}
public void add(Complex c) {
re += c.re;
im += c.im;
}
public String toString() {
String str = "%f + %fi";
if (im < 0) {
str = "%f %fi";
}
return String.format(str, re, im);
}
}