- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathComplex.java
More file actions
Latest commit
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
importjava.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
classComplex
{
floatre;
floatim;
floatfreq;
floatamp;
floatphase;
Complex(floatr, floati)
{
re = r;
im = i;
}
Complex(floatr, floati, floatf, floata, floatp)
{
re = r;
im = i;
freq = f;
amp = a;
phase = p;
}
Complexmult(Complexother)
{
floatrea = re * other.re - im * other.im;
floatima = re * other.im + im * other.re;
returnnewComplex(rea, ima);
}
Complexadd(Complexother)
{
returnnewComplex(re + other.re, im + other.im);
}
Complexsub(Complexother)
{
returnnewComplex(re - other.re, im - other.im);
}
Complexpow(intn)
{
Complexresult = this;
for (inti = 0; i < n; i++)
{
result = result.mult(this);
}
returnresult;
}
Complexmult(floatmult)
{
re *= Math.sqrt(mult);
im *= Math.sqrt(mult);
returnnewComplex(re, im);
}
Complexdiv(floatdivisor)
{
floatr = re / divisor;
floati = im / divisor;
returnnewComplex(r, i);
}
voidnormalize()
{
re /= mag();
im /= mag();
}
floatheading()
{
return (float) Math.atan2(im, re);
}
floatmag()
{
return (float) Math.sqrt(re * re + im * im);
}
voidrotate(floattheta)
{
re += Math.cos(theta);
im += Math.sin(theta);
}
staticvoidSortComplex(ArrayList<Complex> c){
intn = c.size();
for (inti = 0; i < n-1; i++)
{
intmindex = i;
for (intj = i+1; j < n; j++)
{
if (c.get(j).amp > c.get(mindex).amp)
mindex = j;
}
Complextemp = c.get(mindex);
c.set(mindex, c.get(i));
c.set(i, temp);
}
}
}