
// AnimatedSineWave.java
// https://imagej.net/ij/plugins/animated-sine.html
// Andrew Davison, ad@coe.psu.ac.th, April 2023

/* Use the Plot class to create an animated sine wave
   by building a sinesStk of images.

   Usage:
     > compile AnimatedSineWave.java
     > run AnimatedSineWave
*/

import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.plugin.PlugIn;


public class AnimatedSineWave
{
  private static final int NUM_FRAMES = 50;  
                  // number of frames in the sines stack
  private static final int NUM_PTS = 100; 
                  // number of points in one sine curve


  public static void main(String[] args)
  {
    double start = Math.PI*1.5;
    ImageStack sinesStk = null;

    double[] xs = new double[NUM_PTS];
    double[] ys = new double[NUM_PTS];

    for (int i=0; i < NUM_FRAMES; i++) {
      for (int j=0; j < NUM_PTS; j++) {
        double angle = j*(Math.PI*4/NUM_PTS);
        xs[j] = angle;
        ys[j] = Math.sin(start+angle);  // start keeps increasing
      }

      start += Math.PI*4/NUM_PTS;
      Plot plot = new Plot("Sine Wave", "x", "y");
      plot.add("line", xs, ys);

      ImageProcessor ip = plot.getProcessor();
      if (i==0)
        sinesStk = new ImageStack(ip.getWidth(), ip.getHeight());
      sinesStk.addSlice(null, ip);
    }

    new ImagePlus("Animated Sine Wave", sinesStk).show();

    ImageUtils.waitGone("Animated Sine Wave");
    System.exit(0);
  } /* main */


} // end of AnimatedSineWave class
