add angle func
parent
b40ad7c83f
commit
ccf733ac95
|
@ -26,6 +26,11 @@ pub fn (c Complex) str() string {
|
|||
return out
|
||||
}
|
||||
|
||||
// Complex Angle
|
||||
pub fn (c Complex) angle() f64 {
|
||||
return atan2(c.im, c.re)
|
||||
}
|
||||
|
||||
// Complex Addition c1 + c2
|
||||
pub fn (c1 Complex) + (c2 Complex) Complex {
|
||||
return Complex{c1.re+c2.re,c1.im+c2.im}
|
||||
|
|
|
@ -102,3 +102,18 @@ fn test_complex_equals() {
|
|||
c2 = math.complex(-3,19)
|
||||
assert c1.equals(c2)
|
||||
}
|
||||
|
||||
fn test_complex_angle(){
|
||||
mut c := math.complex(1, 0)
|
||||
assert c.angle() * 180 / math.Pi == 0
|
||||
c = math.complex(1, 1)
|
||||
assert c.angle() * 180 / math.Pi == 45
|
||||
c = math.complex(0, 1)
|
||||
assert c.angle() * 180 / math.Pi == 90
|
||||
c = math.complex(-1, 1)
|
||||
assert c.angle() * 180 / math.Pi == 135
|
||||
c = math.complex(-1, -1)
|
||||
assert c.angle() * 180 / math.Pi == -135
|
||||
mut cc := c.conjugate()
|
||||
assert cc.angle() + c.angle() == 0
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue