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.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(gifski)
library(anim.plots)
# Plotting M
x=c(rep(-2,1000),seq(-2,-1,length.out=500), seq(-1,0,length.out=500), rep(0,1000))
y=c(seq(-1,2,length.out=1000),seq(2, 1,length.out=500), seq(1, 2,length.out=500), seq(2,-1,length.out=1000))
z=rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))
# Plotting Q
x=c(seq(1,2,length.out = 500), seq(1,2,length.out = 500), seq(1.5,2,length.out=1000))
y=c(sqrt(1-(seq(0,2.0,length.out = 500)-1)^2), -sqrt(1-(seq(0,2.0,length.out = 500)-1)^2),seq(0,-2,length.out=1000))
z=rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))
# Plotting MQ
x=c(rep(-2,1000),seq(-2,-1,length.out=500), seq(-1,0,length.out=500), rep(0,1000), seq(1,2,length.out = 500), seq(1,2,length.out = 500), seq(1.5,2,length.out=1000))
y=c(seq(-1,2,length.out=1000),seq(2, 1,length.out=500), seq(1, 2,length.out=500), seq(2,-1,length.out=1000), sqrt(1-(seq(0,2.0,length.out = 500)-1)^2), -sqrt(1-(seq(0,2.0,length.out = 500)-1)^2),seq(0,-2,length.out=1000))
z=rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3))
# matrix from the x and y coordinates
coord_matrix = matrix(c(x, y), ncol=2)
# Identity matrix plug in with k to convert to scaling matrix
k <- 3
identity_matrix <- matrix(c(k, 0, 0, k), ncol=2)
scale_matrix <- identity_matrix
scale_matrix
## [,1] [,2]
## [1,] 3 0
## [2,] 0 3
for (k in 1:5) {
identity_matrix <- matrix(c(k, 0, 0, k), ncol=2)
scale_matrix <- identity_matrix
transformed_matrix_scale= coord_matrix %*% scale_matrix
z<-transformed_matrix_scale
plot(z)
}
### Shear
# Horizontal Shear
for (k in 1:5) {
shear_matrix <- matrix(c(1, 0, k, 1), ncol=2)
transformed_matrix_shear = coord_matrix %*% shear_matrix
z<-transformed_matrix_shear
plot(z)
}
### Vertical Shear
for (k in 1:2) {
shear_matrix <- matrix(c(1, k, 0, 1), ncol=2)
transformed_matrix_shear = coord_matrix %*% shear_matrix
z<-transformed_matrix_shear
plot(z)
}
### x-axis Reflection Matrix
for (k in 1:5) {
reflection_matrix <- matrix(c(k, 0, 0, -k), ncol=2)
transformed_matrix_reflection = coord_matrix %*% reflection_matrix
z<-transformed_matrix_reflection
plot(z)
}
### Reflection across the line y=x
for (k in 1:5) {
reflection_matrix <- matrix(c(0, k, k, 0), ncol=2)
transformed_matrix_reflection = coord_matrix %*% reflection_matrix
z<-transformed_matrix_reflection
plot(z)
}
for (k in 1:5) {
Projection_matrix <- matrix(c(1, 0, 0, 0), ncol=2)
transformed_matrix_projection = coord_matrix %*% Projection_matrix
z<-transformed_matrix_projection
plot(z)
}
for (k in 1:5) {
Projection_matrix <- matrix(c(0, 0, 0, 1), ncol=2)
transformed_matrix_projection = coord_matrix %*% Projection_matrix
z<-transformed_matrix_projection
plot(z)
}