-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComplex.java
More file actions
107 lines (91 loc) · 2.09 KB
/
Copy pathComplex.java
File metadata and controls
107 lines (91 loc) · 2.09 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.ArrayList;
//This is the class for complex numbers, it has many function that aren't used in this program but they this class could be copied and used in another program
class Complex
{
float re;
float im;
float freq;
float amp;
float phase;
Complex(float r, float i)
{
re = r;
im = i;
}
Complex(float r, float i, float f, float a, float p)
{
re = r;
im = i;
freq = f;
amp = a;
phase = p;
}
Complex mult(Complex other)
{
float rea = re * other.re - im * other.im;
float ima = re * other.im + im * other.re;
return new Complex(rea, ima);
}
Complex add(Complex other)
{
return new Complex(re + other.re, im + other.im);
}
Complex sub(Complex other)
{
return new Complex(re - other.re, im - other.im);
}
Complex pow(int n)
{
Complex result = this;
for (int i = 0; i < n; i++)
{
result = result.mult(this);
}
return result;
}
Complex mult(float mult)
{
re *= Math.sqrt(mult);
im *= Math.sqrt(mult);
return new Complex(re, im);
}
Complex div(float divisor)
{
float r = re / divisor;
float i = im / divisor;
return new Complex(r, i);
}
void normalize()
{
re /= mag();
im /= mag();
}
float heading()
{
return (float) Math.atan2(im, re);
}
float mag()
{
return (float) Math.sqrt(re * re + im * im);
}
void rotate(float theta)
{
re += Math.cos(theta);
im += Math.sin(theta);
}
static void SortComplex(ArrayList<Complex> c){
int n = c.size();
for (int i = 0; i < n-1; i++)
{
int mindex = i;
for (int j = i+1; j < n; j++)
{
if (c.get(j).amp > c.get(mindex).amp)
mindex = j;
}
Complex temp = c.get(mindex);
c.set(mindex, c.get(i));
c.set(i, temp);
}
}
}