# A line that starts with a pound sign (hash) is called a "comment"

# If you do not have the ggplot2 and tibble packages installed, you will need to 
# run the following command:

#  install.packages(c("ggplot2", "tibble"))


library(ggplot2)

# Define variables and parameters
t <- seq(0, 4 * pi, by = 0.005) + pi / 2
min(t)
max(t)
omega <- 1
R <- 0.5

# Functions for the four cycloids.
cycloid <-  tibble::tibble(x1 =  R * cos(omega * t) + omega * R * t,
                           y1 =  R * sin(omega * t),
                           x2 =  R * cos(omega * t) + omega * R * t,
                           y2 = -R * sin(omega * t),
                           x3 =  R * cos(omega * t + pi) + omega * R * t,
                           y3 =  R * sin(omega * t + pi ),
                           x4 =  R * cos(omega * t + pi) + omega * R * t,
                           y4 = -R * sin(omega * t + pi))

# Shift all the cycloids to start at x = 0
cycloid$x2 <- cycloid$x2 - min(cycloid$x2)
cycloid$x3 <- cycloid$x3 - min(cycloid$x3)
cycloid$x4 <- cycloid$x4 - min(cycloid$x4)

cycloid$color12 <- "blue"
cycloid$color34 <- "gray"
cycloid1 <- cycloid
cycloid2 <- cycloid

cycloid2$color12 <- "gray"
cycloid2$color34 <- "blue"

space <- (max(cycloid$x1) - min(cycloid$x1)) / pi

for(i in c(1:7)){
  cycloid20 <- cycloid2
  cycloid2$x1 <- cycloid2$x1 + space * (2 * i - 1) * pi
  cycloid2$x2 <- cycloid2$x2 + space * (2 * i - 1) * pi
  cycloid2$x3 <- cycloid2$x3 + space * (2 * i - 1) * pi
  cycloid2$x4 <- cycloid2$x4 + space * (2 * i - 1) * pi
  
  cycloid10 <- cycloid1
  cycloid1$x1 <- cycloid1$x1 + space * 2 * i * pi
  cycloid1$x2 <- cycloid1$x2 + space * 2 * i * pi
  cycloid1$x3 <- cycloid1$x3 + space * 2 * i * pi
  cycloid1$x4 <- cycloid1$x4 + space * 2 * i * pi
  cycloid <- dplyr::bind_rows(cycloid, cycloid2, cycloid1)
  cycloid1 <- cycloid10
  cycloid2 <- cycloid20
}

s1 <- 0.375
g1 <- ggplot(cycloid) + 
  geom_point(aes(x = x1, y = y1, color = color12), size = s1) +
  geom_point(aes(x = x2, y = y2, color = color12), size = s1) +
  geom_point(aes(x = x3, y = y3, color = color34), size = s1) +
  geom_point(aes(x = x4, y = y4, color = color34), size = s1) +
  scale_color_manual(values = c("#003087", "#898D8D")) + 
  theme_bw() + 
  theme(legend.position = "none",
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank(),
        panel.border = element_blank())
g1
# getwd()
# setwd("path/to/where/you/want/to/save/the/file")
# The horizontal design was not used in the Newsletter.
# ggsave(plot = g1, 
#        filename = "horizontal_border.png", 
#        width = 15, 
#        height = 0.5, units  =  "in")


for(i in c(1:10)){
  cycloid20 <- cycloid2 
  cycloid2$x1 <- cycloid2$x1 + space * (2 * i - 1) * pi
  cycloid2$x2 <- cycloid2$x2 + space * (2 * i - 1) * pi
  cycloid2$x3 <- cycloid2$x3 + space * (2 * i - 1) * pi
  cycloid2$x4 <- cycloid2$x4 + space * (2 * i - 1) * pi
  
  cycloid10 <- cycloid1
  cycloid1$x1 <- cycloid1$x1 + space * 2 * i * pi
  cycloid1$x2 <- cycloid1$x2 + space * 2 * i * pi
  cycloid1$x3 <- cycloid1$x3 + space * 2 * i * pi
  cycloid1$x4 <- cycloid1$x4 + space * 2 * i * pi
  cycloid <- dplyr::bind_rows(cycloid, cycloid2, cycloid1)
  cycloid1 <- cycloid10
  cycloid2 <- cycloid20
  if(i == 10) cycloid <- dplyr::bind_rows(cycloid, cycloid2)
}

s1 <- 0.375
g1 <- ggplot(cycloid) + 
  geom_point(aes(x = y1, y = x1, color = color12), size = s1) +
  geom_point(aes(x = y2, y = x2, color = color12), size = s1) +
  geom_point(aes(x = y3, y = x3, color = color34), size = s1) +
  geom_point(aes(x = y4, y = x4, color = color34), size = s1) +
  scale_color_manual(values = c("#003087", "#898D8D")) + 
  theme_bw() + 
  theme(legend.position = "none",
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank(),
        panel.border = element_blank())
g1

# getwd()
# setwd("path/to/where/you/want/to/save/the/file")
# ggsave(plot = g1, 
#        filename = "vertical_border.png", 
#        width = 0.5, 
#        height = 20, units  =  "in")