﻿/* Programming by Greg Thatcher, http://www.GregThatcher.com */

    
    function RandomSignToggle ()
    {
        if (Math.random() > 0.5)
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }
    
    function JuliaSet (range)
    {
        var toggle1 = 
        this.C = new complexNumber(RandomSignToggle () * Math.random(), RandomSignToggle () * Math.random());
        // this.C = new complexNumber(0, 0);
        // this.C = new complexNumber(.01, -.3);
        this.Z = new complexNumber(0, 0);
        this.Bound = Math.max(2, this.C.GetRadius());
        this.currentIteration = 0;
        this.itsMaxIteration = 100;
    }

    JuliaSet.prototype.SetZ = function(real, complex)
    {
        this.Z = new complexNumber(real, complex);
        this.currentIteration = 0;
    }

    JuliaSet.prototype.GetRadius = function()
    {
        return this.Z.GetRadius();
    }
    
    JuliaSet.prototype.Escape = function()
    {
        return (this.Z.GetRadius() > this.Bound);
    }
    JuliaSet.prototype.Prisoner = function()
    {
        return (!this.Escape() && (this.currentIteration > this.itsMaxIteration));
    }
        
    JuliaSet.prototype.NextIteration = function()
    {
        this.currentIteration++;
        var newZ = this.Z.Multiply(this.Z);
        newZ = newZ.Add(this.C);
        this.Z = newZ;
                
        
        if (
            (this.Escape()) || 
            (this.currentIteration > this.itsMaxIteration))
        {
            var radius = this.Z.GetRadius();
            return true;
        }
        return false;
    }

    JuliaSet.prototype.ToString = function()
    {
        // return "z = " + this.Z.ToString() + "&sup2; + " + this.C.ToString();
        return "z<sub>n+1</sub> = z<sub>n</sub>&sup2; + " + this.C.ToString();
    }
