This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(ggplot2)
library(gganimate)
library(gifski)
Then, write R code that will left multiply (%*%) a square matrix (x) against each of the vectors of points (y). Initially, that square matrix will be the Identity matrix.
Use a loop that changes the transformation matrix incrementally to demonstrate 1) shear, 2) scaling, 3) rotation , and 4) projection in animated fashion.
Hint: Use x11() to open a new plotting window in R.
Upload your document as an .RMD file. I will know if your assignment is correct if the animation runs. correctly.
x=c(rep(-2,500),
seq(-2,-1,length.out=500),
seq(-1,0,length.out=500),
rep(0,500), rep(1,500),
seq(1,2,length.out=500),
seq(1,2,length.out=500),
rep(2,500),rep(2,500),
seq(2,1.5,length.out=500),
seq(2,1.5,length.out=500),
seq(1.5,1,length.out=500) )
y=c(seq(-1,1,length.out=500),
seq(1,-1,length.out=500),
seq(-1,1,length.out=500),
seq(-1,1,length.out=500),
seq(-1,1,length.out=500),
rep(1,500),rep(-1,500),
seq(0.5,1,length.out=500),
seq(-0.5,-1,length.out=500),
seq(0.5,0,length.out=500),
seq(-0.5,0,length.out=500),
rep(0,500) )
z=rbind(x,y)
plot(y~x, xlim=c(-2,3), ylim=c(-2,3))
Creating the shearing animation by updating the transformation matrix.
scale <- as.numeric(seq(0, 5, by = 0.2 ))
x11()
for (val in scale) {
t_matrix <- matrix(c(1, 0, val, 1), nrow=2, ncol=2)
shearing <- apply(z, 2, function(x) x %*% t_matrix)
plot(shearing[2,] ~ shearing[1,], xlim=c(-3, 3), ylim=c(-3, 3))
}
Creating the scaling animation by updating the transformation matrix.
x11()
for (val in scale) {
ext_z <- rbind(z, numeric(3000))
trans_matrix <- matrix(c(cos(val), sin(val), 0, -sin(val), cos(val), 0, 0, 0, 1), nrow=3, ncol=3)
scaling <- apply(ext_z, 2, function(x) x %*% trans_matrix)
plot(scaling[2,] ~ scaling[1,], xlim=c(-3,3), ylim=c(-3,3))
}
Creating the rotation animation by updating the transformation matrix.
x11()
for (val in scale) {
rot_z <- rbind(z, numeric(3000))
trans_matrix <- matrix(c(cos(val), sin(val), 0, -sin(val), cos(val), 0, 0, 0, 1), nrow=3, ncol=3)
rotation <- apply(rot_z, 2, function(x) x %*% trans_matrix)
plot(rotation[2,] ~ rotation[1,], xlim=c(-3,3), ylim=c(-3,3))
}
Creating the projection animation by updating the transformation matrix.
x11()
for (val in seq(-3,3,length.out=50)) {
proj_z <- rbind(z, numeric(3000))
trans_matrix <- matrix(c(val,0,0,0,1,0,0,0,1), nrow=3, ncol=3)
projection <- apply(proj_z, 2, function(x) x %*% trans_matrix)
plot(projection [2,] ~ projection [1,], xlim=c(-3,3), ylim=c(-3,3))
}