-
Notifications
You must be signed in to change notification settings - Fork 12
/
entrypoint.R
61 lines (50 loc) · 1.71 KB
/
entrypoint.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env Rscript
# Load in libraries
library(ggplot2)
library(dplyr)
library(deldir)
library(colourlovers)
library(rlist)
args <- commandArgs(trailingOnly = TRUE)
iter = as.numeric(args[1])
radius = as.numeric(args[2])
points = as.numeric(args[3])
outfile = args[4]
# Angles of points from center
angles=seq(0, 2*pi*(1-1/points), length.out = points)+pi/2
# Initial center
df=data.frame(x=0, y=0)
# Iterate over centers again and again
for (k in 1:iter)
{
temp=data.frame()
for (i in 1:nrow(df))
{
data.frame(x=df[i,"x"]+radius^(k-1)*cos(angles),
y=df[i,"y"]+radius^(k-1)*sin(angles)) %>% rbind(temp) -> temp
}
df=temp
}
# Function to extract id, coordinates and area of each polygon
crea = function(tile) {tile %>% list.match("ptNum|x|y|area") %>% as.data.frame()}
# Generate tesselation, obtain polygons and create a dataframe with results
# This dataframe will be the input of ggplot
df %>%
deldir(sort = TRUE) %>%
tile.list() %>%
list.filter(sum(bp)==0) %>%
list.filter(length(intersect(which(x==0), which(y==0)))==0) %>%
lapply(crea) %>%
list.rbind() -> df_polygon
# Pick a random top palette from colourLovers
palette <- sample(clpalettes('top'), 1)[[1]] %>% swatch %>% .[[1]]
# Draw mandala with geom_polygon. Colur depends on area
ggplot(df_polygon, aes(x = x, y = y)) +
geom_polygon(aes(fill = area, color=area, group = ptNum),
show.legend = FALSE, size=0)+
scale_fill_gradientn(colors=sample(palette, length(palette))) +
scale_color_gradientn(colors="gray30") +
theme_void()
# Do you like the result? You can save it (change the filename):
print(paste("Saving Mandala to", outfile, "..."))
ggsave(outfile, height=5, width=5, units='in', dpi=600)