Skip to content

Instantly share code, notes, and snippets.

@thomasp85
Created July 10, 2018 02:25
Show Gist options
  • Save thomasp85/b9e9a1bac5a0c11e24332b204e8bb90c to your computer and use it in GitHub Desktop.
Save thomasp85/b9e9a1bac5a0c11e24332b204e8bb90c to your computer and use it in GitHub Desktop.
upper <- seq(0, pi, length.out = 181)[-181]
upper <-cbind(x = cos(upper), y = sin(upper))
lower <- seq(pi, 2*pi, length.out = 181)[-181]
lower <- cbind(x = cos(lower), y = sin(lower))
right <- seq(1.5*pi, 2.5*pi, length.out = 181)[-181]
right <- cbind(x = cos(right), y = sin(right))
left <- seq(0.5*pi, 1.5*pi, length.out = 181)[-181]
left <- cbind(x = cos(left), y = sin(left))
full <- cbind(x = cos(seq(0, 2*pi, length.out = 361)[-361]),
y = sin(seq(0, 2*pi, length.out = 361)[-361]))
top <- rbind(
upper,
upper[c(T,F), ][90:1, ]/2 - matrix(c(0.5, 0), nrow = 90, ncol = 2, byrow = TRUE),
lower[c(T,F), ]/2 + matrix(c(0.5, 0), nrow = 90, ncol = 2, byrow = TRUE)
)
bottom <- rbind(
lower[180:1, ],
upper[c(T,F), ][90:1, ]/2 - matrix(c(0.5, 0), nrow = 90, ncol = 2, byrow = TRUE),
lower[c(T,F), ]/2 + matrix(c(0.5, 0), nrow = 90, ncol = 2, byrow = TRUE)
)
lefty <- sfsmisc::rot2(bottom, -pi/2)
righty <- sfsmisc::rot2(top, -pi/2)
left_spot <- sfsmisc::rot2(full, pi/2)/6 - matrix(c(0.5, 0), nrow = 360, ncol = 2, byrow = TRUE)
right_spot <- sfsmisc::rot2(full[360:1, ], pi/2)/6 + matrix(c(0.5, 0), nrow = 360, ncol = 2, byrow = TRUE)
left_dot <- cbind(x = rep(-0.5, 360), y = rep(1/12, 360))
right_dot <- cbind(x = rep(0.5, 360), y = rep(-1/12, 360))
full_start <- rbind(
lefty*2,
righty*2,
bottom,
top,
right_spot,
left_spot,
left_dot,
right_dot
)
full_start <- data.frame(
x = full_start[,1],
y = full_start[,2],
id = rep(1:8, each = 360),
col = rep(rep(c('black', 'white'), each = 360), 4),
angle = 0
)
full_end <- sfsmisc::rot2(rbind(
sfsmisc::rot2(lefty, -pi/2)*3,
sfsmisc::rot2(righty, -pi/2)*3,
lefty*2,
righty*2,
bottom,
top,
right_spot,
left_spot
), pi/2)
full_end <- data.frame(
x = full_end[,1],
y = full_end[,2],
id = rep(1:8, each = 360),
col = rep(rep(c('black', 'white'), each = 360), 4),
angle = -pi/2
)
full <- tweenr::tween_state(full_start, full_end, 'linear', 101)
full <- split(full, full$.frame)[-101]
full <- do.call(rbind, lapply(full, function(d) {
new_pos <- sfsmisc::rot2(cbind(d$x, d$y), d$angle[1])
d$x <- new_pos[,1]
d$y <- new_pos[,2]
d
}))
p <- ggplot(full) +
geom_polygon(aes(x, y, group = id, fill = col)) +
scale_fill_identity() +
coord_fixed(xlim = c(-1, 1), ylim = c(-1, 1)) +
theme_void() +
transition_manual(.frame)
animate(p, fps = 20, width = 500, height = 500)
@thomasp85
Copy link
Author

yinyang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment