Skip to content

Instantly share code, notes, and snippets.

@marcusvolz
Last active June 28, 2021 16:36
Show Gist options
  • Save marcusvolz/ff92936a99acc0b9034fe25ff16682ce to your computer and use it in GitHub Desktop.
Save marcusvolz/ff92936a99acc0b9034fe25ff16682ce to your computer and use it in GitHub Desktop.
Butterfly knn
# The curve is a generalisation of the butterfly curve (Fay, 1989)
# See the Wiki for details: https://en.wikipedia.org/wiki/Butterfly_curve_(transcendental)
# Load packages
library(mathart) # devtools::install_github("marcusvolz/mathart")
library(tidyverse)
# Set parameters
n <- 1000
a <- 2
b <- 7
c <- 8
d <- 17
e <- 7
f <- 1
g <- 5
h <- 2
i <- 17
j <- 9
# Generate data
df <- data.frame(t = seq(0, a*pi, b*pi/n)) %>%
mutate(x = sin(t) * (exp(cos(t)) - c * cos(e*t) - (sin(t/i))^g),
y = cos(t) * (exp(cos(t)) - d * cos(f*t) - (sin(t/j))^h)) %>%
mutate(xend = lead(x), yend = lead(y)) %>%
k_nearest_neighbour_graph(k = 100)
# Create plot
p <- ggplot() +
geom_segment(aes(x, y, xend = xend, yend = yend), df, size = 0.03, alpha = 0.25) +
coord_equal() +
theme_blankcanvas(margin_cm = 0)
# Save plot
ggsave("butterfly_knn.png", width = 20, height = 20, units = "cm")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment