random_point_in_triangle | R Documentation |
Generates random points inside a triangle using Osada et al.'s (2002, Sect. 4.2) method.
random_point_in_triangle(n, X, rfun1 = runif, rfun2 = runif)
rpit(n, X, rfun1 = runif, rfun2 = runif)
n |
an integer giving the number of points to generate. |
X |
a numeric matrix with 3 rows and 2 columns giving the coordinates of the triangle. |
rfun1 |
a function generating random values in [0,1]. By default, the values are generated under a uniform distribution. |
rfun2 |
same as the previous argument (see details). |
By default, the points are uniformly distributed in the triangle.
The Beta
function offers an interesting
alternative to generate points concentrated in a specific part of the
triangle (see examples).
A numeric matrix with n
rows and two columns giving the
coordinates of the points.
Emmanuel Paradis
Osada, R., Funkhouser, T., Chazelle, B., and Dobkin, D. (2002) Shape distributions. ACM Transactions on Graphics, 21, 807–832. <doi:10.1145/571647.571648>
## a random triangle in [0,1]^2:
P <- matrix(runif(6), 3, 2)
## n points uniformly distributed in the triangle P:
n <- 10000
x <- rpit(n, P)
layout(matrix(1:2, 1))
plot(P, type = "n", asp = 1)
polygon(P, col = "yellow", border = NA)
points(x, pch = ".", col = "blue")
## using Beta distributions:
foo <- function(n) rbeta(n, 1, 10)
bar <- function(n) rbeta(n, 1, 1)
y <- rpit(n, P, foo, bar)
plot(P, type = "n", asp = 1)
polygon(P, col = "yellow", border = NA)
points(y, pch = ".", col = "blue")
layout(1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.