Skip to content

Instantly share code, notes, and snippets.

@marcusvolz
Last active June 28, 2021 16:37
Show Gist options
  • Save marcusvolz/ddaffce2d50bcff479ae32bf40389ed9 to your computer and use it in GitHub Desktop.
Save marcusvolz/ddaffce2d50bcff479ae32bf40389ed9 to your computer and use it in GitHub Desktop.
Rose curve k-nearest neighbour graph
# Load packages
library(mathart) # devtools::install_github("marcusvolz/mathart")
library(tidyverse)
# Set parameters
# Rose curve wiki: https://en.m.wikipedia.org/wiki/Rose_(mathematics)
# Nearest neighbour graph wiki: https://en.m.wikipedia.org/wiki/Nearest_neighbor_graph
n <- 9
d <- 8 # Note: k = n/d is a curve parameter
N <- 11 # Controls the total curve length
delta <- 0.6*pi/180 # Timestep (controls the number of curve increments)
k <- 35 # Number of neighbours to connect each point to
# Generate data
df <- rose_curve(n, d, delta, N) %>%
k_nearest_neighbour_graph(k = k)
# Create plot
p <- ggplot() +
geom_segment(aes(x, y, xend = xend, yend = yend), df, size = 0.03, alpha = 0.7) +
coord_equal() +
theme_blankcanvas(margin_cm = 0)
# Save plot
ggsave("rose_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