Skip to content

Instantly share code, notes, and snippets.

@chichacha
Created December 11, 2018 06:25
Show Gist options
  • Save chichacha/082fb1c79c4636a5c71d385d11559cc3 to your computer and use it in GitHub Desktop.
Save chichacha/082fb1c79c4636a5c71d385d11559cc3 to your computer and use it in GitHub Desktop.
Spirograph Inspired Art
---
title: "Spirograph Inspired Drawing"
output: html_notebook
editor_options:
chunk_output_type: console
---
Spirograph Inspired Drawing & Collaging Them Together.
```{r}
library(tidyverse)
library(imager)
library(magick)
library(glue)
## Function to Create Dataframe & Draw it
artwork <- function(r1=1,r2=3,r3=0,s1=1,s2=12,s3=24,save=T,points=3000,color_num=16,...){
# Create data frame based on input
df <- tibble(
k = seq.int(0,points-1),
t = seq(0,2*pi, length.out=points),
#color = factor(k%%color_num),
color = cut_interval(k,n=color_num),
x = r1 * cos(s1*t) + r2 * cos(s2*t) + r3 * cos(s3*t),
y = r1 * sin(s1*t) + r2 * sin(s2*t) + r3 * sin(s3*t)
)
col_pal = c("#F44336","#E91E63","#9C27B0","#673AB7","#3F51B5","#2196F3","#03A9F4","#00BCD4","#009688","#4CAF50","#8BC34A","#CDDC39","#FFEB3B","#FFC107","#FF9800","#FF5722","#795548","#9E9E9E","#607D8B")
# Draw the artwork!
p <-df %>%
ggplot(aes(x=x,y=y,color=color)) +
geom_point(alpha=0.5 ,size=0.8) +
geom_path(size=1) +
theme_void(base_family="Courier") +
coord_fixed() +
scale_color_manual(values=col_pal, guide="none") +
labs(title=glue(
'x = {r1} * cos({s1}*t) + {r2} * cos({s2}*t) + {r3} * cos({s3}*t)
y = {r1} * sin({s1}*t) + {r2} * sin({s2}*t) + {r3} * sin({s3}*t)
{color_num} colors used with {points} points'))
print(p)
if(save){
filename <- glue('art/{r1}_{r2}_{r3}_{s1}_{s2}_{s3}_{color_num}_{points}.png')
ggsave(filename, width=6, height=6)
}
}
artwork(r1=1,r2=3,r3=0,s1=1,s2=12,s3=0,points=3000,color_num=16)
artwork(r1=1,r2=3,r3=3,s1=1,s2=12,s3=24,points=3000,color_num=16)
artwork(5,7,3,49,24,-49,T)
artwork(5,7,3,49,27,-49,F)
artwork(5,7,3,49,9,-49,T)
artwork(0.2,0.9,1,9,2,35,F)
params <- tibble(
r1=round(rnorm(n=100)*10),
r2=round(rnorm(n=100)*10),
r3=round(rnorm(n=100)*10),
s1=round(rnorm(n=100,mean=0,sd=5)*10),
s2=round(rnorm(n=100,mean=0,sd=3)*10),
s3=round(rnorm(n=100,mean=0,sd=2)*10)
)
params %>% pmap(.,artwork)
img_list <- dir(path="art", include.dirs=T, full.names=T)
## Prepare List of Image URLs
img_list <- img_list %>% sample(size=6*8)
## Split into Lists
img_list <-img_list %>% as.tibble() %>% mutate(group=(row_number()-1)%%6)
img_list <-img_list %>% split(.$group)
## Create row of images
rows_of_imgs <-img_list %>% map(.,"value") %>%
map(.,~image_read(.) %>%
image_scale(.,"300x300") %>%
image_append(.))
## Join the above and stack them vertically!
rows_of_imgs %>%
image_join() %>%
image_append(stack=T) %>%
image_write("Spirograph_Inspired.png")
```
@chichacha
Copy link
Author

spirograph_inspired

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment