Threejs Particle System with joining lines. Programming logic?

大城市里の小女人 提交于 2019-12-06 11:23:09

The problem you're actually trying to solve is that while looping through your list of points, you're doubling the number of successful matches.

Example:

Consider a list of points, [A, B, C, D]. Your looping tests each point against all other points. For this example, A and C are the only points close enough to be considered nearby.

During the first iteration, A vs. all, you find that A and C are nearby, so you add a line. But when you're doing your iteration for C, you also find that A is nearby. This causes the second line, which you want to avoid.

Fixing it:

The solution is simple: Don't re-visit nodes you already checked. This works because the answer of distance from A to C is no different from distance from C to A.

The best way to do this is adjust your indexing for your check loop:

// (Note: This is example code, and won't "just work.")
for(var check = 0, checkLength = nodes.length; check < checkLength; ++check){
    for(var against = check + 1, against < checkLength; ++against){
        if(nodes[check].distanceTo(nodes[against]) < delta){
            buildThatLine(nodes[check], nodes[against]);
        }
    }
}

In the inner loop, the indexing is set to:

  1. Skip the current node
  2. Skip all nodes before the current node.

This is done by initializing the inner indexing to the outer index + 1.

Caveat:

This particular logic assumes that you discard all your lines for every frame. It's not the most efficient way to achieve the effect, but I'll leave making it more efficient as an exercise for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!