0

こちらに投稿するのは初めてなので、情報を省略したり、提供しすぎたり、その他の方法で混乱したりした場合は申し訳ありません. もしそうなら教えてください。

そのため、円のリストがあり、その多くは重複する可能性があります。私はオーバーラップを検出しています(ノーズは円のサイズ(半径)と位置を指定する私のクラスです):

public boolean isSame(Nose other)
{
    // if the difference between their X and Y positions are both
    // less than half the combined sizes...
    if (Math.abs(other.x - x) < (other.size + size) &&
        Math.abs(other.y - y) < (other.size + size) )
        return true;
    else
        return false;
}

円が近接しているが、必ずしも重なっているとは限らない場合、これは true を返すはずです。これが私のアプリケーションに必要なものです(真のオーバーラップが必要な場合は、これの一番の答えが役に立ちます:) オーバーラップする円を検出し、それに応じて色を塗りつぶす方法は?

この「重複」検出を考慮して、リスト内の「重複」円を次のコードと組み合わせようとしています (noses は ListArray です)。

for (int i = 0; i < noses.size(); i++)
{
    //for each nose after the current one, check for redundant noses
    for (int j = i+1; j < noses.size(); j++)
    {
        Nose noseI = noses.get(i);
        Nose noseJ = noses.get(j);
        if (noseI.isSame(noseJ))
        {
            noses.set(i, new Nose((noseI.x + noseJ.x),
                                  (noseI.y + noseJ.y),
                                  (noseI.size + noseJ.size)));
            noses.remove(j);
            j--; // decrement back to check the nose that is NOW at that place.
        }
    }
    Nose noseI = noses.get(i).getBig();
    //Add noses[i] to the image being returned
    canvas.drawCircle((float)(noseI.x), (float)(noseI.y), (float)(noseI.size), paint);
}

次のコードが正しく機能するため (リストのアイテムを結合から変更) 、ループして類似要素を見つけてそれらを結合するこの方法は機能するはずだと思います。

public static void main(String[] args) {
  List<Integer> list = new LinkedList<>();
  list.add(10);
  list.add(80);
  list.add(5);
  list.add(30);
  list.add(13);
  list.add(18);
  list.add(36);
  System.out.println(list);

  for (int i = 0; i < list.size(); i++)
  {
     for (int j = i + 1; j < list.size(); j++)
     {
        if (Math.abs(list.get(i) - list.get(j)) < 10)
        {
           Integer a = list.get(i);
           Integer b = list.get(j);
           list.set(i, (a + b) / 2);
           list.remove(j);
           j--;
        }
     }
  }

  System.out.println(list);
}

次の出力が得られます。

[10, 80, 5, 30, 13, 18, 36]
[14, 80, 33]

ただし、実際のアプリケーションでは一貫して重複する円が描かれています。組み合わせループを単純な整数でチェックしたので、その理由について本当に困惑しています。重複検出には、互いに近くにある円があってはならないので、重複がないはずです。 .

4

1 に答える 1

0

オーバーラップする条件は、次のように計算できます。

public boolean isSame(Nose other) {
   double dx       = other.x - x;
   double dy       = other.y - y;
   double distance = Math.sqrt( dx*dx + dy*dy );
   return distance < Math.max( size, other.size );
}

命名は重要isSameではありませんoverlaps

于 2013-03-16T20:45:09.883 に答える