CMU462 Library  1.0
15-462/15-662: Computer Graphics (Fall 2015)
complex.h
1 #ifndef CMU462_COMPLEX_H
2 #define CMU462_COMPLEX_H
3 
4 #include "CMU462.h"
5 #include "vector2D.h"
6 
7 namespace CMU462 {
8 
13 class Complex : public Vector2D {
14  public:
15 
20  Complex( ) : Vector2D( 0.0, 0.0 ) { }
21 
26  Complex( double a, double b ) : Vector2D( a, b ) { }
27 
32  Complex( const Vector2D& v ) : Vector2D( v ) { }
33 
37  inline Complex conj( ) const {
38  return Complex( x, -y );
39  }
40 
44  inline Complex inv( ) const {
45  double r = 1.0/norm2();
46  return Complex( r*x, -r*y );
47  }
48 
52  inline double arg( ) const {
53  return atan2( y, x );
54  }
55 
59  inline Complex exponential( ) const {
60  return exp( x ) * Complex( cos( y ), sin( y ));
61  }
62 
63  // Complex multiply by z
64  inline void operator*=( const Complex& z ) {
65  double a = x;
66  double b = y;
67  double c = z.x;
68  double d = z.y;
69  x = a*c-b*d;
70  y = a*d+b*c;
71  }
72 
73  // complex divide by z
74  void operator/=( const Complex& z ) {
75  *this *= z.inv();
76  }
77 
78 }; // class Complex
79 
80 // returns the real part of z
81 double Re( const Complex& z );
82 
83 // returns the imaginary part of z
84 double Im( const Complex& z );
85 
86 // binary Complex multiplication
87 inline Complex operator*( const Complex& z1, const Complex& z2 ) {
88  Complex z = z1;
89  z *= z2;
90  return z;
91 }
92 
93 // complex division
94 inline Complex operator/( const Complex& z1, const Complex& z2 ) {
95  Complex z = z1;
96  z /= z2;
97  return z;
98 }
99 
100 // prints components
101 std::ostream& operator<<( std::ostream& os, const Complex& z );
102 
103 } // namespace CMU462
104 
105 #endif // CMU462_COMPLEX_H
Complex(const Vector2D &v)
Constructor.
Definition: complex.h:32
double norm2(void) const
Returns norm squared.
Definition: vector2D.h:105
double arg() const
Return argument.
Definition: complex.h:52
Represents complex numbers as 2D vectors of their real and imaginary components.
Definition: complex.h:13
Definition: CMU462.h:8
Defines 2D vectors.
Definition: vector2D.h:14
Complex()
Constructor.
Definition: complex.h:20
Complex(double a, double b)
Constructor.
Definition: complex.h:26
Complex conj() const
Compute the complex conjugate.
Definition: complex.h:37
Complex inv() const
Compute the inverse.
Definition: complex.h:44
Complex exponential() const
Complex exponentiation.
Definition: complex.h:59