Learn R Programming

SIMplyBee (version 0.4.1)

rcircle: Sample random points within a circle

Description

Level 0 function that samples random points (x, y) within a circle via rejection sampling.

Usage

rcircle(n = 1, radius = 1, uniform = TRUE, normScale = 1/3)

Value

matrix with two columns for the x and y coordinates of the points.

Arguments

n

integer, number of samples points

radius

numeric, radius of the sampled circle

uniform

logical, should sampling be uniform or according to a bi-variate spherical (uncorrelated) Gaussian distribution (see examples)

normScale

numeric, if uniform = FALSE, a factor to scale radius to standard deviation of the Gaussian density in x and in y (see examples)

References

nubDotDev (2021) The BEST Way to Find a Random Point in a Circle https://youtu.be/4y_nmpv-9lI

Wolfram MathWorld (2023) Disk Point Picking https://mathworld.wolfram.com/DiskPointPicking.html

Examples

Run this code
x <- rcircle(n = 500)
lim <- range(x)
plot(x, xlim = lim, ylim = lim, main = "Uniform")

x <- rcircle(n = 500, uniform = FALSE)
lim <- range(x)
plot(x, xlim = lim, ylim = lim, main = "Gaussian")

Run the code above in your browser using DataLab