Assignment 1

S

s_x=c(seq(-.5,.3,length.out=500),seq(-.3,.3,length.out=500),seq(-.3,.5,length.out=500))

s_y=c(seq(-.5,-1, length.out=225),rep(-1,50),seq(-1,-.3,length.out=225), seq(.3,-.3,length.out=500), seq(.3,1, length.out=225),rep(1,50),seq(1,.5,length.out=225))

s_z=rbind(s_x,s_y)

plot(s_y~s_x, xlim=c(-1,1), ylim=c(-1,1))

T

t_x=c(seq(-.5,.5,length.out=500),rep(0,1000), seq(-.5,.5,length.out=500))

t_y=c(rep(1,500),seq(-1,1,length.out=1000), rep(1,500))

t_z=rbind(t_x,t_y)

test <- plot(t_y~t_x, xlim=c(-1,1), ylim=c(-1,1))

Transformation Matrix

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.

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

Loop - Shear

S

img_list <- c()
for (i in seq(0, 5, length.out = 20)) {
img_list <- c(img_list, str_c(i, "_rplot.jpg"))
jpeg(str_c(i, "_rplot.jpg"), width = 350, height = 350)
shear_z <- apply(s_z, 2, function(x) left_multiply(x, matrix(c(1, i, 0, 1), nrow = 2, ncol = 2)))
plot(shear_z[2,] ~ shear_z[1,], xlim=c(-3,3), ylim=c(-3,3))
dev.off()
}
read_images <- lapply(img_list, magick::image_read)
join_images <- magick::image_join(read_images)
animate_images <- magick::image_animate(join_images, fps = 1)

animate_images

T

img_list <- c()
for (i in seq(0, 5, length.out = 20)) {
img_list <- c(img_list, str_c(i, "_rplot.jpg"))
jpeg(str_c(i, "_rplot.jpg"), width = 350, height = 350)
shear_z <- apply(t_z, 2, function(x) left_multiply(x, matrix(c(1, i, 0, 1), nrow = 2, ncol = 2)))
plot(shear_z[2,] ~ shear_z[1,], xlim=c(-3,3), ylim=c(-3,3))
dev.off()
}
read_images <- lapply(img_list, magick::image_read)
join_images <- magick::image_join(read_images)
animate_images <- magick::image_animate(join_images, fps = 1)

animate_images

Loop - Scale

S

img_list <- c()
for (i in seq(0, 5, length.out = 20)) {
img_list <- c(img_list, str_c(i, "_rplot.jpg"))
jpeg(str_c(i, "_rplot.jpg"), width = 350, height = 350)
shear_z <- apply(s_z, 2, function(x) left_multiply(x, matrix(c(i, 0, 0, i), nrow = 2, ncol = 2)))
plot(shear_z[2,] ~ shear_z[1,], xlim=c(-3,3), ylim=c(-3,3))
dev.off()
}
read_images <- lapply(img_list, magick::image_read)
join_images <- magick::image_join(read_images)
animate_images <- magick::image_animate(join_images, fps = 1)

animate_images

T

img_list <- c()
for (i in seq(0, 5, length.out = 20)) {
img_list <- c(img_list, str_c(i, "_rplot.jpg"))
jpeg(str_c(i, "_rplot.jpg"), width = 350, height = 350)
shear_z <- apply(t_z, 2, function(x) left_multiply(x, matrix(c(i, 0, 0, i), nrow = 2, ncol = 2)))
plot(shear_z[2,] ~ shear_z[1,], xlim=c(-3,3), ylim=c(-3,3))
dev.off()
}
read_images <- lapply(img_list, magick::image_read)
join_images <- magick::image_join(read_images)
animate_images <- magick::image_animate(join_images, fps = 1)

animate_images

Loop - Rotate

S

img_list <- c()
for (i in seq(0, 5, length.out = 20)) {
img_list <- c(img_list, str_c(i, "_rplot.jpg"))
jpeg(str_c(i, "_rplot.jpg"), width = 350, height = 350)
shear_z <- apply(s_z, 2, function(x) left_multiply(x, matrix(c(cos(i),-sin(i),sin(i),cos(i)), nrow = 2, ncol = 2)))
plot(shear_z[2,] ~ shear_z[1,], xlim=c(-3,3), ylim=c(-3,3))
dev.off()
}
read_images <- lapply(img_list, magick::image_read)
join_images <- magick::image_join(read_images)
animate_images <- magick::image_animate(join_images, fps = 1)

animate_images

T

img_list <- c()
for (i in seq(0, 5, length.out = 20)) {
img_list <- c(img_list, str_c(i, "_rplot.jpg"))
jpeg(str_c(i, "_rplot.jpg"), width = 350, height = 350)
shear_z <- apply(t_z, 2, function(x) left_multiply(x, matrix(c(cos(i),-sin(i),sin(i),cos(i)), nrow = 2, ncol = 2)))
plot(shear_z[2,] ~ shear_z[1,], xlim=c(-3,3), ylim=c(-3,3))
dev.off()
}
read_images <- lapply(img_list, magick::image_read)
join_images <- magick::image_join(read_images)
animate_images <- magick::image_animate(join_images, fps = 1)

animate_images

Loop - Projection

S

img_list <- c()
for (i in seq(0, 2*pi, length.out = 20)) {
img_list <- c(img_list, str_c(i, "_rplot.jpg"))
jpeg(str_c(i, "_rplot.jpg"), width = 350, height = 350)

temp_s_z <- rbind(s_z,rep(0,ncol(s_z)))

proj_z <- apply(temp_s_z, 2, function(x) left_multiply(x, matrix(c(1,0,0,0,cos(i),-sin(i),0,sin(i),cos(i)),nrow=3,ncol=3)))
plot(proj_z[2,] ~ proj_z[1,], xlim=c(-3,3), ylim=c(-3,3))
dev.off()
}
read_images <- lapply(img_list, magick::image_read)
join_images <- magick::image_join(read_images)
animate_images <- magick::image_animate(join_images, fps = 1)

animate_images

T

img_list <- c()
for (i in seq(0, 2*pi, length.out = 20)) {
img_list <- c(img_list, str_c(i, "_rplot.jpg"))
jpeg(str_c(i, "_rplot.jpg"), width = 350, height = 350)

temp_t_z <- rbind(t_z,rep(0,ncol(t_z)))

proj_z <- apply(temp_t_z, 2, function(x) left_multiply(x, matrix(c(1,0,0,0,cos(i),-sin(i),0,sin(i),cos(i)),nrow=3,ncol=3)))
plot(proj_z[2,] ~ proj_z[1,], xlim=c(-3,3), ylim=c(-3,3))
dev.off()
}
read_images <- lapply(img_list, magick::image_read)
join_images <- magick::image_join(read_images)
animate_images <- magick::image_animate(join_images, fps = 1)

animate_images