Created
March 22, 2019 13:32
-
-
Save ryantimpe/a7363a5e99dceabada150a43925beec7 to your computer and use it in GitHub Desktop.
{brickr} Example - Random Forest
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(brickr) | |
display_colors() | |
random_tree <- function(xloc, yloc, height, radius){ | |
startz <- round(height * (2/5)) | |
#Build a 3x3 trunk | |
trunk_coords <- expand.grid( x = -1:1, y = -1:1, | |
z = 1:startz) %>% | |
mutate(x=x+xloc, y=y+yloc) | |
#Add texture to the trunk color | |
trunk_color <- sample(c("Reddish brown", "Dark orange", "Dark brown"), 3, replace = TRUE) | |
trunk_coords$Color = sample(trunk_color, nrow(trunk_coords), replace = TRUE, prob = (4:2)^2) | |
#Leaves are a cone shape with radius=radius on the bottom, 1 at the top | |
leaf_coords <- expand.grid( x = (xloc-radius):(xloc+radius), | |
y = (yloc-radius):(yloc+radius), | |
z = startz:height) %>% | |
mutate(dist = ((x-median(x))^2 + (y-median(y))^2)^(1/2), | |
leaf = dist <= radius * (max(z)-z)/(max(z)-startz)) %>% | |
filter(leaf) | |
#2 leaf colors, main color is 3/4 of the tree | |
leaf_color <- sample(c("Bright green", "Bright yel. green", "Dark green", "Spring yel. green"), 2) | |
leaf_coords$Color <- sample(leaf_color, nrow(leaf_coords), replace = TRUE, prob = c(3, 1)) | |
#Combine trunk and leaves into a single tree | |
bind_rows(leaf_coords, trunk_coords) %>% | |
group_by(x,y,z) %>% | |
filter(row_number()==1) %>% | |
ungroup() | |
} | |
forest_size <- 32 | |
num_trees <- 9 | |
list(xloc = round(seq(4, forest_size-4, length.out = num_trees)), | |
yloc = sample(seq(4, forest_size-4), num_trees, replace= TRUE), | |
height = sample(10:20, num_trees, replace = TRUE), | |
radius = sample(3:5, num_trees, replace = TRUE)) %>% | |
purrr::pmap_df(random_tree) %>% | |
bind_rows(expand.grid(x=1:forest_size, y=1:forest_size, z=1, Color = "Earth green")) %>% | |
group_by(x,y,z) %>% | |
filter(row_number()==1) %>% | |
ungroup() %>% | |
bricks_from_coords() %>% | |
display_bricks() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@gfleetwood Are you using Version 0.0.0.9150 published on Friday?