-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBB8.R
252 lines (194 loc) · 8.22 KB
/
BB8.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# July 31, 2019
# Creates the BB8 from Star Wars
# Code from: https://www.r-graph-gallery.com/144-droid-bb-8-data-art/
# Packages ----------------------------------------------------------------
library("dplyr")
library("ggplot2")
library("sp")
library("rgeos")
# Funs --------------------------------------------------------------------
coord_circle <- function(centre = c(0, 0), r = 1, n = 1000) {
data_frame(
x = seq(from = 0 - r, to = 0 + r, length.out = n %/% 2),
y = sqrt(r^2 - x^2)
) %>% bind_rows(., -.) %>%
mutate(x = x + centre[1], y = y + centre[2])
}
create_poly <- function(...) {
args <- list(...)
SpatialPolygons(
lapply(
X = seq_along(args),
FUN = function(x) {
Polygons(list(Polygon(as.data.frame(args[[x]]))), names(args)[x])
}
)
)
}
echancrure <- function(to_var, by_var, p = 0.1) {
ind <- which(by_var >= -0.08 & by_var <= 0.08 & to_var > 0)
to_var[ind] <- to_var[ind] - p
ind <- which(by_var >= -0.08 & by_var <= 0.08 & to_var < 0)
to_var[ind] <- to_var[ind] + p
return(to_var)
}
# BB-8 geometries ---------------------------------------------------------
# droid_body -------------------------------------------------------------------
# shape of the droid_body : two circles and a vertical line
droid_body <- coord_circle(centre = c(0, 0), r = 1)
droid_body$xvert <- 0
droid_body$yvert <- droid_body$x
droid_body <- bind_cols(
droid_body,
coord_circle(centre = c(0, 0), r = 0.35, n = nrow(droid_body)) %>% select(xint = x, yint = y)
)
# grey shapes in the central inner circle
droid_body_rect <- data_frame(
x = c(-0.5, 0.5, 0.5, -0.5, c(-0.5, 0.5, 0.5, -0.5) - 0.2, c(-0.5, 0.5, 0.5, -0.5) + 0.2),
y = c(-0.6, 0.4, 0.6, -0.4, c(-0.6, 0.4, 0.6, -0.4) + 0.2, c(-0.6, 0.4, 0.6, -0.4) - 0.2),
group = rep(1:3, each = 4)
)
# a polygon for calculate the intersection between the grey shapes and the inner circle
polyrect <- create_poly(
"polyrect1" = droid_body_rect[droid_body_rect$group == 1, 1:2],
"polyrect2" = droid_body_rect[droid_body_rect$group == 2, 1:2],
"polyrect3" = droid_body_rect[droid_body_rect$group == 3, 1:2]
)
polycircle <- create_poly(
"polycircle" = droid_body[, c("xint", "yint")]
)
# plot(polyrect); plot(polycircle, add = TRUE)
polyrect <- gIntersection(spgeom1 = polyrect, spgeom2 = polycircle)
# plot(polyrect); plot(polycircle, add = TRUE)
# fortify the polygon for ggplot
droid_body_rect <- fortify(polyrect)
# Central ring (orange)
ring <- coord_circle(centre = c(0, 0), r = 0.4)
ring$y <- echancrure(to_var = ring$y, by_var = ring$x, p = 0.1)
ring$x <- echancrure(to_var = ring$x, by_var = ring$y, p = 0.1)
ring <- bind_rows(
ring %>% mutate(group = (x >= 0) * 1),
coord_circle(centre = c(0, 0), r = 0.55, n = nrow(ring)) %>% mutate(y = -y, group = (x >= 0) * 1)
) %>%
filter(group == 1) # oups something went wrong
ring <- bind_rows(ring, ring %>% mutate(x = -x, group = 2))
# ring left and right
# we make a copy of the right part of the central ring
ring_left <- ring %>% filter(group == 1)
# and we shift the ring center
ring_left$x <- ring_left$x - 1.3
# the same ...
ring_right <- ring %>% filter(group == 2)
ring_right$x <- ring_right$x + 1.3
# we creta a polygon for the intersection with the droid_body circle
polyring <- create_poly(
"polyring_g" = ring_left[, c("x", "y")],
"polyring_d" = ring_right[, c("x", "y")]
)
polydroid_body <- create_poly("polydroid_body" = droid_body[, c("x", "y")])
# plot(polyring); plot(polydroid_body, add = TRUE)
polyring <- gIntersection(spgeom1 = polyring, spgeom2 = polydroid_body)
fort_ring <- fortify(polyring)
# the horizontal line of the body (in two parts)
ligne_hori <- data_frame(
x = c(-1, range(ring$x), 1),
y = 0,
group = c(1, 1, 2, 2)
)
# droid head --------------------------------------------------------------------
droid_head <- coord_circle(centre = c(0, 1.02), r = 0.52) %>%
filter(y >= 1.02) %>%
mutate(group = 1, fill = "white", col= "black") %>%
bind_rows(
data_frame(
x = c(-0.52, -0.4, 0.4, 0.52),
y = c(1.02, 0.95, 0.95, 1.02),
group = 2, fill = "white", col= "black"
)
)
# Grey bars in droid's head
poly_head_grey <- create_poly(
"poly_head_grey_haut" = data_frame(
x = c(-0.52, 0.52, 0.52, -0.52),
y = c(1.44, 1.44, 1.51, 1.51)
),
"poly_head_grey_bas" = data_frame(
x = c(-0.52, 0.52, 0.52, -0.52),
y = c(1.02, 1.02, 1.07, 1.07)
)
)
polydroid_head <- create_poly("polydroid_head" = droid_head[droid_head$group == 1, c("x", "y")])
poly_head_grey <- gIntersection(spgeom1 = poly_head_grey, spgeom2 = polydroid_head)
fort_droid_headrectgris <- fortify(poly_head_grey)
# orange bars
poly_head_orange <- create_poly(
"poly_head_orange1" = data_frame(
x = c(-0.52, 0.52, 0.52, -0.52),
y = c(1.38, 1.38, 1.42, 1.42)
),
"poly_head_orange2" = data_frame(
x = c(-0.35, -0.35, -0.2, -0.2),
y = c(1.07, 1.15, 1.15, 1.07)
),
"poly_head_orange3" = data_frame(
x = c(-0.55, -0.55, -0.45, -0.45),
y = c(1.07, 1.15, 1.15, 1.07)
),
"poly_head_orange4" = data_frame(
x = c(0.44, 0.44, 0.47, 0.47),
y = c(1.07, 1.15, 1.15, 1.07)
)
)
poly_head_orange <- gIntersection(spgeom1 = poly_head_orange, spgeom2 = polydroid_head)
fort_droid_headrectorange <- fortify(poly_head_orange)
polygones_droid_head <- bind_rows(
fort_droid_headrectgris %>% select(-piece) %>%
mutate(group = as.numeric(as.character(group)), fill = "#8E8E9C", col= "black"),
fort_droid_headrectorange %>% select(-piece) %>%
mutate(group = as.numeric(as.character(group)) * 2, fill = "#DF8D5D", col= "black")
)
# Eyes
droid_eyes <- bind_rows(
coord_circle(centre = c(0, 1.35), r = 0.14) %>% mutate(group = 1, fill = "white", col = "white"),
coord_circle(centre = c(0, 1.35), r = 0.12) %>% mutate(group = 2, fill = "white", col = "black"),
coord_circle(centre = c(0, 1.35), r = 0.10) %>% mutate(group = 3, fill = "grey40", col = "grey40"),
coord_circle(centre = c(0, 1.35), r = 0.08) %>% mutate(group = 4, fill = "black", col = "black"),
coord_circle(centre = c(0, 1.16), r = 0.04) %>% mutate(group = 5, fill = "#76B1DE", col = "black"),
coord_circle(centre = c(0.25, 1.20), r = 0.08) %>% mutate(group = 6, fill = "black", col = "black"),
coord_circle(centre = c(0.25, 1.20), r = 0.07) %>% mutate(group = 7, fill = "white", col = "black"),
coord_circle(centre = c(0.25, 1.20), r = 0.06) %>% mutate(group = 8, fill = "grey40", col = "grey40"),
coord_circle(centre = c(0.25, 1.20), r = 0.04) %>% mutate(group = 9, fill = "black", col = "black")
)
eye_line <- data_frame(
x = 0,
y = c(1.07, 1.16-0.04)
)
# Antennas
antennas <- data_frame(
x = c(0.01, 0.01, 0.10, 0.10),
y = c(sqrt(0.52^2 - 0.01^2) + 1.02, sqrt(0.52^2 - 0.01^2) + 1.02 + 0.15,
sqrt(0.52^2 - 0.1^2) + 1.02, sqrt(0.52^2 - 0.1^2) + 1.02 + 0.25),
group = c(1, 1, 2, 2)
)
# bb-8/ggplot2 ------------------------------------------------------------
bb8 <- ggplot(data = droid_body) +
coord_fixed() +
geom_polygon(mapping = aes(x = x, y = y), fill = "white", col = "black") +
geom_polygon(data = droid_body_rect, mapping = aes(x = long, y = lat, group = group), fill = "#8E8E9C") +
geom_path(mapping = aes(x = xvert, y = yvert)) +
geom_path(mapping = aes(x = xint, y = yint)) +
geom_polygon(data = ring, mapping = aes(x = x, y = y, group = group), fill = "#DF8D5D", col = "#DF8D5D") +
geom_path(data = ligne_hori, mapping = aes(x = x, y = y, group = group)) +
geom_polygon(data = fort_ring , mapping = aes(x = long, y = lat, group = group), fill = "#DF8D5D") +
geom_polygon(data = droid_head, mapping = aes(x = x, y = y, group = group, fill = fill, col = col)) +
geom_polygon(data = polygones_droid_head, mapping = aes(x = long, y = lat, group = group, fill = fill, col = col)) +
geom_polygon(data = droid_eyes, mapping = aes(x = x, y = y, group = group, fill = fill, col = col)) +
scale_fill_identity() + scale_color_identity() +
geom_line(data = eye_line, mapping = aes(x = x, y = y)) +
geom_line(data = antennas, mapping = aes(x = x, y = y, group = group), col = "black")
bb8
# or
bb8 + theme_void()
# save --------------------------------------------------------------------
#ggsave(filename = "#144_bb8_ggplot2.png", plot = bb8, width = 6, height = 8)
#ggsave(filename = "#144_bb8_ggplot2_void.png", plot = bb8 + theme_void(), width = 6, height = 8)