java - 想要生成随机坐标集(Java/Swing)

标签 java

我有一个像这样的 Java 程序 ( https://www3.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html )。它是继示例 2 之后的面向对象的版本,但有一些细微的变化。

我想为球生成生成随机坐标。但在产卵时不允许它们相互交叉。生成的坐标是围绕圆的矩形的左上角。

因此坐标需要的最小距离为 2 * ballRadius。

我只得到了距离为 2 * ballRadius 的以太坐标,但是 x 和 y 只有唯一的坐标。所以我每个可用的 y 坐标只有一个球。 Example红色圆圈处可能有一个球,但左边的那个球“阻挡”了 y 坐标。

我得到的所有其他坐标都彼此相交。

这是我到目前为止的代码。

    int uniqueXY[][] = new int[ballCount][2];
    for (int i = 0; i < ballCount; i++) {
        int tempx = 0;
        int tempy = 0;
        Boolean foundX = true;
        Boolean foundY = true;
        while(foundX && foundY) {
            tempx = (int) (Math.random() * field.maxX); // generate random number in range of filed
            tempy = (int) (Math.random() * field.maxY);

            for (int j = 0; j < ballCount; j++) { // Here it should check if the number is within the given rules
                if ((uniqueXY[j][0] - (2 * ballRadius) > tempx) || (uniqueXY[j][0] + (2 * ballRadius) < tempx)) {   
                    foundX = false;
                } else {
                    foundX = true;
                    if ((uniqueXY[j][1] - (2 * ballRadius) > tempy) || (uniqueXY[j][1] + (2 * ballRadius) < tempy)) {
                        foundY = false;
                        foundX = false;
                        break;
                    } else {
                        foundY = true;
                        break;
                    }
                }
            }

        }
        uniqueXY[i][0] = tempx;
        uniqueXY[i][1] = tempy;

最佳答案

所以我想出了一些具有类似问题的新代码。

它计算每个设定坐标与临时坐标之间的距离。在大多数情况下,它工作得很好,但如果我强调代码并强制使用大量球,它就会表现得很尴尬。 Example with 400 balls只有边界处的球才会被迫聚集在一起。

int uniqueXY[][] = new int[ballCount][2];


    for (int k = 0; k < ballCount; k++) {
        Boolean found = true;
        while(found) {

            int tempX = (int) (Math.random() * field.maxX); //create rnd x/y in range of field
            int tempY = (int) (Math.random() * field.maxY);

            if (k == 0) {       // first case gets set, because nothing to compare
                uniqueXY[k][0] = tempX;
                uniqueXY[k][1] = tempY;
                found = false;
                break;
            }
            for (int j = 0; j < k; j++) {   //calculates distance between every set coordinate and the temp
                int erg1 = (int) (Math.pow(uniqueXY[j][0] - tempX, 2));
                int erg2 = (int) (Math.pow(uniqueXY[j][1] - tempY, 2));
                int distance = (int) Math.sqrt(erg1 + erg2);
                if (distance < 60) { // if distance between coordinates < 60 temp gets discarded
                    found = true;
                    break;
                }
                if (j == k - 1) { // if every set case is checked and distance was always fine, temp gets set
                    uniqueXY[k][0] = tempX;
                    uniqueXY[k][1] = tempY;
                    found = false;
                }
            }   
        }
    }

关于java - 想要生成随机坐标集(Java/Swing),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61793827/

相关文章:

java - Endeca 导航引擎无法处理请求

java - 为什么泛型类型和通配符类型输出结果不同

java - Libgdx 使用 uiskin.json 创建一个带有背景的 TextButton

java - Java 中欧拉项目#14 的运行时间太长

java - 如何使用 java 将文件移动到另一个文件夹?

java - Get() 模式以避免 nullPointerException

java - 在 Java 中,在哪里保存正在扩展为另一个类的类?

Java - 注册所有用@MyAnnotation 注释的类

java - 自定义对象映射器 bean 更改打开 Feign 客户端/自动配置的 objectMapper bean 的默认属性

java - 如果 Object 是可变的并且不会在排序的 Set 等中使用,compareTo 和 equals 是否应该一致