给定 3D 中的中心坐标随机生成聚类点 [英] Randomly generate clustered points given a center coordinate in 3D
问题描述
我希望能够在 3D 空间中生成一组点,这些点将从起点(在本例中为 0,0,0).我也希望它偶尔生成异常值.
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.
类似于:
但我希望能够调整一些设置.我将在 C# 中生成它,我熟悉 Random 类,并且对球体有一些熟悉.我不确定如何将它们拼凑在一起.
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));
推荐答案
您可以查看这篇文章 关于球坐标系.马丁是对的,你需要
You can check this article about Spherical Coordinate System. Martin is correct, you will need
- 一个从 0 到 r 的随机数设置为 r
- 一个从 0 到 pi 设置为 theta 的随机数
- 一个从 0 到 2*pi 的随机数,设置为 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.
这篇关于给定 3D 中的中心坐标随机生成聚类点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!