library(gifski)
# Plot M

x <- c(rep(-1.5,500),
       seq(-1,-1.5,length.out=500), 
       seq(-1,-0.5,length.out=500),
       #rep(1,500), 
       #seq(2,1,length.out=500), 
       rep(-0.5,500))
y <- c(seq(-2,2,length.out=500), 
       seq(-2,2,length.out=500), 
       seq(-2,2,length.out=500), 
       seq(-2,2,length.out=500)) 
z=rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))

# Plot H
x=c(rep(0,500),
    seq(0,1,length.out=1000), 
    rep(1,500))
y=c(seq(-2,2,length.out=500),
    rep(0,1000), 
    seq(-2,2,length.out=500))
z=rbind(x,y)
plot(y~x, 
     xlim=c(-3,3), 
     ylim=c(-3,3))

# Plot MH together
x <- c(rep(-1.5,500),
       seq(-1,-1.5,length.out=500), 
       seq(-1,-0.5,length.out=500),
       #rep(1,500), 
       #seq(2,1,length.out=500), 
       rep(-0.5,500),
       rep(0,500),
       seq(0,1,length.out=1000), 
       rep(1,500))
y <- c(seq(-2,2,length.out=500), 
       seq(-2,2,length.out=500), 
       seq(-2,2,length.out=500), 
       seq(-2,2,length.out=500),
       seq(-2,2,length.out=500),
       rep(0,1000), 
       seq(-2,2,length.out=500))
       #rep(-2,500),
       #seq(-2,1,length.out=500))
z=rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))

# 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.

identity_matrix <- function(x,y) {
  x %*% y
}

#identity_matrix(matrix(rep(seq(1,3, length.out=3), 3), nrow=3, ncol=3), diag(3))

Shear

x11()
for (a in seq(0,2,length.out=20)) {
  z1<-apply(z,2,function(x) identity_matrix(x,matrix(c(1,a,0,1),nrow=2,ncol=2)))
   plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3), col='blue',
        main="Shear Transformation Animation", xlab="x-axis", ylab = "y-axis",
        col.main="red", col.lab="navy")
}

Scaling

x11()
for (a in seq(0,2,length.out=20)) {
  z1<-apply(z,2,function(x) identity_matrix(x,matrix(c(a,0,0,a),nrow=2,ncol=2)))
   plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3), col='blue',
        main="Scaling Transformation Animation", xlab="x-axis", ylab = "y-axis",
        col.main="red", col.lab="navy")
}

Rotation

x11()
for (a in seq(0,6,length.out=20)) {
  z1<-apply(z,2,function(x) identity_matrix(x,matrix(c(cos(a),-sin(a),sin(a),cos(a)),nrow=2,ncol=2)))
   plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3), col='blue',
        main="Rotation Transformation Animation", xlab="x-axis", ylab = "y-axis",
        col.main="red", col.lab="navy")
}

Projection

x11()
for (a in seq(0,9,length.out=20)) {
  tmpz<-rbind(z,rep(0,ncol(z)))
  z1<-apply(tmpz,2,function(x) identity_matrix(x,matrix(c(1,0,0,0,cos(a),-sin(a),0,sin(a),cos(a)),nrow=3,ncol=3)))
   plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3), col='blue',
        main="Projection Transformation Animation", xlab="x-axis", ylab = "y-axis",
        col.main="red", col.lab="navy")
}