简体   繁体   English

如何检查一个数组中的值是否相等而不检查同一件事

[英]How can I check if values in one array are equal are equal without checking the same thing

I have this array 我有这个数组

Ball[] balls = new Ball[7]; // 7 just being an example

In my Ball class, I have getters and setters for x and y values. 在我的Ball课程中,我有x和y值的吸气剂和吸气剂。 I'm trying to compare the x and y values to make sure that they don't intersect. 我正在尝试比较x和y值,以确保它们不相交。

My first thought was to make a loop looking like 我首先想到的是使循环看起来像

for(Ball b1 : balls) {
    for(Ball b2 : balls) {
    if(b1.intersects(b1, b2)) {. . .} // I made intersects, not my issue 
    }
}

But this is no good, as it compares: 但这并不好,因为与之相比:

  1. balls 0 to balls 0 球0到球0
  2. balls 1 to balls 1 球1到球1
  3. etc. 等等

     for(int i = 0; i < balls.length; i++) { System.out.println(f.getContentPane().getWidth() + "\\n" + f.getContentPane().getHeight()); int radius = 10 + rand.nextInt(20); balls[i] = new Ball(360, radius, rand.nextInt(f.getContentPane().getWidth() - 4 * radius - 5) + radius + 5, rand.nextInt(f.getContentPane().getHeight() - 4 * radius - 5) + radius + 5 ); } for(Ball b1 : balls) { for (Ball b2 : balls) { while (b1.intersects(b1, b2)) { System.out.println("Ball started out inside of another, replacing now."); b1.setX(rand.nextInt(f.getContentPane().getWidth() - 2 * b1.getRadius() - 5) + b1.getRadius() + 5); b1.setY(rand.nextInt(f.getContentPane().getHeight() - 2 * b1.getRadius() - 5) + b1.getRadius() + 5); } } } 

////////////// class change ////////////////// ////////////////类更改///////////////////

class Ball {
private int direction;
private int radius;
private int x,y;

Ball(int direction, int radius, int x, int y) {
    this.direction = direction;
    this.radius = radius;
    this.x = x;
    this.y = y;
}

// Getters + Setters here

boolean intersects(Ball b1, Ball b2) {
    double x = Math.pow((b2.getX() - b1.getX()), 2);    // Distance formula
    double y = Math.pow((b2.getY() - b1.getY()), 2);    // Distance formula
    double r = b1.getRadius() + b2.getRadius();

    //System.out.println(x + " + " + y + " <= " + r );
    return x + y <= r;
}

} }

(Ignore that I didn't put my first hunk of code in a method and class, I've done that in my actual code.) (忽略我没有将我的第一批代码放在方法和类中,而是在我的实际代码中完成了。)

I, for whatever reason, can't think of a way to do this without a whole lot of if statements 无论出于何种原因,如果没有大量的if语句,我想不出办法

(So I'm asking for the best way to do this) (所以我正在寻求最好的方法来做到这一点)

One way to compare every distinct (ie, no ball with itself) pair of Ball s, without comparing any pair more than once would be: 比较每对不同的(即,没有球本身)对Ball而不比较任何一对以上的一种方法是:

for (int i = 0; i < balls.length; ++i) {
    Ball b1 = balls[i];
    for (int j = i+1; j < balls.length; ++j) {
        Ball b2 = balls[j];
        if (b1.intersects(b1, b2)) {
            // ...
        }
    }
}

Detecting new collisions introduced in the process of resolving previous ones just means making multiple passes over balls until you no longer have any collisions. 检测在解决以前的碰撞过程中引入的新碰撞仅意味着多次越过balls直到不再有任何碰撞为止。 A simple, perhaps naive, way of doing this would be something like this: 一个简单的,也许是幼稚的方法是这样的:

boolean foundCollision;
int numTries = 0;
int maxTries = 1000000;
do {
    foundCollision = false;
    for (int i = 0; i < balls.length; ++i) {
        Ball b1 = balls[i];
        for (int j = i+1; j < balls.length; ++j) {
           Ball b2 = balls[j];
           if (b1.intersects(b1, b2)) {
               foundCollision = true;
               // resolve collision...
        }
    }
    ++numTries;
} while (foundCollision && numTries < maxTries);
if (numTries >= maxTries)
    System.err.println("Couldn't sort out balls after " + maxTries + "tries: what now?");

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

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