-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruchet-tiles.qmd
62 lines (54 loc) · 1.58 KB
/
truchet-tiles.qmd
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
---
title: "Truchet tiles"
execute:
message: false
warning: false
---
```{r}
library(tidyverse)
theme_set(theme_void() + theme(legend.position = "none"))
# set up the tiles data
tile1 <- tibble(x = c(0, 0, 1, 0, 1, 1, 0, 1),
y = c(0, 1, 0, 0, 0, 1, 1, 0),
g = factor(rep(1:2, each = 4)),
tile = "tile1")
tile2 <- tibble(x = c(0, 0, 1, 0, 0, 1, 1, 0),
y = c(0, 1, 1, 0, 0, 1, 0, 0),
g = factor(rep(1:2, each = 4)),
tile = "tile2")
# the next two tiles just reverse color
tile3 <- tile1 %>%
mutate(g = factor(rep(2:1, each = 4)),
tile = "tile3")
tile4 <- tile2 %>%
mutate(g = factor(rep(2:1, each = 4)),
tile = "tile4")
set.seed(20231030)
gtile <- ggplot(tile1, aes(x, y)) +
geom_polygon(aes(fill = g, group = g), color = "black", linewidth = 2) +
coord_equal() +
# sample two colors randomly
scale_fill_manual(values = sample(colors(), 2))
gtile
gtile %+% tile2
gtile %+% tile3
gtile %+% tile4
```
Now let's make 4 x 4 times grid with these tiles!
```{r}
mygrid <- expand_grid(row = 0:3, col = 0:3) %>%
mutate(tile_id = 1:n()) %>%
rowwise() %>%
mutate(tile = sample(c("tile1", "tile2", "tile3", "tile4"), 1)) %>%
full_join(bind_rows(tile1, tile2, tile3, tile4), by = "tile") %>%
mutate(x = col + x,
y = row + y)
mygrid
```
```{r}
ggplot(mygrid, aes(x, y)) +
geom_polygon(aes(group = paste(tile_id, g), fill = g), color = "black", linewidth = 2) +
coord_equal() +
# sample two colors randomly
scale_fill_manual(values = sample(colors(), 2))
```