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