Last active
December 31, 2021 04:28
-
-
Save dodger487/423a45f429c375680e031179bc8496be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(shades) | |
n_rectangles <- 10000 | |
rect_scale_factor <- 1.5 | |
canvas_size <- 100 | |
xlims <- c(0, canvas_size) | |
ylims <- c(0, canvas_size) | |
height <- 2 | |
width <- 7 | |
make_rect <- function(x, y, height, width) { | |
tibble( | |
x = c(x, x, x + width, x + width, x), | |
y = c(y, y + height / 2, y + height, y + height / 2, y) | |
) | |
} | |
tibble( | |
x = runif(n_rectangles) * (canvas_size + 20) - 10, | |
y = runif(n_rectangles) * (canvas_size + 20) - 10 | |
) %>% | |
mutate( | |
id = row_number(), | |
base_color = '#00ff00', | |
layer = rlnorm(n(), sd = .7), | |
max_layer = max(layer) | |
) %>% | |
rowwise() %>% | |
summarize(make_rect(x, y, layer*rect_scale_factor, layer*rect_scale_factor), | |
id = id, base_color = base_color, layer=layer, max_layer = max_layer) %>% | |
rowwise() %>% | |
# This line takes a very long time and there is probably a better way to do it! | |
mutate(color = lightness(base_color, scalefac(0.94^((max_layer-layer)^1.1)))) %>% | |
ungroup() %>% | |
# The group = layer*10 thing is because geom_polygon plots in order of group. | |
# Therefore, need to encode the layer (which is how close to the screen a poly is) in the group ID. | |
ggplot(aes(x=x, y=y, fill=color, group=layer*10*n_rectangles + id)) + | |
geom_polygon(color="black") + | |
scale_fill_identity() + | |
theme_void() + | |
coord_fixed(xlim = xlims, ylim = ylims) + | |
theme(panel.background = element_rect(fill = 'black', colour = 'black')) + | |
NULL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment