简体   繁体   English

Java 生成不重复的“Vector3”

[英]Java generate non-repeating “Vector3”

I am trying to generate a list of a hundred thousand random points in 3d space within a 3d boundary without having any of the points occupy the same position.我正在尝试在 3d 边界内的 3d 空间中生成十万个随机点的列表,而没有任何点占据相同的 position。 I'm literally trying to create a non-repeating Vector3 generator.我实际上是在尝试创建一个不重复的 Vector3 生成器。 Is there any efficient way of doing this?有没有有效的方法来做到这一点? Also, it is okay if these points are not evenly distributed, it is actually preferable if they are someone clustered here there, just as long as they do not occupy the same position.另外,如果这些点分布不均匀也没关系,实际上最好是有人聚集在这里,只要它们不占用相同的position。

To clarify I am not trying to generate 300,000 unique points.澄清一下,我并不是要生成 300,000 个独特点。 But instead 100,000 3d points.而是 100,000 3d 点。 So a vector values of (0, 0, 0) and (0, 0, 1) is acceptable.所以 (0, 0, 0) 和 (0, 0, 1) 的向量值是可以接受的。 But (4, 4, 4) and (4, 4, 4) is unacceptable.但是 (4, 4, 4) 和 (4, 4, 4) 是不可接受的。

public class Vector3
{
    public float x;
    public float y;
    public float z;

    Vector3(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public static ArrayList<Vector3> generateVector3s()
    {
        ArrayList<Vector3> tempVector3List = new ArrayList<>();
        
        for (int i = 0; i < 100000; i++)
        {   
            tempVector3List.add(new Vector3(RANDOMVALUE, RANDOMVALUE, RANDOMVALUE));
        }
        
        return tempVector3List ;
    }
}

First, generation of Random Numbers in Java:一、Java中随机数的生成:

Geeks for Geeks on Random Numbers in Java Java 中的随机数极客极客

What I might do is generate a value then use the triplet as a key for a hashset.我可能会生成一个值,然后将三元组用作哈希集的键。 If it exists, the value is already there, and toss it and try again.如果存在,则该值已经存在,折腾再试一次。 This will ensure uniqueness and should be relatively efficient.这将确保唯一性,并且应该相对有效。

If you want your datapoints spread equally about your problem space, you might need a fancier algorithm.如果您希望数据点在您的问题空间中平均分布,您可能需要一个更高级的算法。

It sounds like you are looking for a 3D version of Poisson disc sampling, which guarantees points will not cluster in the same areas.听起来您正在寻找泊松圆盘采样的 3D 版本,它保证点不会聚集在同一区域。

The algorithm is fairly complicated so I won't describe it here fully, but the basic idea is that you choose a seed point and grow outwards by always adding points that are at least a given distance away from the existing points.该算法相当复杂,所以我不会在这里完整地描述它,但基本思想是您选择一个种子点并通过始终添加距现有点至少给定距离的点来向外增长。

You can find detailed descriptions and implementations here:你可以在这里找到详细的描述和实现:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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