Guest User

Untitled

a guest
Dec 11th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. ---
  2. title: "Spirograph Inspired Drawing"
  3. output: html_notebook
  4. editor_options:
  5. chunk_output_type: console
  6. ---
  7.  
  8. Spirograph Inspired Drawing & Collaging Them Together.
  9.  
  10.  
  11. ```{r}
  12.  
  13. library(tidyverse)
  14. library(imager)
  15. library(magick)
  16. library(glue)
  17.  
  18.  
  19. ## Function to Create Dataframe & Draw it
  20.  
  21. artwork <- function(r1=1,r2=3,r3=0,s1=1,s2=12,s3=24,save=T,points=3000,color_num=16,...){
  22. # Create data frame based on input
  23. df <- tibble(
  24. k = seq.int(0,points-1),
  25. t = seq(0,2*pi, length.out=points),
  26. #color = factor(k%%color_num),
  27. color = cut_interval(k,n=color_num),
  28. x = r1 * cos(s1*t) + r2 * cos(s2*t) + r3 * cos(s3*t),
  29. y = r1 * sin(s1*t) + r2 * sin(s2*t) + r3 * sin(s3*t)
  30. )
  31. col_pal = c("#F44336","#E91E63","#9C27B0","#673AB7","#3F51B5","#2196F3","#03A9F4","#00BCD4","#009688","#4CAF50","#8BC34A","#CDDC39","#FFEB3B","#FFC107","#FF9800","#FF5722","#795548","#9E9E9E","#607D8B")
  32. # Draw the artwork!
  33. p <-df %>%
  34. ggplot(aes(x=x,y=y,color=color)) +
  35. geom_point(alpha=0.5 ,size=0.8) +
  36. geom_path(size=1) +
  37. theme_void(base_family="Courier") +
  38. coord_fixed() +
  39. scale_color_manual(values=col_pal, guide="none") +
  40. labs(title=glue(
  41. 'x = {r1} * cos({s1}*t) + {r2} * cos({s2}*t) + {r3} * cos({s3}*t)
  42. y = {r1} * sin({s1}*t) + {r2} * sin({s2}*t) + {r3} * sin({s3}*t)
  43. {color_num} colors used with {points} points'))
  44.  
  45. print(p)
  46.  
  47. if(save){
  48. filename <- glue('art/{r1}_{r2}_{r3}_{s1}_{s2}_{s3}_{color_num}_{points}.png')
  49. ggsave(filename, width=6, height=6)
  50. }
  51.  
  52. }
  53.  
  54.  
  55. artwork(r1=1,r2=3,r3=0,s1=1,s2=12,s3=0,points=3000,color_num=16)
  56. artwork(r1=1,r2=3,r3=3,s1=1,s2=12,s3=24,points=3000,color_num=16)
  57. artwork(5,7,3,49,24,-49,T)
  58. artwork(5,7,3,49,27,-49,F)
  59. artwork(5,7,3,49,9,-49,T)
  60. artwork(0.2,0.9,1,9,2,35,F)
  61.  
  62.  
  63. params <- tibble(
  64. r1=round(rnorm(n=100)*10),
  65. r2=round(rnorm(n=100)*10),
  66. r3=round(rnorm(n=100)*10),
  67. s1=round(rnorm(n=100,mean=0,sd=5)*10),
  68. s2=round(rnorm(n=100,mean=0,sd=3)*10),
  69. s3=round(rnorm(n=100,mean=0,sd=2)*10)
  70. )
  71.  
  72.  
  73. params %>% pmap(.,artwork)
  74.  
  75.  
  76.  
  77. img_list <- dir(path="art", include.dirs=T, full.names=T)
  78.  
  79. ## Prepare List of Image URLs
  80. img_list <- img_list %>% sample(size=6*8)
  81.  
  82.  
  83. ## Split into Lists
  84. img_list <-img_list %>% as.tibble() %>% mutate(group=(row_number()-1)%%6)
  85. img_list <-img_list %>% split(.$group)
  86.  
  87.  
  88. ## Create row of images
  89. rows_of_imgs <-img_list %>% map(.,"value") %>%
  90. map(.,~image_read(.) %>%
  91. image_scale(.,"300x300") %>%
  92. image_append(.))
  93.  
  94. ## Join the above and stack them vertically!
  95. rows_of_imgs %>%
  96. image_join() %>%
  97. image_append(stack=T) %>%
  98. image_write("Spirograph_Inspired.png")
  99.  
  100. ```
Add Comment
Please, Sign In to add comment