#install.packages("circular")
library("circular")
library("animation")

saveHTML(
  {
  oopt = ani.options(interval = 0.2)
  set.seed(1234)
  x <- rvonmises(n=1000, mu=circular(pi/5), kappa=3)
  y <- rvonmises(n=1000, mu=circular(-pi/3), kappa=5)
  z <- rvonmises(n=1000, mu=circular(0), kappa=10)
  r <- runif(n=1000, min=0, max=2)
  vectorX = as.vector(x)
  vectorY = as.vector(y)
  vectorZ = as.vector(z)
  plotX = c(vectorX[1])
  plotY = c(vectorY[1])
  plotZ = c(vectorZ[1])
  for (i in 2:1000){
    plotX = c(plotX,vectorX[i])
    plotY = c(plotY,vectorY[i])
    plotZ = c(plotZ,vectorZ[i])
    
    # Try to "stack=T the points ....
    #r1 = sqrt((resx$x)^2+(resx$y)^2)/2; 
    #resx$x = r1*cos(resx$data)
    #resx$x = r1*cos(resx$data)
    
    tempX = circular(plotX)
    tempY = circular(plotY)
    tempZ = circular(plotZ)
    resx <- density(tempX, bw=25, xaxt='n', yaxt='n')
    resy <- density(tempY, bw=25, xaxt='n', yaxt='n')
    resz <- density(tempZ, bw=25, xaxt='n', yaxt='n')
    res <- plot(resx, points.plot=TRUE, xlim=c(-1.1,1.5), ylim=c(-1.5, 1.5), 
                main="Trivariate random sampling of\n kime-magnitudes (times) and kime-directions (phases)", 
                offset=0.9, shrink=1.0, ticks=T, lwd=3, axes=F, stack=TRUE, bins=150)
    lines(resy, points.plot=TRUE, col=2, points.col=2, plot.info=res, offset=1.0, shrink=1.45, lwd=3, stack=T)
    lines(resz, points.plot=TRUE, col=3, points.col=3, plot.info=res, offset=1.1, shrink=1.2, lwd=3, stack=T)
    segments(0, 0, r[i]*cos(vectorX[i]), r[i]*sin(vectorX[i]), lwd=2, col= 'black')
    segments(0, 0, r[i]*cos(vectorY[i]), r[i]*sin(vectorY[i]), lwd=2, col= 'red')
    segments(0, 0, r[i]*cos(vectorZ[i]), r[i]*sin(vectorZ[i]), lwd=2, col= 'green')
    ani.pause()
  }
  
  
  # To save animation as MP4 video
  saveVideo({ 
    oopt = ani.options(interval = 0.2)
    set.seed(1234)
    x <- rvonmises(n=1000, mu=circular(pi/5), kappa=3)
    y <- rvonmises(n=1000, mu=circular(-pi/3), kappa=5)
    z <- rvonmises(n=1000, mu=circular(0), kappa=10)
    r <- runif(n=1000, min=0, max=2)
    vectorX = as.vector(x)
    vectorY = as.vector(y)
    vectorZ = as.vector(z)
    plotX = c(vectorX[1])
    plotY = c(vectorY[1])
    plotZ = c(vectorZ[1])
    for (i in 2:1000){
      plotX = c(plotX,vectorX[i])
      plotY = c(plotY,vectorY[i])
      plotZ = c(plotZ,vectorZ[i])
      
      # Try to "stack=T the points ....
      #r1 = sqrt((resx$x)^2+(resx$y)^2)/2; 
      #resx$x = r1*cos(resx$data)
      #resx$x = r1*cos(resx$data)
      
      tempX = circular(plotX)
      tempY = circular(plotY)
      tempZ = circular(plotZ)
      resx <- density(tempX, bw=25, xaxt='n', yaxt='n')
      resy <- density(tempY, bw=25, xaxt='n', yaxt='n')
      resz <- density(tempZ, bw=25, xaxt='n', yaxt='n')
      res <- plot(resx, points.plot=TRUE, xlim=c(-1.1,1.5), ylim=c(-1.5, 1.5), 
                  main="Trivariate random sampling of\n kime-magnitudes (times) and kime-directions (phases)", 
                  offset=0.9, shrink=1.0, ticks=T, lwd=3, axes=F, stack=TRUE, bins=150)
      lines(resy, points.plot=TRUE, col=2, points.col=2, plot.info=res, offset=1.0, shrink=1.45, lwd=3, stack=T)
      lines(resz, points.plot=TRUE, col=3, points.col=3, plot.info=res, offset=1.1, shrink=1.2, lwd=3, stack=T)
      segments(0, 0, r[i]*cos(vectorX[i]), r[i]*sin(vectorX[i]), lwd=2, col= 'black')
      segments(0, 0, r[i]*cos(vectorY[i]), r[i]*sin(vectorY[i]), lwd=2, col= 'red')
      segments(0, 0, r[i]*cos(vectorZ[i]), r[i]*sin(vectorZ[i]), lwd=2, col= 'green')
      ani.pause()
    }
    }, 
    video.name = "C:/Users/Dinov/Desktop/KimeRandomSamplingAnimation.mp4", 
    other.opts = "-pix_fmt yuv420p -b:v 500K",
    ffmpeg = "E:/Ivo.dir/Ivo_Tools/ffmpeg-20200218-ebee808-win64-static/bin/ffmpeg.exe", 
    ani.width = 600, ani.height = 600
  )

  },
  htmlfile  = "Chapter6_Kime_Phases.html"
  
)


