Data 605 - Week 1

Intro

For homework we will only use core R and ggplot2 library.

rm(list=ls())
library(ggplot2)

Define a Matrix with the vectors required to plot “J” and “F”.

We will draw each line segment defining it’s X and Y coordinates.

x<- c(#J
      seq(-0.9,-0.1,length.out=500), 
      rep(-0.5,500),
      seq(-0.9,-0.5,length.out=500),
      rep(-0.9,500),
      #F
      rep(0.2,500),
      seq(0.2,0.9,length.out=500),
      seq(0.2,0.5,length.out=500))

y<- c(rep(1,500),
      seq(-1,1,length.out=500),
      rep(-1,500),
      seq(-1,-0.8,length.out=500),
      # F
      seq(-1,1,length.out=500),
      rep(1,500),
      rep(0,500))

z <- rbind(x,y)
steps <- 25
wait_t <- 0
plot(y~x, xlim=c(-2,2), ylim=c(-2,2), asp=1)

Basic Transformations.

We will apply matrix transformations to our “JF” matrix defined earlier. The transformation we will do will be

  1. Shear
  2. Rotation
  3. Enlarge
  4. Shrink
  5. Projection

Shear

# Shear
max_shear <- 2
f <- max_shear/steps 

x11()

for (i in 0:steps) {
  t <- i * f
  S <- matrix(c(1,t,0,1),nrow=2, ncol=2, byrow = TRUE)
  Transx_Sheared = S %*% z
  X <- Transx_Sheared[1,]
  Y <- Transx_Sheared[2,]
  plot(Y~X, xlim=c(-3,3), ylim=c(-3,3), asp=1)
  Sys.sleep(wait_t)
}

Rotation

# Rotation
max_rotation <- 180  #degrees
f <- max_rotation/steps 

x11()

for (i in 0:steps) {
  t <- i * f
  R <- matrix(c(cos(t * pi / 180),-sin(t * pi / 180),sin(t * pi / 180),cos(t * pi / 180)),nrow=2, ncol=2, byrow = TRUE)
  Transx_Rotated <- R %*% z
  X <- Transx_Rotated[1,]
  Y <- Transx_Rotated[2,]
  plot(Y~X, xlim=c(-3,3), ylim=c(-3,3), asp=1)
  Sys.sleep(wait_t)
}

Enlarge

# Enlarge
max_size <- 2  #times original size
f <- (max_size-1)/steps 

x11()

for (i in 0:steps) {
  t <- 1+ (i * f)
  E <- matrix(c(t,0,0,t),nrow=2, ncol=2, byrow = TRUE)
  Transx_Enlarged <- E %*% z
  X <- Transx_Enlarged[1,]
  Y <- Transx_Enlarged[2,]
  plot(Y~X, xlim=c(-3,3), ylim=c(-3,3), asp=1)
  Sys.sleep(wait_t)
}

Shrink

# Shrink
min_size <- 0.5  #percent of original size
f <- (1-min_size)/steps 

x11()

for (i in 0:steps) {
  t <- 1- (i * f)
  E <- matrix(c(t,0,0,t),nrow=2, ncol=2, byrow = TRUE)
  Transx_Shrinked <- E %*% z
  X <- Transx_Shrinked[1,]
  Y <- Transx_Shrinked[2,]
  plot(Y~X, xlim=c(-3,3), ylim=c(-3,3), asp=1)
  Sys.sleep(wait_t)
}

Projection

f <- list(1,0.75,0.5,0.25,0,-0.25,-0.5,-0.75,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1)

x11()

for (t in f){
  P = matrix(c(t,0,0,1),nrow=2, ncol=2, byrow = TRUE)
  Transx_Projected <- P %*% z
  X <- Transx_Projected[1,]
  Y <- Transx_Projected[2,]
  plot(Y~X, xlim=c(-3,3), ylim=c(-3,3), asp=1)
  Sys.sleep(0.5)
}