package steam;
import java.awt.Graphics;
import receipts.*;
import java.util.Random;

public class electroMagneticField extends body
  {public boolean doPaint=true;
   String theStatisticsNames[]={"sameOmega","guassian","rectangle"};
   String theProcessesNames[]={"oneWave","ten waves"};
   steamMultNameButton theStatistics;
   steamMultNameButton theProcesses;
   steamDoubleButton diffButton;
   public int statistics=0,processes=0;
   public double center[]=new double[3];

   public boolean speduo=false;
   electroMagneticWave theWaves[]=new electroMagneticWave[1000];
   public spaceVector place;
   public double time=0.0,minOmega,diff;
   public int itsSize=1;
  public void setDiffButton(steamDoubleButton aBut)
  {
   diffButton=aBut;
  }
  public steamDoubleButton createDiffButton(int x,int y,int dx,int dy)
  {
     diffButton=new steamDoubleButton("diff",x,y,dx,dy,null);
    diffButton.max=0.1;diffButton.min=0.01;diffButton.newPrimValue=0.01;
    return diffButton;
  }
  public void setProcessesButton(steamMultNameButton aBut)
  {
   theProcesses=aBut;
  }
  public steamMultNameButton createTypeProcessesButton(int x,int y,int dx,int dy)
  {
    return (theProcesses=new steamMultNameButton(theProcessesNames,x,y,dx,dy));
  }
  public void setStatisticsButton(steamMultNameButton aBut)
  {
   theStatistics=aBut;
  }
  public steamMultNameButton createTypeButton(int x,int y,int dx,int dy)
  {
    return (theStatistics=new steamMultNameButton(theStatisticsNames,x,y,dx,dy));
  }
   public Random init(Random theRandom)
   { Random aRandom;
     double xs=0.0,ys=0.0,zs=0.0;
     int i,j,k;
     if(theRandom==null)
     aRandom=new Random();
     else aRandom=theRandom;
     if(diffButton!=null) diff=diffButton.newPrimValue;
     place=new spaceVector((aRandom.nextDouble()-0.5)*10,(aRandom.nextDouble()-0.5)*10,(aRandom.nextDouble()-0.5)*10);
     center[0]=place.x;center[1]=place.y;center[2]=place.z;
     if(theProcesses.pos==0) itsSize=1;
     else itsSize=10;
     minOmega=10000.0;
     for (i=0;i<itsSize;i++)
     {
      (theWaves[i]=new electroMagneticWave()).statistics=theStatistics.pos;
       theWaves[i].diff=diff;theWaves[i].init(aRandom);
      if(theWaves[i].omega<minOmega) minOmega=theWaves[i].omega;
     }
     return aRandom;
   }
   public synchronized body paint(Graphics g)
   {
    spaceVector dummyPi=new spaceVector(0.0,0.0,0.0),lastDummy=new spaceVector(0.0,0.0,0.0);
    int i;
    double loopMax=10;
   if(!doPaint) return this;
    bgnpoint(g);
    if(minOmega>0.1) loopMax=Math.PI*2/minOmega; else loopMax=100;
   for(double t=0;t<loopMax;t+=0.1)
   {spaceVector dummy=new spaceVector(0.0,0.0,0.0);
     for(i=0;i<itsSize;i++)
     dummy=dummy.add(theWaves[i].electricValueAtplaceAtTime(place,t+time));
     v3f(place.add(dummy));
     if(t<Math.PI*0.5) dummyPi=dummy; lastDummy=dummy;
   }
     endpoint(g);
   bgnline(g);
    v3f(place);v3f(place.add(lastDummy.cross(dummyPi).unit()));
   endline(g);
   bgnline(g);
   spaceVector etot=new spaceVector(0.0,0.0,0.0),mtot=new spaceVector(0.0,0.0,0.0);
   v3f(place);
     for(i=0;i<itsSize;i++)
     {
      etot=etot.add(theWaves[i].electricValueAtplaceAtTime(place,time));
      mtot=mtot.add(theWaves[i].magneticValueAtplaceAtTime(place,time));
     }
     v3f(place.add(etot.cross(mtot).unit().mult(2.0)));
    endline(g);
   return this;
   }
  }