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)
## Warning: package 'plotly' was built under R version 4.2.2
## 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(ggplot2)
#install.packages("gifski")
library(gifski)
## Warning: package 'gifski' was built under R version 4.2.2
#install.packages("anim.plots")
library(anim.plots)
## Warning: package 'anim.plots' was built under R version 4.2.2
Build the first letters for both your first and last name using point plots in R.
x=c(seq(-1,-.5,length.out=500),
rep(-1,500),
seq(-1,-.5,length.out=500),
seq(-1,-.5,length.out=500),
seq(.5,1,length.out=500),
seq(.5,1,length.out=500),
rep(.5,500),
rep(1,500),
rep(.5,500),
seq(1,.5,length.out = 500))
y=c(rep(-1,500),
seq(-1,1,length.out=500),
rep(-0,500),
rep(1,500),
rep(1,500),
rep(0,500),
seq(-1,1,length.out=500),
seq(1,0,length.out=500),
seq(0,-1,length.out=500),
seq(-1,0,length.out = 500))
z=rbind(x,y)
plot(y~x, xlim=c(-3,3), ylim=c(-3,3),col='blue')
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
Use a loop that changes the transformation matrix incrementally to demonstrate 1) shear, 2) scaling, 3) rotation , and 4) projection in animated fashion.
SHEAR
for (i in seq(0,1,length.out=8)) {
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(-3,3), ylim=c(-3,3), col='blue')
}
SCALING
for (i in seq(0,1,length.out=10)) {
z1<-apply(z,2,function(x) leftMultiply(x,matrix(c(i,0,0,i), nrow=2, ncol=2)))
plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3), col='RED')
}
ROTATION
for (i in seq(0,pi*2,length.out=10)) {
z1<-apply(z,2,function(x) leftMultiply(x,matrix(c(cos(i), -sin(i), sin(i), cos(i)), nrow=2, ncol=2)))
plot(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3), col='YELLOW')
}
PROJECTION
for (i in seq(0,2*pi,length.out=12)) {
tempZ<-rbind(z,rep(0, ncol(z)))
z1<-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(z1[2,]~z1[1,], xlim=c(-3,3), ylim=c(-3,3), col='DARK GREEN')
}