Skip to content

Instantly share code, notes, and snippets.

@rasmusab
Created June 23, 2015 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasmusab/7bdcb599ed790c8d40ac to your computer and use it in GitHub Desktop.
Save rasmusab/7bdcb599ed790c8d40ac to your computer and use it in GitHub Desktop.
Harmonograf in Shiny by Antonio S. Chinchón from https://aschinchon.wordpress.com/2015/06/23/the-2d-harmonograph-in-shiny/
# From https://aschinchon.wordpress.com/2015/06/23/the-2d-harmonograph-in-shiny/
# 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.
library(shiny)
CreateDS = function () {
f=jitter(sample(c(2,3),4, replace = TRUE))
d=runif(4,0,1e-02)
p=runif(4,0,pi)
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))
}
shinyApp(
ui = fluidPage(
titlePanel("Mathematical Beauties: The Harmonograph"),
sidebarLayout(
sidebarPanel(
#helpText(),
# 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=\"http://paulbourke.net/geometry/harmonograph/harmonograph3.html\">here</a> to see an image of a real harmonograph</p>")
),
actionButton('rerun','Launch the harmonograph!')
),
mainPanel(
plotOutput("HarmPlot")
)
)
),
server = function(input, output) {
dat<-reactive({if (input$rerun) dat=CreateDS() else dat=CreateDS()})
output$HarmPlot<-renderPlot({
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