我正在使用处理,到目前为止,我有一个绘制随机球的草图,并在一定半径范围内连接时画一条线。
for(int i=0;i<=people.size()-1;i++){
Person p = people.get(i);
for(Person pp: people){
if(pp!=p){
if(p.isIntersecting(pp)){
// Draw the line connecting the two here
line(p.loc.x,p.loc.y,pp.loc.x,pp.loc.y);
// Store the other person in this persons "connections" list
if(!p.connections.contains(pp)){ p.connections.add(pp);}
} else {
p.connections.remove(pp);
}
}
}
}
这可以很好地直观地显示哪些组聚集在一起。 但是现在我怎么能将这组连接对象存储到ArrayList中,而它们是否已连接?所以我可以回忆起其他功能。
就像我的意思一样,我可以很容易地在视觉上看到4个人在屏幕上链接。但是,如何告诉计算机它们都已链接在一起?
链接存储在每个对象连接列表中。 但是每个单独的对象如何知道其他对象连接以将它们分组。
所以我可以围绕它们绘制一个“群体blob”,而它们是相互关联的。
我已经尝试了很多东西,但我似乎总是遇到递归/堆栈溢出错误。因为很明显我只是通过递归查看链接它们的所有连接,而且它太多了。
知道如何将连接的行作为Group存储在ArrayList中吗?
答案 0 :(得分:0)
我假设您将“群集”定义为一组&lt;来自该群集中任何其他对象的N个像素。换句话说,群集是由template <typename T>
uint qHash(const std::shared_ptr<T>& ptr, uint seed = 0)
{
return qHash(ptr.get(), seed);
}
列表链接的一组Person
个对象。请注意,这并不是传统计算机科学意义上的“群集”,因此可能会丢掉您的Google搜索。
作为第一个尝试,您可能会考虑将群集组织为connections
ArrayList
个ArrayLists
个对象。我们称之为Person
。每个内部ArrayList<ArrayList<Person>> clusters
代表连接的ArrayList
个对象的“群集”。您还有另外Person
代表您已访问过的ArrayList<Person>
个对象,请将其称为Person
。然后,你要做的事情的基础是:
visited
请注意,我还没有测试过这段代码,但基本知识是:你需要跟踪你已经访问过的人,并跳过它们以避免无限递归。这是你的基本情况。
另请注意,您可能希望研究“真正的”群集算法。谷歌是你的朋友,因为聚类本身就是一个整体领域。
答案 1 :(得分:-1)
看起来你有一个名为Person
的类,所以我会在类中包含ArrayList。不知道你的完整代码,它可能看起来像这样:
class Person {
ArrayList<Person> connected = new ArrayList<Person>();
Person() {
// create as you already are doing
}
void checkConnections() {
connected.clear(); // delete existing connections
for (Person other : persons) {
if (dist(x,y, other.x,other.y) < 100) {
connected.add(other):
}
}
}
}
首先清除ArrayList,然后检查自身与所有其他之间的距离。如果距离足够短(在我的示例中为100px),则将其添加到ArrayList。
根据您的评论进行编辑:
这基本上是关于图形和节点的问题。请参阅this examples作为起点。