如何根据距离找到最近的代理?

how to find the nearest agent based on distance?

我试图让当一个代理与任何其他代理之间的距离小于指定距离时,它们会改变颜色。但是当我使用 getNearestAgent(this.getPopulation()) 时,它会在人口列表的位置 0 中给出代理。 我正在使用一个事件让代理改变颜色。 this is the code of the event and the output of the console

并且颜色变化取决于事件的发生率,有时还取决于与点 (0,0,0) 的距离

您可以使用distanceTo()函数。

每个代理都需要遍历所有其他代理,然后检查它们与其他代理的距离。