【发布时间】:2013-03-05 11:02:23
【问题描述】:
这是我第一次在这里发帖,如果我遗漏了信息或提供了太多信息,或者搞砸了,我深表歉意。如果我这样做了,请告诉我。
所以我有一个圆圈列表,其中许多可能重叠。 我通过以下方式检测重叠(鼻子是我的类,指定圆的大小(半径)和位置):
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。这就是我想要的应用程序(对于那些想要真正重叠的人,最重要的答案将对此有所帮助:) How to detect overlapping circles and fill color accordingly?
鉴于这种“重叠”检测,我试图将列表中的“重叠”圆圈与以下代码结合起来(鼻子是一个 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);
}
我相信这种循环查找相似元素并将它们组合起来的方法应该可以工作,因为以下代码可以正常工作(修改自combine items of list):
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]
但是,我的实际应用程序始终在其上绘制重叠的圆圈。我真的很困惑为什么,因为我已经用简单的整数检查了组合循环,而且我相当确定我的重叠检测甚至不应该有彼此接近的圆圈,并且所以当然不应该有任何重叠。
【问题讨论】:
-
重叠圆圈的条件是错误的。如果两个中心之间的距离小于半径之和,则两个圆重叠。距离由((x1-x2)^2 + (y1-y2)^2)的平方根测量
-
首先,重叠圆圈的条件最终不是问题。没错,它不是检测圆形重叠,而是检测方形重叠,但如前所述,这对于我的应用程序是可以接受的。问题最终在于组合项目列表。当我在我的列表 [第三个代码块] 中添加一个随机的 30,000 个数字时,我发现结果列表 将 的数字彼此相差 10 以内(它不应该)。我发现通过简单地运行该算法 2 或 3 次(在 30k 个数字上,只需要 3 次迭代)会给我一个正确的列表。
-
哦!我发现只进行多次迭代的解决方案感觉就像是 hack。这不能证明是正确的。如果有人能够弄清楚 为什么 一个迭代中仍然存在“重叠”值,并且可能是一个正确的解决方案(在它工作之前不只是诉诸运行迭代),我会接受这样的答案。
标签: java arraylist geometry overlapping