Skip to contents

Draws the curve traced by a harmonograph: two damped pendulums move the pen along x and two along y.

Usage

draw_harmonograph(
  frequency = NULL,
  damping = NULL,
  phase = NULL,
  t = seq(1, 500, by = 0.01),
  seed = NULL,
  color = NULL,
  alpha = 0.2,
  line_width = 1,
  pad = DEFAULT_PAD,
  title = NULL,
  theme = NULL,
  margins = DEFAULT_MARGINS,
  width = NULL,
  height = NULL,
  element_id = NULL,
  filename = NULL
)

Arguments

frequency

Optional Numeric: Pendulum frequencies, length 4. The first two move the pen along x, the last two along y.

damping

Optional Numeric [0, Inf): Pendulum damping rates, length 4.

phase

Optional Numeric: Pendulum phases in radians, length 4.

t

Numeric: Time points at which the curve is evaluated.

seed

Optional Integer: Seed for the random parameters. The caller's random number generator state is restored on exit. NULL uses the current state.

color

Optional Character: Line color. NULL uses the theme palette's first color.

alpha

Numeric [0, 1]: Line opacity.

line_width

Numeric (0, Inf): Line width in pixels.

pad

Numeric [0, Inf): Fraction of the curve's larger range added on each side.

title

Optional Character: Chart title.

theme

Optional Theme: Theme override. NULL follows the light or dark appearance of the viewer.

margins

Optional Named numeric vector or named list: Plot margins in pixels for any of "top", "right", "bottom", "left".

width

Optional Character or Numeric: Widget width.

height

Optional Character or Numeric: Widget height.

element_id

Optional Character: Explicit DOM element ID for the widget container. NULL lets htmlwidgets generate one.

filename

Optional Character: If provided, save the widget to this file via save_drawing().

Value

htmlwidget: Widget object.

Details

The curve is $$x(t) = e^{-d_1 t} \sin(f_1 t + p_1) + e^{-d_2 t} \sin(f_2 t + p_2)$$ $$y(t) = e^{-d_3 t} \sin(f_3 t + p_3) + e^{-d_4 t} \sin(f_4 t + p_4)$$ with frequencies \(f\), damping rates \(d\) and phases \(p\).

Parameters left NULL are drawn at random: each frequency is 2 or 3, perturbed by base::jitter(); damping rates are uniform on [0, 0.01]; phases are uniform on [0, pi]. All 12 parameters are drawn in that order and supplied vectors replace their drawn counterparts, so a fixed seed keeps the remaining parameters fixed whichever vectors are supplied. A given seed yields the same parameters as mplot3_harmonograph() in rtemis 0.98. Seeds to try include 9, 17, 26, 202 and 208.

The line follows the theme: color = NULL uses the first palette color, on the theme background. The curve is drawn as up to 500 consecutive segments, and the opacity of overlapping segments accumulates, so dense regions of the curve appear brighter.

The curve formulation follows the Fronkonstin blog post "Beautiful Curves: The Harmonograph" (2014).

Author

EDG

Examples

draw_harmonograph(seed = 9)
draw_harmonograph(
  frequency = c(2, 3, 3, 2),
  damping = rep(0.004, 4),
  phase = c(0, pi / 2, pi / 4, 0),
  alpha = 0.5
)