import ChaosDemos.*;
import java.awt.*;
import java.util.*;

//********************************************************************
/**                    
* Diagnostics for Map1D <br>
* Shows iteration of map against map function.<br>
* Allows region of plot to be enlarged, and displays
* sensitivity to intitial consitions.
* @version 10 October 1997
* @author Michael Cross
*/
//********************************************************************

public class Map1DTimeSeries extends Map1DDiagnostics {

/* stored x for plotting */      
      private double xp;
/* second x variable */      
      private double x1=0.;
/* stored x1 for plotting */      
      private double xp1=0;
/* perturbation for second trace */      
      private double dx=0.;
/* string for status box */
      private String text;

//********************************************************************
/**
* @param inParent parent class
* @see Map1Dr
*/ 
//****************************************************************             
      public Map1DTimeSeries(Map1D inParent) {
            super(inParent);
            parameters = new double[parent.nParameters];                                                                                        
      }

//********************************************************************
/**
* Updates parameters from the text controls in parent class 
*/ 
//********************************************************************          
      public void updateParameters() {
            int i;           
            iterations=0;                     
            x0=parent.variables.parseTextField(5,x0);
            x=x0;
            ntrans=parent.variables.parseTextField(3,ntrans);
            if(ntrans>0) runTrans=true;                                                
            curve2=false;

            dx=parent.variables.parseTextField(4,dx);                  
            if(dx!=0.) curve2=true;
            nf=parent.variables.parseTextField(2,nf,true);
            parent.mapFunction.setCompose(nf);
                                                 
      }      
                
//*********************************************************************
/**
* Restarts plot
*/
//*********************************************************************
      
      public void restart() {
            double data[] = new double[2*functionPoints];
            double data1[] = new double[6];
            double data2[] = new double[6];
            int i,j;
            double xSave;
            double tSave;
            double f;
            int ip=0;

//            iterations=0;
            parent.mapFunction.windingAdd=0;
            // Eliminate transient
            if(runTrans) {                
                for(i=0;i<ntrans;i++) 
                    x=parent.mapFunction.iterate(x);                   
                runTrans=false;
                iterations+=ntrans;                       
            }
            xSave=x;                        
            
            // Reset graph
            ncurve=parent.resetGraph();
                        
            data1[0]=xmin;
            data1[1]=xmin;
            data1[2]=xmax;
            data1[3]=xmax;
            ncurve = parent.graph.addCurve(data1,2,Color.black);
  
            // Plot function
            for(i=j=0; i<functionPoints; i++) {
                x =((double) i)/((double)(functionPoints-1));
                f = parent.mapFunction.iterate(x);
                if((x>=xmin) && (x<=xmax) ){
                      data[j++] = x;
                      data[j++] =f;
                      ip++;
                }
                x=f;
            }
            if(ip > 2) {
                  ncurve = parent.graph.addCurve(data,ip,Color.darkGray);
            }
            
            // Plot initial point
            x=xSave;
            if(curve2&&(iterations==ntrans))  x1=x+dx;                                    
            runTrans=false;
            xp=x;
            data1[0]=x;
            data1[1]=x;
            x=parent.mapFunction.iterate(x);
            data1[2]=xp;
            data1[3]=x;
            xp=x;
            iterations++;                                                         
            ncurve = parent.graph.addCurve(data1,2,Color.blue);
            ncurve1=ncurve;
            if(curve2) { 
                  xp1=x1;
                  data2[0]=x1;
                  data2[1]=x1;
                  x1=parent.mapFunction.iterate(x1);
                  data2[2]=xp1;
                  data2[3]=x1;
                  xp1=x1;                                      
                  ncurve = parent.graph.addCurve(data2,2,Color.red);
                  ncurve2=ncurve;
            } 
            parent.graph.clearAll=true;
            parent.graph.paintAll=true;
            parent.graph.repaint();                                             
            parent.mapFunction.windingAdd=1;           
            
      }


//*********************************************************************
/**
* Iterates Map equations and updates graph
*/
//*********************************************************************
      
      public boolean iterate() {
            int i,j;
            double[] moredata = new double[6];
            double[] moredata1 = new double[6];

            moredata[0]=xp;
            moredata[1]=x;
            moredata[2]=x;
            moredata[3]=x;
            moredata[4]=x;
            x=parent.mapFunction.iterate(x);
            moredata[5]=x;                     
            if(parent.showTime) {
                  text="Iterations: " + iterations;
                  if(parent.mapFunction.showWinding && parent.mapFunction.total > 0)
                        text=text+" Winding: "+
                          String.valueOf((float) parent.mapFunction.winding/(float) parent.mapFunction.total);
                  parent.status.setText(text);
            }                      
            iterations++;                     
            xp=x;
            if(curve2) {
                  moredata1[0]=xp1;
                  moredata1[1]=x1;
                  moredata1[2]=x1;
                  moredata1[3]=x1;
                  moredata1[4]=x1;
                  x1=parent.mapFunction.iterate(x1);                                             
                  moredata1[5]=x1;
                  xp1=x1;                     
            } 
            parent.graph.paintAll=false;
            parent.graph.clearAll=false;    
            parent.graph.appendToCurve(moredata,3,ncurve1);
            if(curve2)
                 parent.graph.appendToCurve(moredata1,3,ncurve2);
            parent.graph.paintAll=true;
            parent.graph.repaint();                     
        
                                        
            return true;
      }
      
//**********************************************************************
/**
* Sets default values of parameters
*/
//**********************************************************************          
      public void setPlotDefaults() {

              String[] label={"Transient","Delta-x","Start x",""};
              String[] text={"0","0.","0.2",""};
              parent.setPlotTextBoxes(label,text);                              
               
              parent.choices.enable(0);                                              
           
      }                 

//**********************************************************************               
      public void respondToClick(double xcoord, double ycoord) {
                   x=xcoord;
                   restart();
                   parent.clicked=false;
      }   

}      
