在圆柱体中生成随机点 [英] Generating random point in a cylinder
问题描述
如果圆柱体的半径r和高度h给定,那么在圆柱体的体积内产生一个随机3d点[x,y,z]的最佳方法或算法是什么?
s = random.uniform(0,1)
theta = random.uniform(0,2 * pi)
z = random.uniform(0,H)
r = sqrt(s)* R
x = r * cos(theta)
y = r * sin(theta)
z = z#.. for symmetry :-)
简单地取 x = r * cos(角度)
和 y = r * sin(angle)
是那么当r很小时,即在圆的中心,r的微小变化不会改变x和y的位置非常。 IOW,它导致笛卡尔坐标中的非均匀分布,并且这些点集中在圆的中心。以平方根来纠正这一点,至少如果我已经正确地做了我的算术。
[啊,它看起来像sqrt 是正确的。]
(请注意,我假设没有考虑到圆柱体与z轴对齐,圆柱体中心位于(0,0,H / 2)。在圆柱体中心设置(0,0,0)不太随意,在这种情况下z应该选择在-H / 2和H / 2之间,而不是0,H。)
What is best way or an algorithm for generating a random 3d point [x,y,z] inside the volume of the circular cylinder if radius r and height h of the cylinder are given?
解决方案 How about -- in Python pseudocode, letting R be the radius and H be the height:
s = random.uniform(0, 1)
theta = random.uniform(0, 2*pi)
z = random.uniform(0, H)
r = sqrt(s)*R
x = r * cos(theta)
y = r * sin(theta)
z = z # .. for symmetry :-)
The problem with simply taking x = r * cos(angle)
and y = r * sin(angle)
is that then when r is small, i.e. at the centre of the circle, a tiny change in r doesn't change the x and y positions very much. IOW, it leads to a nonuniform distribution in Cartesian coordinates, and the points get concentrated toward the centre of the circle. Taking the square root corrects this, at least if I've done my arithmetic correctly.
[Ah, it looks like the sqrt was right.]
(Note that I assumed without thinking about it that the cylinder is aligned with the z-axis and the cylinder centre is located at (0,0,H/2). It'd be less arbitrary to set (0,0,0) at the cylinder centre, in which case z should be chosen to be between -H/2 and H/2, not 0,H.)
这篇关于在圆柱体中生成随机点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!