Skip to content

Instantly share code, notes, and snippets.

Created June 23, 2015 17:24
What would you like to do?
Harmonograf in Shiny by Antonio S. Chinchón from
# From
# All code by Antonio S. Chinchón (@aschinchon), just rearranged so that you can directly
# run it by copy and pasting it into an R console.
CreateDS = function () {
f=jitter(sample(c(2,3),4, replace = TRUE))
xt = function(t) exp(-d[1]*t)*sin(t*f[1]+p[1])+exp(-d[2]*t)*sin(t*f[2]+p[2])
yt = function(t) exp(-d[3]*t)*sin(t*f[3]+p[3])+exp(-d[4]*t)*sin(t*f[4]+p[4])
t=seq(1, 200, by=.0005)
data.frame(t=t, x=xt(t), y=yt(t))
ui = fluidPage(
titlePanel("Mathematical Beauties: The Harmonograph"),
# adding the new div tag to the sidebar
tags$div(class="header", checked=NA,
tags$p("A harmonograph is a mechanical apparatus that employs pendulums to create a
geometric image. The drawings created typically are Lissajous curves, or
related drawings of greater complexity. The devices, which began to appear
in the mid-19th century and peaked in popularity in the 1890s, cannot be
conclusively attributed to a single person, although Hugh Blackburn, a professor
of mathematics at the University of Glasgow, is commonly believed to be the official
inventor. A simple, so-called \"lateral\" harmonograph uses two pendulums to control the movement
of a pen relative to a drawing surface. One pendulum moves the pen back and forth along
one axis and the other pendulum moves the drawing surface back and forth along a
perpendicular axis. By varying the frequency and phase of the pendulums relative to
one another, different patterns are created. Even a simple harmonograph as described
can create ellipses, spirals, figure eights and other Lissajous figures (Source: Wikipedia)")),
tags$div(class="header", checked=NA,
HTML("<p>Click <a href=\"\">here</a> to see an image of a real harmonograph</p>")
actionButton('rerun','Launch the harmonograph!')
server = function(input, output) {
dat<-reactive({if (input$rerun) dat=CreateDS() else dat=CreateDS()})
plot(rnorm(1000),xlim =c(-2,2), ylim =c(-2,2), type="n")
with(dat(), plot(x,y, type="l", xlim =c(-2,2), ylim =c(-2,2), xlab = "", ylab = "", xaxt='n', yaxt='n', col="gray10", bty="n"))
}, height = 650, width = 650)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment