Skip to content

Instantly share code, notes, and snippets.

@mrjoh3
Created August 27, 2018 07:35
Show Gist options
  • Save mrjoh3/e41b80a07347af5909b82628528f09c0 to your computer and use it in GitHub Desktop.
Save mrjoh3/e41b80a07347af5909b82628528f09c0 to your computer and use it in GitHub Desktop.
Animated Harmonograph
library(dplyr)
library(purrr)
library(ggplot2)
library(gganimate)
# setup variables
A1 <- 0
A2 <- 1
A3 <- 0
A4 <- 2
f1 <- 10
f2 <- 5
f3 <- 8
f4 <- 3
p1 <- 0
p2 <- pi/4
p3 <- pi/2
p4 <- 2
d1 <- 0.89
d2 <- 0
d3 <- 0.89
d4 <- 0.0005
n = 1e5
t = seq(0, 200*pi, length.out = n)
# setup cordinates
x = A1*sin(t*f1+p1)*exp(-d1*t) + A2*sin(t*f2+p2)*exp(-d2*t)
y = A3*sin(t*f3+p3)*exp(-d3*t) + A4*sin(t*f4+p4)*exp(-d4*t)
# formula from https://github.com/marcusvolz/mathart/blob/master/R/harmonograph.R
# setup colours
r = seq(0, 255, length.out = n)
g = seq(0, 255, length.out = n)
b = seq(0, 255, length.out = n)
col = rgb(r,g,b, max = 255)
plt <- df %>%
ggplot(aes(x=x, y=y, colour = col)) +
geom_point(alpha = 1, size = 0.5) +
coord_equal() +
theme_void() +
theme(legend.position="none",
panel.background = element_rect(fill = 'black')) +
transition_time(time = im) +
ease_aes('linear') +
shadow_wake(.9)
plt %>% animate(fps = 24)
anim_save("~/Desktop/rainbow_05.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment