library(ggplot2)
library(gganimate)
library(gifski)
library(knitr)
rG <- -1
aG <- -1 #origin x coordinate since half circle for G won't be centered at (0, 0) like half circle for D is, but at (-1, 0) instead
tG <- seq(-1,-2,length.out=500)
uG <- seq(-2,-1,length.out=500)
vG <- rep(-1,250)
wG <- seq(-1,-1.5,length.out=250)
xG <- c(tG, uG, vG, wG)
yG <- c(sqrt(rG^2 - (tG-aG)^2), -1 * sqrt(rG^2 - (uG-aG)^2), seq(-1, 0, length.out=250), rep(0,250))
rD <- 1
uD <- seq(0,1,length.out=500)
vD <- seq(1,0,length.out=500)
wD <- rep(0,500)
xD <- c(uD, vD, wD)
yD <- c(sqrt(rD^2 - uD^2), -1 * sqrt(rD^2 - vD^2), seq(-1, 1, length.out=500))
x <- c(xG, xD)
y <- c(yG, yD)
z <- rbind(x, y)
plot(y~x, xlim=c(-4,4), ylim=c(-4,4))
#Identity Matrix
I <- matrix(0,nrow=2,ncol=2)
diag(I) <- 1
#Uniform Scaling
initial <- 1
final <- 2
Sxy <- seq(initial, final, length.out=25)
Tf <- I
anim_df1 <- data.frame(matrix(nrow=0,ncol=3))
colnames(anim_df1) <- c("x", "y", "state")
for (i in 1:length(Sxy)){
diag(Tf) <- Sxy[i]
zTf <- Tf %*% z
zTf <- as.data.frame(zTf)
zTf <- t(zTf)
state <- rep(i,3000)
zTf <- cbind(zTf, state)
colnames(zTf) <- c("x", "y", "state")
anim_df1 <- rbind(anim_df1, zTf)
}
p1 <- ggplot(anim_df1, aes(x = x, y = y)) +
geom_point(aes(group = seq_along(state))) +
xlim(-7, 7) +
ylim(-7, 7)
anim1 <- p1 +
transition_states(state,
transition_length = 2,
state_length = 1)
anim1
#X-Shearing
initial <- 0
final <- 3
Shx <- seq(initial, final, length.out=25)
Tf <- I
anim_df2 <- data.frame(matrix(nrow=0,ncol=3))
colnames(anim_df2) <- c("x", "y", "state")
for (i in 1:length(Shx)){
Tf[1,2] <- Shx[i]
zTf <- Tf %*% z
zTf <- as.data.frame(zTf)
zTf <- t(zTf)
state <- rep(i,3000)
zTf <- cbind(zTf, state)
colnames(zTf) <- c("x", "y", "state")
anim_df2 <- rbind(anim_df2, as.data.frame(zTf))
}
p2 <- ggplot(anim_df2, aes(x = x, y = y)) +
geom_point(aes(group = seq_along(state))) +
xlim(-7, 7) +
ylim(-7, 7)
anim2 <- p2 +
transition_states(state,
transition_length = 2,
state_length = 1)
anim2
#Y-Shearing
initial <- 0
final <- 3
Shy <- seq(initial, final, length.out=25)
Tf <- I
anim_df3 <- data.frame(matrix(nrow=0,ncol=3))
colnames(anim_df3) <- c("x", "y", "state")
for (i in 1:length(Shy)){
Tf[2,1] <- Shy[i]
zTf <- Tf %*% z
zTf <- as.data.frame(zTf)
zTf <- t(zTf)
state <- rep(i,3000)
zTf <- cbind(zTf, state)
colnames(zTf) <- c("x", "y", "state")
anim_df3 <- rbind(anim_df3, as.data.frame(zTf))
}
p3 <- ggplot(anim_df3, aes(x = x, y = y)) +
geom_point(aes(group = seq_along(state))) +
xlim(-7, 7) +
ylim(-7, 7)
anim3 <- p3 +
transition_states(state,
transition_length = 2,
state_length = 1)
anim3
#Rotation
initial <- 0
final <- 90
if (final > 0){
clockwise <- FALSE
}else{
clockwise <- TRUE
}
theta <- seq(initial, final, length.out=18)
Tf <- I
anim_df4 <- data.frame(matrix(nrow=0,ncol=3))
colnames(anim_df4) <- c("x", "y", "state")
for (i in 1:length(theta)){
cos_th <- cos(theta[i])
sin_th <- sin(theta[i])
diag(Tf) <- cos_th
if (clockwise == FALSE){
Tf[2,1] <- sin_th
Tf[1,2] <- -1 * sin_th
}else{
Tf[2,1] <- -1 * sin_th
Tf[1,2] <- sin_th
}
zTf <- Tf %*% z
zTf <- as.data.frame(zTf)
zTf <- t(zTf)
state <- rep(i,3000)
zTf <- cbind(zTf, state)
colnames(zTf) <- c("x", "y", "state")
anim_df4 <- rbind(anim_df4, as.data.frame(zTf))
}
p4 <- ggplot(anim_df4, aes(x = x, y = y)) +
geom_point(aes(group = seq_along(state))) +
xlim(-7, 7) +
ylim(-7, 7)
anim4 <- p4 +
transition_states(state,
transition_length = 2,
state_length = 1)
anim4
#2D to 1D Projection
y_inv <- 1 / z[2, ] #so that the line we are projecting to is y = 1
Tf <- I
anim_df5 <- data.frame(matrix(nrow=0,ncol=3))
colnames(anim_df5) <- c("x", "y", "state")
zero_state <- rep(0, 3000)
z_init <- as.data.frame(z)
z_init <- rbind(z_init, zero_state)
z_init <- t(z_init)
colnames(z_init) <- c("x", "y", "state")
anim_df5 <- rbind(anim_df5, z_init)
zTf <- data.frame(matrix(nrow=2,ncol=0))
j = 0
while (j < length(y_inv)){
j <- j + 1
Tf[2,2] <- y_inv[j]
zTf_vec <- Tf %*% z[ ,j]
zTf <- cbind(zTf, zTf_vec)
}
final_state <- rep(1, 3000)
zTf <- rbind(zTf, final_state)
zTf <- zTf
zTf <- t(zTf)
colnames(zTf) <- c("x", "y", "state")
anim_df5 <- rbind(anim_df5, zTf)
p5 <- ggplot(anim_df5, aes(x = x, y = y)) +
geom_point(aes(group = seq_along(state))) +
xlim(-7, 7) +
ylim(-7, 7)
anim5 <- p5 +
transition_states(state,
transition_length = 2,
state_length = 1)
anim5