void setup() { Complex c1 = new Complex(1,2); Complex c2 = new Complex(3,4); Complex m = Complex.cMult(c1,c2); println("c1 = " + c1 + " c2 = " + c2 + " c1*c2 = " + m); } //********************************************************** static class Complex { float real = 0; float imag= 0; Complex() { } Complex(Complex c) { real = c.real; imag = c.imag; } Complex (float r, float i) { real = r; imag = i; } static Complex conjugate(Complex c) { return new Complex(c.real, - c.imag); } static Complex cMult(Complex c1, Complex c2) { float realPart = c1.real*c2.real-c1.imag*c2.imag; float imagPart = c1.real*c2.imag + c2.real*c1.imag; return new Complex(realPart,imagPart); } /* // Adds two complex numbers, c1 + c2 static Complex cAdd(Complex c1, Complex c2) { // Add your code here } // Subtracts two complex numbers, c1 - c2 static Complex cSub(Complex c1, Complex c2) { // Add your code here } // Divides two complex numbers, c1 / c2 static Complex cDiv(Complex c1, Complex c2) { // Add your code here } */ // convert polar coordinates to Cartesian coordinates. static Complex polar2Cart(float r, float degrees) { float realPart = r*cos(radians(degrees)); float imagPart = r*sin(radians(degrees)); return new Complex(realPart,imagPart); } static float len(Complex c) { return sqrt(c.real*c.real+ c.imag*c.imag); } static float angle(Complex c) { if (c.real==0 && c.imag == 0) return 0; else if (c.real >= 0) return asin( c.imag/len(c)); else return PI-asin(c.imag/len(c)); } float zAbs() { return real*real + imag*imag; } String toString() { return real + " + " + imag + "i"; } }