library(gifski)

Instructions

One of the most useful applications for linear algebra in data science is image manipulation. We often need to compress, expand, warp, skew, etc. images. To do so, we left multiply a transformation matrix by each of the point vectors.

For this assignment, build the first letters for both your first and last name using point plots in R. For example, the following code builds an H.

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

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 a .RMD file. I will know if your assignment is correct if the animation runs correctly

Student Work

Build Letters

x = c(seq(-1,0,length.out=1000),c(rep(0,200)),c(rep(0,200)),seq(0,1,length.out=1000))
y = c(rep(0,1000),seq(0,2,length.out=200),seq(0,2,length.out=200),rep(0,1000))
letters=rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3), col='blue')

Shearing

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

Horizontally.

Changing the shape and size along the axes - a tilt using the identity matrix.

#shearing - changing the shape and size along the axes - a tilt using the identity matrix
for (s in seq(0,1,length.out=25)) {
  intermediary <- apply(letters,2,function(x) sq_matrix_mult(x,matrix(c(1,s,0,1),nrow=2)))
    plot(intermediary[2,]~intermediary[1,], xlim= c(-3,3), ylim= c(-1,3), col= 'blue')
}

Vertically.

#shearing - changing the shape and size along the axes - a tilt using the identity matrix
for (s in seq(0,1,length.out=25)) {
  intermediary <- apply(letters,2,function(x) sq_matrix_mult(x,matrix(c(1,0,s,1),nrow=2,byrow=F)))
    plot(intermediary[2,]~intermediary[1,], xlim= c(-3,3), ylim = c(-1,3), col= 'blue')
}

Scaling

Applying scalar multiple to matrix to make bigger or smaller uniformly.

#scaling - applying scalar multiple to matrix to make bigger or smaller uniformly
for (s in seq(.1,10,length.out=25)) {
  intermediary <- apply(letters,2,function(x) sq_matrix_mult(x,matrix(c(s,0,0,s),nrow=2,byrow=F)))
    plot(intermediary[2,]~intermediary[1,], xlim= c(-10,10), col= 'blue')
}

Rotation

Motion around a fixed point, often a coordinate system.

#Rotation - motion around a fixed point, often a coordinate system
#https://teunbrand.github.io/ggh4x/reference/position_lineartrans.html
for (s in seq(0,5,length.out=25)) {
  intermediary <- apply(letters,2,function(x) sq_matrix_mult(x,matrix(c(cos(s),sin(s),-sin(s),cos(s)),nrow=2,byrow=F)))
    plot(intermediary[2,]~intermediary[1,], xlim= c(-3,3), col= 'blue')
}

Projection

Project coordinates from one axis while collapsing the other axis. Does not work well with my perpendicular letters.

Y-axis:

for (s in seq(0,10,length.out=25)) {
  intermediary <- apply(letters,2,function(x) sq_matrix_mult(x,matrix(c(0,0,0,s),nrow=2,byrow=F)))
    plot(intermediary[2,]~intermediary[1,], xlim= c(-10,10), col= 'blue')
}

X-axis:

for (s in seq(0,10,length.out=25)) {
  intermediary <- apply(letters,2,function(x) sq_matrix_mult(x,matrix(c(s,0,0,0),nrow=2,byrow=F)))
    plot(intermediary[2,]~intermediary[1,], xlim= c(-10,10), col= 'blue')
}

References

https://homerhanumat.github.io/r-notes/matrix-indexing.html

https://stackoverflow.com/questions/16496210/rotate-a-matrix-in-r-by-90-degrees-clockwise

https://rpubs.com/rnivas2028/image-manipulation

https://yihui.org/en/2018/08/gifski-knitr/