For this assignment, build the first letters for both your first and last name using point plots in R. (My initials are B & K)

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 4) projection in animated fashion.

Hint: Use x11() to open a new plotting window in R.

# Build the first letters of my First and Last Name - B K
x =c(    
     rep(-1,500),                    
     seq(-1,0,length.out=500),      
     seq(0,.25,length.out=100),  
     rep(.25,300),                     
     seq(0,.25,length.out=100),     
     seq(-1,0,length.out=500),
      seq(0,.25,length.out=100), 
      seq(.25,.25,length.out=100),
     seq(0,.25,length.out=100), 
     seq(-1,0,length.out=500),
      seq(1,2,length.out=500),
    rep(1,500),  
    rep(1,500), 
    seq(2,1,length.out = 500)
     )
 y =c(
     seq(-1,2,length.out=500),  
     rep(.5,500),                  
     seq(.5,1,length.out=100),   
     seq(1,1.5,length.out=300),  
     seq(2,1.5,length.out=100),      
     seq(-1,-1,length.out=500),
      seq(.5,0,length.out=100), 
      seq(0,-.5, length.out=100), 
     seq(-1,-.5,length.out=100), 
     rep(2,500),
     seq(.5,2,length.out=500), 
    seq(-1,2,length.out=500), 
    seq(-1,-1,length.out=500),
    seq(-1,.5,length.out = 500)
     )
 
 InitialsBK = rbind(x, y)

 plot(y~x, xlim=c(-3,4), ylim=c(-3,4), col='#671087')

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

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
# The following Loop changes the transformation matrix incrementally to demonstrate shear


for (i in seq(0, 1, length.out = 15)) {
  zShear <- apply(InitialsBK, 2, function(x) leftMultiply(x, matrix(c(1,i,0,1), nrow=2, ncol=2)))
   plot(zShear[2,]~zShear[1,], xlim=c(-3,6), ylim=c(-3,6), col='#671087', xlab = "x axis", ylab = "y axis", main =  "Transformation Matrix that demonstrate Shear of My Initials")
}

#The following Loop changes the transformation matrix incrementally to demonstrate scaling
for (i in seq(0,1,length.out=15)) {
  zScale <-apply(InitialsBK,2,function(x) leftMultiply(x,matrix(c(i,0,0,i), nrow=2, ncol=2)))
   plot(zScale[2,]~zScale[1,], xlim=c(-3,6), ylim=c(-3,6), col='#671087' , xlab = "x axis", ylab = "y axis", main =  "Transformation Matrix that demonstrate Scaling of My Initials")
}

# The following Loop changes the transformation matrix incrementally to demonstrate rotation
for (i in seq(0,pi*2,length.out=15)) {
  zRotate<-apply(InitialsBK,2,function(x) leftMultiply(x,matrix(c(cos(i), -sin(i), sin(i), cos(i)), nrow=2, ncol=2)))
   plot(zRotate[2,]~zRotate[1,], xlim=c(-3,6), ylim=c(-3,6), col='#671087', xlab = "x axis", ylab = "y axis", main =  "Transformation Matrix that demonstrate Rotation of My Initials")
}

# The following Loop changes the transformation matrix incrementally to demonstrate projection in animated fashion
for (i in seq(0,2*pi,length.out=15)) {
  tempZ<-rbind(InitialsBK,rep(0, ncol(InitialsBK)))
  zProj<-apply(tempZ,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(zProj[2,]~zProj[1,], xlim=c(-3,3), ylim=c(-3,3), col = '#671087', xlab = "x axis", ylab = "y axis", main =  "Transformation Matrix that demonstrate Projection")
}