library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
spring <- c("K1", "K2") 
valuesK1 <- seq(0, 3*pi, length.out = 200)
valuesK2 <- seq(0, 2*pi, length.out = 200)
df1 <- data.frame(spring = spring[1], values = sin(valuesK1), frame = seq(1, 1000))
df2 <- data.frame(spring = spring[2], values = cos(valuesK2), frame = seq(1, 1000))
df <- rbind(df1, df2)

plot_ly(data = df, type = "bar", quartilemethod = "linear",
        x = ~spring, y = ~values, color = ~spring, frame = ~frame,
        name = ~spring, showlegend = FALSE,
        lowerfence = list(0, 0), upperfence = list(50, 100)) %>%
  animation_opts(.1, easing = "elastic")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning: 'bar' objects don't have these attributes: 'quartilemethod', 'lowerfence', 'upperfence'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

## Warning: 'bar' objects don't have these attributes: 'quartilemethod', 'lowerfence', 'upperfence'
## Valid attributes include:
## '_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
library(plotly)


delX <- 1; delY <- 2; delZ <- 3

t <- seq(-pi, pi, 0.01) 
curve <- data.frame(
  x = cos(10*t) + delX,
  y = sin(10*t) + delY,
  z = t + delZ
)
curve_mat <- as.matrix(curve)

theta_x <- pi/2 
theta_y <- pi/4

rot_matrix_x <- matrix(c(1, 0, 0, 0, cos(theta_x), sin(theta_x), 0, -sin(theta_x), cos(theta_x)), nrow=3)
rot_matrix_y <- matrix(c(cos(theta_y), 0, -sin(theta_y), 0, 1, 0, sin(theta_y), 0, cos(theta_y)), nrow=3)


rotated_curveX <- curve_mat %*% rot_matrix_x
rotated_curveY <- curve_mat %*% rot_matrix_y 

rotated_curve_Y_X <- curve_mat %*% rot_matrix_x %*% rot_matrix_y
rotated_curve_X_Y <- curve_mat %*% rot_matrix_y %*% rot_matrix_x

std <- list(range = c(-4,4))
plot_ly(x = curve_mat[,1], y = curve_mat[,2], z = curve_mat[,3], 
        type = "scatter3d", mode = "lines", name="Original Spring") %>%
  add_trace(x=rotated_curve_Y_X[,1], y=rotated_curve_Y_X[,2],
            z=rotated_curve_Y_X[,3], 
            type = "scatter3d", mode = "lines", name="Ry(Rx)") %>%
  add_trace(x=rotated_curve_X_Y[,1], y=rotated_curve_X_Y[,2],
            z=rotated_curve_X_Y[,3], 
            type = "scatter3d", mode = "lines", name="Rx(Ry)") %>%
  add_trace(x=rotated_curveX[,1], y=rotated_curveX[,2],
            z=rotated_curveX[,3], 
            type = "scatter3d", mode = "lines", name="Rx") %>%
  add_trace(x=rotated_curveY[,1], y=rotated_curveY[,2],
            z=rotated_curveY[,3], 
            type = "scatter3d", mode = "lines", name="Ry") %>%
  layout(scene = list(xaxis = std, yaxis = std, zaxis = std, 
                      aspectmode = 'manual',
                      aspectratio = list(x = 1, y = 1, z = 1)), 
         title="Spring Rotations",
         legend = list(orientation = 'h'))