var PI2 = 2* Math.PI;

/*-------------------------------
COMPLEX NUMBERS
theta in [-PI,PI] pho >= 0
-----------------------------*/
function Complex(rho, theta)
	{
	while(theta > PI) theta -= PI2;
	while(theta < -PI) theta += PI2;
	this.r = rho;
	this.t = theta ;
	}
	
Complex.xy = function(x,y)
	{
	return new Complex (sqrt(x*x+y*y), atan2(y,x));

}
	
// Return the magnitude of a complex number. This is defined
// as its distance from the origin (0,0) of the complex plane.
Complex.prototype.magnitude = function() {
    return this.r ;
};

// Return a complex number that is the negative of this one.
Complex.prototype.negative = function() {
    return new Complex(this.r, this.r + PI);
};

// Return a complex number that is the inverse of this one.
Complex.prototype.inv = function() {
    return new Complex(1/this.r, this.r / 2);
};

// Convert a Complex object to a string in a useful way.
// This is invoked when a Complex object is used as a string.
Complex.prototype.toString = function() {
    return "{ " + this.r + ", " + this.t + " } ";
};

// Return the real portion of a complex number. This function
// is invoked when a Complex object is treated as a primitive value.
Complex.prototype.valueOf = function() { return this.r* cos(this.t); }

// Multiply two complex numbers and return the product.
Complex.multiply = function(a, b) {
    return new Complex(a.r * b.r , a.t + b.t);
};

// Divide two complex numbers and return the product.
Complex.divide = function(a, b) {
    return new Complex(a.r / b.r , a.t - b.t);
};


// a**n
Complex.power = function(a,n) {
    return new Complex(Math.pow(a.r,n) ,a.t *n);
    }
    
// Add two complex numbers
Complex.add = function (a,b)
	{
	var x = a.r*cos(a.t)+ b.r*cos(b.t);
	var y = a.r*sin(a.t)+ b.r*sin(b.t);
	return new Complex (sqrt(x*x+y*y), atan2(y,x));
	}
// Substract two complex numbers
Complex.sub = function (a,b)
	{
	var x = a.r*cos(a.t)- b.r*cos(b.t);
	var y = a.r*sin(a.t)- b.r*sin(b.t);
	return new Complex (sqrt(x*x+y*y), atan2(y,x));
	}
	
Complex.zero = new Complex(0,0);
Complex.one = new Complex(1,0);
Complex.i = new Complex(1,PI/2);

/*--------------------------------
INTERFACE WITH GBgraph and gLine canvas
--------------------------------------*/
Complex.prototype.draw = function()
	{
	moveto(0,0);
	polar(this.r,this.t);
	} ;

function plane(xmax,color)
{
xAxis=yAxis=1;
yMax=xMax=xmax;
wantsAn=0;
if(arguments.length > 1)
	{
	xmax += xmax/10 ;
	rect(-xmax,-xmax,2*xmax,2*xmax);
	fillstyle(color);
	fill();
	}
}

function rcolor() // random color
{
return lColor[Math.floor(random()*512)] ;
}


