我在计算圆内点相对于R中圆心的距离时遇到了一些困难。用函数计算出均匀分布的点的距离后(请参见下面的完整示例),基于距离截止的数据。绘制时,结果可能不是一个直觉的均匀环。取而代之的是,结果是一个形状奇怪的对象,几乎“是”一个环,但沿水平轴被挤压。
我可能会在这里遗漏一些确实很明显的东西,但是我无法理解可能是什么。
library(dplyr)
set.seed(111)
# Generate a set of angles from a uniform distribution
theta <- runif(2500,2 * pi)
# Generate a set of radii from a uniform distribution
radius <- sqrt(runif(2500))
# Assemble into a matrix of 2D coordinates
x <- radius * cos(theta)
y <- radius * sin(theta)
z <- cbind(x,y)
par(pty = "s") # square plot region
plot.diameter <- c(-1,1) # plot boundaries
plot(z,pch = 20,cex = 0.20,col = rgb(red = 0,green = 0,blue = 0,alpha = 0.5),xlim = plot.diameter,ylim = plot.diameter)
# Radial distance function
# Computes the distance of all points within the circle to the circle center
# Default center: (0,0)
rad.dist <- function(m,x.center = 0,y.center = 0) {
(m[,1] - x.center) ^ 2 + (m[,2] - y.center) ^ 2 %>%
sqrt() -> d
return(d)
}
z.radial <- rad.dist(z)
# Bind radial distance column to coordinates
z <- cbind(z[,1],z[,2],z.radial)
# Rename columns for convenience
colnames(z) <- c("x","y","rd")
# Subset based on radial distance
outer.ring <- subset(z,3] >= 0.75)
plot(outer.ring,ylim = plot.diameter)