给定3D中心坐标的点随机生成 [英] Randomly generate clustered points given a center coordinate in 3D

查看:231
本文介绍了给定3D中心坐标的点随机生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在3D空间中生成一个点簇,该点簇将在从起点(在这种情况下为0,0)的指定球体半径(在这种情况下为4)内创建大多数点,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆