Instructions

For this assignment, build the first letters for both your first and last name using point plots in R.
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.

Load Packages

Plot Initials

x = c(rep(0,500), seq(0,0, length.out=1000), # vertical line - K
       rep(0,500), seq(0,1, length.out = 1000), # first diagonal - K
       rep(0,500), seq(0,1, length.out = 1000), # second diagonal - K
       rep(0,500), seq(2,2, length.out = 1000), # vertical line - L
       rep(0,500), seq(2,3, length.out = 1000)) # horizontal line - L
y = c(rep(0,500), seq(-1,1, length.out = 1000), # vertical line - K
       rep(0,500), seq(0,1, length.out = 1000), # first diagonal - K
       rep(0,500), seq(0,-1, length.out = 1000), # second diagonal - K
       rep(0,500), seq(-1,1, length.out = 1000), # vertical line - L
       rep(0,500), seq(-1,-1, length.out = 1000)) # horizontal line - L

z = rbind(x, y) # matrix

plot(y~x, xlim=c(-1,4), ylim=c(-2,2), col = "turquoise",
     main = "Initials") # plot

Left Multiply

leftMultiply <- function(x,y){x %*% y}
leftMultiply(matrix(rep(seq(1,3, length.out = 3), 3), nrow = 3, ncol = 3), diag(3))
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    2    2
## [3,]    3    3    3

Shear

invisible(saveGIF(for (i in seq(0, 1, length.out = 20)) {
  z1 <- apply(z, 2, function(x) leftMultiply(x,matrix(c(1, i, 0, 1), nrow = 2, ncol = 2)))
  plot(z1[2,] ~ z1[1,], xlim = c(-1,4), ylim = c(-2,2), 
       main = "Shear", xlab = "x", ylab = "y", col = 'turquoise')
}, movie.name = 'shear_animation.gif'))

include_graphics("shear_animation.gif")

Scaling

invisible(saveGIF(for (i in seq(0, 1, length.out = 20)) {
  z2 <- apply(z, 2, function(x) leftMultiply(x, matrix(c(i, 0, 0, i), nrow = 2, ncol = 2)))
  plot(z2[2,] ~ z2[1,], xlim = c(-1, 4), ylim = c(-2, 2),
       main = "Scale", xlab = 'x', ylab = 'y', col = 'turquoise')
}, movie.name = 'scale_animation.gif'))

include_graphics("scale_animation.gif")

Rotation

invisible(saveGIF(for (i in seq(0, pi*3, length.out = 50)) {
  z3 <- apply(z, 2, function(x) leftMultiply(x, matrix(c(cos(i), -sin(i), sin(i), cos(i)), nrow = 2, ncol = 2)))
  plot(z3[2,] ~ z3[1,], xlim = c(-1,4), ylim = c(-2, 2),
       main = "Rotation", xlab = 'x', ylab = 'y', col = 'turquoise')
}, movie.name = 'rotate_animation.gif'))

include_graphics("rotate_animation.gif")

Projection

invisible(saveGIF(for (i in seq(0, pi*3, length.out = 50)) {
  z_proj <- rbind(z, rep(0, ncol(z)))
  z4 <- apply(z_proj, 2, function(x) leftMultiply(x, matrix(c(1,0,0,0, cos(i), -sin(i), 0 , sin(i), cos(i)), nrow = 3, ncol = 3)))
  plot(z4[2,] ~ z4[1,], xlim = c(-1,4), ylim = c(-2, 2),
       main = "Projection", xlab = 'x', ylab = 'y', col = 'turquoise')
}, movie.name = 'project_animation.gif'))

include_graphics("project_animation.gif")