【问题标题】:Generate a point within a sphere of a certain radius that's around a SPECIFIC given point (not at 0,0,0) - Python在特定给定点(不在 0,0,0 处)周围的特定半径的球体内生成一个点 - Python
【发布时间】:2018-12-04 11:28:21
【问题描述】:

我有一个函数可以在特定半径的球体内创建一个均匀的随机点:

radius = 5
r = radius * ( numpy.random.random()**(1./3.) )  
phi = numpy.random.uniform(0,2*numpy.pi) 
costheta = numpy.random.uniform(-1,1) 
theta = numpy.arccos(costheta) 
x = numpy.sin(theta) * numpy.cos(phi) 
y = numpy.sin(theta) * numpy.sin(phi)
z = numpy.cos(theta)
point = numpy.array([x, y, z]) * r`

但是,我试图弄清楚如何让生成的点位于空间中特定点周围的球体内,而不是当前在 周围生成的位置>0,0,0。我根本不精通数学,所以我不知道该怎么做。 有很多关于如何在特定半径的球体中生成随机点的示例(即Generate a random point within a circle (uniformly),但我在 python 中没有看到任何说明如何在用户指定点周围的半径内执行此操作的示例(或者我只是误解了使用的数学......)。

这里提出/回答了一个问题 (generate a random cluster of points around a given point python),但这并没有真正帮助,还有其他一些类似的问题,但它们是用 Java 或 C# 编写的(即Randomly generate clustered points given a center coordinate in 3D)。

我画了一张我现在拥有的东西(左侧)和我正在尝试做的事情(右侧)的简单图。differentOrigin

任何帮助或示例将不胜感激!

【问题讨论】:

  • 对于非 0,0,0 的任意点,行为是相同的。你只需要一点加法/减法
  • point = numpy.array([x, y, z]) * r + specific_point 其中specific_point = [x_0, y_0, z_0]

标签: python math geometry


【解决方案1】:

你真的想多了。用2D演示更简单,逻辑是一样的:

在上图中,我们有一个以 (0, 0) 为圆心的圆,以及一个位于 (0, 1) 的点。

现在,让我们以 (1, 1) 为圆心并将该点移动到相同的相对位置:

我们圆的新中心是 (1, 1),该点现在位于 (1, 2)。要实现这种转变,您需要做的就是:

1 + 0 = 1      # new_center_x + point_x
1 + 1 = 2      # new_center_y + point_y

真的就这么简单!

现在numpy 已经内置了功能,使这变得更加容易,因为您可以简单地添加 numpy 数组。所以如果你有你的新中心和你的初始点,你可以像这样计算新点:

new_center = np.array([1, 1])
original_point = np.array([0, 1])
new_center + original_point
# array([1, 2])

这很容易转化为 3D 表面:

这里我们有一个以 (0, 0, 0) 为中心的球体和一个在 (0, 0, 10) 处的点。我们可以使用相同的逻辑将这个圆移动到以 (5, 5, 5) 为中心,而该点仍处于相同的相对位置:

new_center = np.array([5, 5, 5])
original_point = np.array([0, 0, 10])
new_center + original_point
# array([ 5,  5, 15])

【讨论】:

  • 啊,是的,这是有道理的——我肯定是想多了。感谢您的清晰解释!
猜你喜欢
  • 2016-07-13
  • 2013-07-30
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多