import java.applet.Applet; import java.awt.event.*; import java.awt.*; /*This class does the basic arithmetic of complex numbers */ public class Complex { double x,y; public Complex() { this.x=0.0; this.y=0.0; } public Complex(double x,double y) { this.x=x; this.y=y; } /* ------------------------------------------- * * STATIC ARITHMETIC */ /** Returns the norm of z. * Call like this: *
Complex.norm(z)
*/ public static double norm(Complex z) { return Math.sqrt(z.x*z.x+z.y*z.y); } /** Return the unit complex number with the same argument. *
Call like this: *
Complex.unit(z)
*/ public static Complex unit(Complex z) { double d=z.norm(z); return new Complex(z.x/d,z.y/d); } /** Static addition... Call like this: *
Complex.plus(a,b)
*/ public static Complex plus(Complex z1,Complex z2) { return new Complex(z1.x+z2.x, z1.y+z2.y); } /** Static subtraction... Call like this: *
Complex.minus(a,b)
*/ public static Complex minus(Complex z1,Complex z2) { return new Complex(z1.x-z2.x, z1.y-z2.y); } /** Static Multiplication. *
Call like this: *
Complex.times(a,b)
*/ public static Complex times(Complex z1,Complex z2) { return new Complex(z1.x*z2.x-z1.y*z2.y, z1.x*z2.y+z1.y*z2.x); } /** Static inverse. *
Call like this: *
Complex.inverse(a)
*/ public static Complex inverse(Complex z) { double d=z.x*z.x+z.y*z.y; return new Complex(z.x/d,-z.y/d); } /** Static division. Call like this: *
Complex.divide(a,b)
*/ public static Complex divide(Complex z1,Complex z2) { return times(z1,inverse(z2)); } /** Static conjugation. * Call like this: *
Complex.conjugate(a)
*/ public static Complex conjugate(Complex z) { return new Complex(z.x,-z.y); } /** Static Euclidean dot product. * Call Like this: *
Complex.dot(v,w)
*/ public static double dot(Complex a, Complex b) { return a.x*b.x+a.y*b.y; } /* ------------------------------------------------- * * REGULAR ARITHMETIC */ /** Returns the norm of z. * Call like this: *
z.norm()
*/ public double norm() { return Math.sqrt(x*x+y*y); } /** Compute the unit complex number with the same argument as this. * Call like this: *
z.unit()
*/ public Complex unit() { double d=norm(); return new Complex(x/d,y/d); } /** Add this complex number to z and return the result. *
Call like this: *
a.plus(b)
*/ public Complex plus(Complex z) { return new Complex(x+z.x, y+z.y); } /** Subtract z from this complex number and return the result. *
This is unchanged. *
Call like this: *
a.minus(b)
*/ public Complex minus(Complex z) { return new Complex(x-z.x, y-z.y); } /** Multiplication. *
Call like this: *
a.times(b)
*/ public Complex times(Complex z) { return new Complex(x*z.x-y*z.y, x*z.y+y*z.x); } /** Inverse. *
Call like this: *
a.inverse()
*/ public Complex inverse() { double d=x*x+y*y; return new Complex(x/d,-y/d); } /** Division. Call like this to get a/b: *
a.divide(b)
*/ public Complex divide(Complex z2) { return times(inverse(z2)); } /** Conjugation. * Call like this: *
a.conjugate()
*/ public Complex conjugate() { return new Complex(x,-y); } /** Static Euclidean dot product. * Call Like this: *
v.dot(w)
*/ public double dot(Complex a) { return a.x*x+a.y*y; } /* ---------------------------------------------------- * * OTHER FUNCTIONS */ /** Compute the area of the given triangle. * Call like this: *
Complex.area(a,b,c)
*/ public static double area(Complex z1,Complex z2,Complex z3) { double a; Complex[] z=new Complex[5]; for(int i=1;i<=9;++i) z[i]=new Complex(); z[1]=z1.minus(z2,z1); z[2]=z1.minus(z3,z1); z[3]=z1.conjugate(z[2]); z[4]=z1.times(z[1],z[3]); a=-z[4].y; return(a); } public Complex scale(Complex z,double r) { Complex w=new Complex(); w.x=r*x+(1.0-r)*z.x; w.y=r*y+(1.0-r)*z.y; return(w); } }