-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffon_needle.r
182 lines (106 loc) · 3.3 KB
/
buffon_needle.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
# Code1 with tidyverse package:
#####################################
library(tidyverse)
Buffon_needle <- function(n = 25, a = 1.5, L = 1.5, seed = 1234){
Yintercept <- seq(-4, 4, by = L)
P <- ggplot() +
geom_hline(yintercept = Yintercept) +
labs(y = NULL) +
coord_cartesian(xlim = c(0, 8), ylim = c(-4, 4), expand = TRUE)+
theme(
panel.border = element_blank(),
axis.text = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
panel.background = element_rect(fill = "white")
)
P
set.seed(seed)
x1 <- runif(n, 1, 7)
y1 <- runif(n, -3.5, 3.5)
theta1 <- runif(n, 0, pi)
x1_2 <- x1 + a * cos(theta1)
y1_2 <- y1 + a * sin(theta1)
X <- numeric(2*n)
Y <- numeric(2*n)
ind1 <- seq(1, 2*n, by = 2)
ind2 <- seq(2, 2*n, by = 2)
X[ind1] <- x1
X[ind2] <- x1_2
Y[ind1] <- y1
Y[ind2] <- y1_2
Meanx <- (x1 + x1_2)/2
Meany <- (y1 + y1_2)/2
yy <- Yintercept
nn <- length(Yintercept)
Res <- expand_grid(Meany, yy)
ID <- rep(paste0("needle", 1:n), each = nn)
Res$ID = factor(ID, levels = unique(ID), ordered = TRUE)
Res %>%
mutate(Distance = sqrt((Meany - yy)^2)) %>%
group_by(ID) %>%
summarise(MIN = min(Distance)) -> DISTANCE
DISTANCE
needle_dist <- a * sin(theta1)/2
DISTANCE <- DISTANCE %>%
mutate(HIT = 1 * (MIN < needle_dist))
IND_hit <- which(DISTANCE$HIT == 1)
n_hit <- length(IND_hit)
df2 <- tibble(x = Meanx[IND_hit], y = Meany[IND_hit])
Df <- tibble(x = X, y = Y, Time = rep(1:n, each = 2))
P + geom_line(data = Df, aes(x = x, y = y, group = Time),
size = 1.2, color = "darkblue", alpha = .7) +
geom_point(data = df2, aes(x = x, y = y), shape = 4,
color = "orange", stroke = 3, size = 3) +
labs(title = paste0("# Hits = ", n_hit), x = NULL) -> P1
myresult <- list(number_hits = n_hit, Plot = P1)
return(myresult)
}
outPut <- Buffon_needle()
P1 <- outPut$Plot
P1
## if you want show this plot as a giff then run these codes below
#library(gganimate)
#P1 + transition_reveal(Time)
# code2: with base functions in R
Buffon_updated <- function(n = 20, a = 1.5, L = 2,
show = TRUE, seed = 12){
if(a > L) stop("a must be smaller than L")
Yintercept <- seq(-4, 4, by = L)
set.seed(seed)
x1 <- runif(n, 1, 7)
y1 <- runif(n, -3.5, 3.5)
theta1 <- runif(n, 0, pi)
x1_2 <- x1 + a * cos(theta1)
y1_2 <- y1 + a * sin(theta1)
Meanx <- (x1 + x1_2)/2
Meany <- (y1 + y1_2)/2
nn <- length(Yintercept)
Res <- expand.grid(Yintercept, Meany)
names(Res) <- c("Meany", "y")
ID <- rep(paste0("needle", 1:n), each = nn)
Res$ID = factor(ID, levels = unique(ID), ordered = TRUE)
Distance = sqrt((Res$Meany - Res$y)^2)
Res$distance <- Distance
result <- aggregate(Distance ~ ID, data = Res, FUN = min)
needle_dist <- a * sin(theta1)/2
Hit <- 1 * (result$Distance < needle_dist)
IND_hit <- which(Hit == 1)
n_hit <- length(IND_hit)
df2 <- data.frame(x = Meanx[IND_hit], y = Meany[IND_hit])
if(show){
plot(x = c(0, 8), y = c(-4, 4), type = "n", frame = FALSE,
xaxt = "n", yaxt = "n", xlab = "", ylab = "",
main = paste0("# Hits = ", n_hit))
abline(h = Yintercept)
for(i in 1:n){
xx <- c(x1[i], x1_2[i])
yy <- c(y1[i], y1_2[i])
lines(x = xx, y = yy, col = "blue", lwd = 2)
}
points(x = df2$x, y = df2$y, pch = 4,
col = "tomato", lwd = 3)
}
return(n_hit)
}
Buffon_updated(L = 1, a = 1, seed = 2, n = 20)