1

I have a function that creates a uniform random point inside a sphere of a specific radius:

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`

However, I'm trying to figure out how to get the point that gets generated to be within a sphere that's around a specific point in space rather than where it's being generated at currently around 0,0,0. I'm not math savvy at all so I'm not sure how to do this. There are plenty of examples of how to generate a random point in a sphere of a specific radius (ie Generate a random point within a circle (uniformly), but I haven't seen any in python that state how to do so inside a radius that's around a user specified point (or perhaps I'm just misunderstanding the math that gets used...).

There was one question that got asked/answered here (generate a random cluster of points around a given point python) but this didn't really help, there were some other somewhat similar questions but they were in either Java or C# (ie Randomly generate clustered points given a center coordinate in 3D).

I put in a simple drawing of what I have right now (on the left) and what I'm trying to do (on the right).differentOrigin

Any help or examples would be greatly appreciated!!

ddog
  • 39
  • 5

1 Answers1

2

You're really overthinking this quite a bit. It's simpler to demonstrate in 2D, and the logic is the same:

enter image description here

In the figure above, we have a circle centered at (0, 0), and a point located at (0, 1).

Now, let's center the circle at (1, 1) and move the point to the same relative position:

enter image description here

The new center of our circle is (1, 1), and the point is now located at (1, 2). All you need to do to get this transformation is:

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

It really is that simple!

Now numpy has built in functionality to make this even easier, because you can simply add numpy arrays. So if you have your new center, and your initial point, you can calculate the new point like so:

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

This translates easily to 3D surfaces:

enter image description here

Here we have a sphere centered at (0, 0, 0), and a point at (0, 0, 10). We can use the same logic to move this circle to be centered at (5, 5, 5), with the point still in the same relative position:

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

enter image description here

user3483203
  • 50,081
  • 9
  • 65
  • 94
  • Ah, yes this makes sense - I was definitely overthinking this. Thank you for the clear explanation! – ddog Jun 26 '18 at 19:42