简体   繁体   中英

Randomly generate clustered points given a center coordinate in 3D

I would like to be able to generate a cluster of points in 3D space that would create a majority of the points within a specified sphere radius (in this case, 4) from the starting point (in this case, 0,0,0). I'd also like it to generate the occasional outlier too.

It would be similar to this:

3D点聚类

But I'd like to be able to tweak some of the settings. I will be generating this in C# and I'm familiar with the Random class, and have some familiarity with spheres. I'm not sure how I can piece it all together.

I have this which can generate a point in a sphere, but I want it to cluster around that center point:

// elsewhere in code
private static readonly Random Random = new Random();

private static double NextDouble(double minValue, double maxValue)
{
    double next = Random.NextDouble();

    return minValue + (next * (maxValue - minValue));
}

// generates random points in sphere
const int r = 6;

double x;
double y;
double z;

do
{
    x = NextDouble(-r, 1 + r);
    y = NextDouble(-r, 1 + r);
    z = NextDouble(-r, 1 + r);

} while (Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2) > Math.Pow(r, 2));

I think it is easiest to generate random points in spherical coordinates and transform them to cartesian coordinates.

You could generate the radius from the distribution of your choice. Get the two angles from a uniform distribution; one ranging from -pi to pi, the other from 0 to pi.

You can check this article about Spherical Coordinate System. Martin is correct, you will need

  • a random number from 0 to r set as r
  • a random number from 0 to pi set as theta
  • a random number from 0 to 2*pi set as phi

then to transform them to the Cartesian Coordinate System you have to do the following

  • x = r sin(theta) cos(phi)
  • y = r sin(theta) sin(phi)
  • x = r cos(theta)

What you are doing right now will lead to a point cloud of a cubic shape.

If you need further help with the code please do tell.

Ok, let's start with distribution, which obviously only depends on r So Probability Density Function would be

PDF(x, y, z) = f(x, y, z) dx dy dz

We know f(x,y,z) depends only on r=sqrt(x^2+y^2+z^2) Lets move to spherical coordinates

PDF(r,theta,phi) = f(r) r^2 sin(theta) dr dtheta dphi

Where r^2 sin(theta) are coming from Jacobian It could be easily factorized into

P(r)     = f(r) r^2 dr
P(theta) = sin(theta) dtheta
P(phi)   = dphi

ok, only phi is distribution uniformly so

phi = 2*pi*random()

for theta it is uniform cos(theta) , so

cos(theta) = 2*random()-1

and for r you have to know what f(r) is, but distribution would be affected by r^2 term

P(r) = f(r) r^2 dr

I might suggest using gaussian for f(r) , but you might want another choice. And of course

sin(theta) = sqrt(1-cos(theta)^2)
x = r * sin(theta) * cos(phi)
y = r * sin(theta) * sin(phi)
z = r * cos(theta)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM