java - 安卓 Canvas 。如何在随机位置设置圆圈?

标签 java android canvas collision-detection

下面的代码生成并将位置 x y 保存到 HashMap 并检查两个圆的碰撞;

        HashMap<Integer, Float> posX = new HashMap<>();
        HashMap<Integer, Float> posY = new HashMap<>();

        int numberOfCircle = 8;

        for(int i=0; i < numberOfCircle; i ++){

           // boolean flag = false;
            while (true){

                float x =random.nextInt(width - raduis/2) + raduis/2f;
                float y =random.nextInt(height - raduis/2) + raduis/2f;

                if(!posX.containsValue(x) && !posY.containsValue(y)){

                    if(i == 0){

                        posX.put(i, x);
                        posY.put(i, y);
                        break;
                    }
                    if(i > 0){

                      double distance = Math.sqrt(((posX.get(i - 1) - x) * (posX.get(i - 1) - x)) + ((posY.get(i - 1) - y) * ( posY.get(i - 1) - y)));

                        if (distance > raduis+raduis) {

                            posX.put(i, x);
                            posY.put(i, y);
                            Log.d(TAG, i + " xPos=" + posX.get(i) + " yPos=" + posY.get(i) + " distance=" + distance);
                            break;
                        }

                        if(numberOfCircle == posX.size()) break;
                    }

                }
            }

        }

此代码仅在圆数 = 2 时才有效;但是当圆数 > 2 时,我就会发生碰撞;如何检查 HashMap 中每个元素当前生成的位置?

例如: x 位置 = {5, 10, 3} y 位置 = {10, 33, 5}

生成位置x=6, y=10; 计算 x=6, y=10 与 Map 中所有位置之间的距离。如果距离<半径+半径生成新位置,而距离>半径+半径;

更新==========================>

我的代码工作方式如下 enter image description here

我想要这样

enter image description here

输出:当前生成位置(X,Y)与先前位置(X,Y)之间的距离相等。我想检查当前生成的 x, y 与 HashMap 中所有添加的位置。

D/DEBUG DATA ===>: 1 xPos=432.0 yPos=411.0 distance=390.6430595825299
D/DEBUG DATA ===>: 2 xPos=316.0 yPos=666.0 distance=280.1446055165082
D/DEBUG DATA ===>: 3 xPos=244.0 yPos=83.0 distance=587.4291446634223
D/DEBUG DATA ===>: 4 xPos=214.0 yPos=551.0 distance=468.96055271205915
D/DEBUG DATA ===>: 5 xPos=76.0 yPos=1011.0 distance=480.2540994098853
D/DEBUG DATA ===>: 6 xPos=289.0 yPos=868.0 distance=256.55019002136794
D/DEBUG DATA ===>: 7 xPos=494.0 yPos=988.0 distance=237.53947040439405

P.s抱歉,英语太差了。

最佳答案

也许更接近这个的东西会起作用?我不完全确定这是否是您想要的或者是否有帮助,但为了清楚起见,我快速重写了。

HashMap<Integer, Integer> posX = new HashMap<>();
HashMap<Integer, Integer> posY = new HashMap<>();

final int circlesToPlace = 8;

for(int i = 0 ; i < circlesToPlace ; i++){

    // boolean flag = false;
     while (true){
         final int x = ThreadLocalRandom.current().nextInt((radius/2f), width + 1);
         final int y = ThreadLocalRandom.current().nextInt((radius/2f), height + 1);

        // Iterate over all other positions to ensure no circle intersects with
        // the new circle.
        for (int index = 0 ; index < posX.size() ; index++) {
            // Calculate distance where d = sqrt((x2 - x1)^2 + (y2 - y1)^2)
            final int otherX = posX.get(index);
            final int otherY = posY.get(index);

            int differenceX = otherX - x;
            differenceX *= differenceX;

            int differenceY = otherY - y;
            differenceY *= differenceY;

            final double distance = Math.sqrt(differenceX + differenceY);


            if (distance > (radius * 2)) {
                posX.put(i, x);
                posY.put(i, y);
                Log.d(TAG, i + " xPos=" + posX.get(i) + " yPos=" + posY.get(i) + " distance=" + distance);
                break;
            }
        }
     }
 }

关于java - 安卓 Canvas 。如何在随机位置设置圆圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41306917/

相关文章:

java - 无法在方法内实例化类?

javascript - 使用自定义文件名保存 Canvas

javascript - canvas.toDataURL() 下载大小限制

java - 如何使用 software.amazon.awssdk.http.service.impl 解决此运行时错误 "Multiple HTTP implementations were found on the classpath"

java - xmlbeans 上的 Getter 生成的类返回 null,它不应该

java - 如何获取 View 的宽度和高度

android - 值得购买 Google Android Dev 手机吗?

javascript - Canvas /JavaScript : How to adjust position of slices in donutgraph using canvas?

Apache Spark 中的 java 要求

java - setLocation 的 Geofire 错误