////////////////////////////////////////////////////////////////
// wave.C  ROOT macro file
//
// The macro in this file performs an animation of a wave
// function. It also shows one mark fixed in space and
// another one fixed in time (thus propagating to the right)
// in the same frame.
//
// Kurt Rinnert Oct-16-1998
////////////////////////////////////////////////////////////////
//#include <iostream.h>
{
  gROOT->Reset();

  //=======================================================================

  // customization
  // wave function properties
  const double Xmax = 2.5*TMath::Pi(); // painted region: [0.0, Xmax]
  const double omega = TMath::Pi()/2.0; //frequency
  double phiMax = 2.0*TMath::Pi(); //animation end: controls overall time
  const double dphi = phiMax/140.0; //animation step: controls speed
  if(phiMax >= Xmax){
    phiMax = Xmax*0.98;
  }

  // graphical appearance
  const int     canvasColor       = 34; // some dark blue grey
  const int     animationPadColor = 20; // some light brown
  const int     legendPadColor    = 16; // some lighter grey

  const int     waveColor         = 4; // blue
  const double  waveLineWidth     = 2;

  const int     markerStyle      = 20; // filled circle
  const int     spaceMarkerColor = 2; // red
  const double  spaceMarkerSize  = 1.5;
  const char*   spaceLegendText  = "fixed space";
  const int     timeMarkerColor  = 3; // green
  const double  timeMarkerSize   = 1.5;
  const char*   timeLegendText   = "fixed time";

  const int     clockBackColor   = 20; // some light brown
  const int     clockForeColor   = 46; // some red brown
  const int     clockFont       = 44; // helvetica-medium-r-normal/Arial(NT)

  const int     arrowColor       = 46; // some red brown

  const int     legendFont       = 44; // helvetica-medium-r-normal/Arial(NT)

  //=======================================================================

  // define function
  TF1* wave = new TF1("wave","sin([1]*x - [0])",0,Xmax);
  wave->SetLineColor(waveColor);
  wave->SetLineWidth(waveLineWidth);
  wave->SetTitle("");

  wave->SetParameter(1,omega);

  // Space and time fixed markers
  // Space
  Float_t spaceStartX = 1.0;
  Float_t spaceStartY = wave->Eval(spaceStartX);
  TMarker* spaceMark = new TMarker(spaceStartX, spaceStartY, markerStyle);
  spaceMark->SetMarkerColor(spaceMarkerColor);
  spaceMark->SetMarkerSize(spaceMarkerSize);
  // Time
  Float_t timeStartX = 1.0;
  Float_t timeStartY = wave->Eval(timeStartX);
  Float_t timeX = timeStartX;
  TMarker* timeMark = new TMarker(timeStartX,timeStartY,markerStyle);
  timeMark->SetMarkerColor(timeMarkerColor);
  timeMark->SetMarkerSize(timeMarkerSize);

  // create a canvas
  TCanvas* waveCanvas = new TCanvas("waveCanvas","Wave Animation",840,450);
  waveCanvas->SetFillColor(canvasColor);
  waveCanvas->cd();

  // create pad for wave animation
  TPad* wavePad = new TPad("wavePad","animation",0.01,0.01,0.99,0.89);
  wavePad->SetFillColor(animationPadColor);
  wavePad->SetGrid();
  wavePad->Draw();
  wavePad->cd();

  // show clock
  Double_t time = 0.0;
  char* clockBuffer[5];
  TText* timeTmp;
  sprintf(clockBuffer, "t = %.3f T", time);
  TPaveText* clockText = new TPaveText(0.82,0.84,0.98,0.97);
  clockText->SetFillColor(clockBackColor);
  clockText->SetTextColor(clockForeColor);
  clockText->AddText(clockBuffer);
  clockText->SetTextFont(clockFont);
  clockText->Draw();
  wavePad->Update();

  // create pad for legend
  waveCanvas->cd();
  TPad* legendPad = new TPad("legendPad","legend",0.01,0.9,0.99,0.99);
  legendPad->SetFillColor(legendPadColor);
  legendPad->Draw();
  legendPad->cd();
  // show legend
  TMarker* legendSpaceMark = new TMarker(0.1,0.55,20);
  legendPad->cd();
  legendSpaceMark->SetMarkerColor(spaceMarkerColor);
  legendSpaceMark->SetMarkerSize(spaceMarkerSize);
  legendSpaceMark->Draw();

  TPaveText* spaceText = new TPaveText(0.12,0.3,0.25,0.9);
  spaceText->SetFillColor(legendPadColor);
  spaceText->AddText(spaceLegendText);
  spaceText->SetTextFont(legendFont);
  spaceText->Draw();

  TMarker* legendTimeMark = new TMarker(0.4,0.55,20);
  legendPad->cd();
  legendTimeMark->SetMarkerColor(timeMarkerColor);
  legendTimeMark->SetMarkerSize(timeMarkerSize);
  legendTimeMark->Draw();

  TPaveText* timeText = new TPaveText(0.42,0.3,0.55,0.9);
  timeText->SetFillColor(legendPadColor);
  timeText->AddText(timeLegendText);
  timeText->SetTextFont(legendFont);
  timeText->Draw();

  //=======================================================================

  // animation loop
  wavePad->cd();
  spaceMark->SetX(spaceStartX);
  spaceMark->SetY(spaceStartY);
  timeMark->SetX(timeStartX);
  timeMark->SetY(timeStartY);


  for(double phi = 0.0; phi <= phiMax; phi += dphi){
    
    // draw wave
    wave->SetParameter(0,phi);
    wave->Draw();

    // draw markers
    spaceMark->SetY(wave->Eval(spaceStartX));
    spaceMark->Draw();

    timeX = timeStartX + phi/omega;
    timeMark->SetX(timeX);
    timeMark->Draw();


    // update clock
    time = phi*0.5/TMath::Pi();
    sprintf(clockBuffer, "t = %.3f T", time);
    timeTmp = clockText->GetLine(0);
    timeTmp->SetText(timeTmp->GetX(),timeTmp->GetY(),clockBuffer);
    clockText->Draw();

    wavePad->Update();
  }

  // update clock
  time = phiMax*0.5/TMath::Pi();
  sprintf(clockBuffer, "t = %.3f T", time);
  timeTmp = clockText->GetLine(0);
  timeTmp->SetText(timeTmp->GetX(),timeTmp->GetY(),clockBuffer);
  clockText->Draw();

  // draw arrows to indicate wavelength
  TArrow *arrow = new TArrow(3.32726,0.996435,4.69968,0.996435,0.03,"|>");
  arrow->SetFillColor(arrowColor);
  arrow->SetFillStyle(1001);
  arrow->SetLineColor(arrowColor);
  arrow->SetLineWidth(3);
  arrow->Draw();
  arrow = new TArrow(2.73,0.996435,1.26863,0.996435,0.03,"|>");
  arrow->SetFillColor(arrowColor);
  arrow->SetFillStyle(1001);
  arrow->SetLineColor(arrowColor);
  arrow->SetLineWidth(3);
  arrow->Draw();
  TText *text = new TText(2.86979,0.877351,"l");
  text->SetTextColor(arrowColor);
  text->SetTextFont(122);
  text->SetTextSize(0.10423);
  text->Draw();

  wavePad->Modified();
}
// end of wave.C ROOT macro file





