碰撞力
🌐 Collide force
碰撞力将节点视为具有给定半径的圆,而不是点,并防止节点重叠。更正式地说,两个节点a和b会被分开,使a和b之间的距离至少为radius(a) + radius(b)。为了减少抖动,这默认是一个“软”约束,具有可配置的强度和迭代次数。
🌐 The collide force treats nodes as circles with a given radius, rather than points, and prevents nodes from overlapping. More formally, two nodes a and b are separated so that the distance between a and b is at least radius(a) + radius(b). To reduce jitter, this is by default a “soft” constraint with a configurable strength and iteration count.
forceCollide(radius)
来源 · 使用指定的半径创建一个新的圆形碰撞力。如果未指定半径,则默认为所有节点使用常量值。
const collide = d3.forceCollide((d) => d.r);碰撞.半径(半径)
🌐 collide.radius(radius)
来源 · 如果指定了 radius,则将半径访问器设置为指定的数字或函数,重新计算每个节点的半径访问器,并返回该力。如果未指定 radius,则返回当前的半径访问器,默认值为:
function radius() {
return 1;
}半径访问器会针对模拟中的每个节点被调用,同时传入node及其从零开始的index。得到的数值随后会被内部存储,因此每个节点的半径仅在力被初始化时或当此方法以新的radius调用时重新计算,而不是在每次施加力时都重新计算。
🌐 The radius accessor is invoked for each node in the simulation, being passed the node and its zero-based index. The resulting number is then stored internally, such that the radius of each node is only recomputed when the force is initialized or when this method is called with a new radius, and not on every application of the force.
collide.strength(strength)
来源 · 如果指定了strength,则将力的强度设置为指定范围 [0,1] 内的数值,并返回此力。如果未指定strength,则返回当前强度,默认值为 1。
重叠节点通过迭代松弛来解决。对于每个节点,首先确定在下一个时刻预期会重叠的其他节点(使用预期位置 ⟨x + vx, y + vy⟩);然后修改该节点的速度,将节点从每个重叠的节点中推出。速度的变化会通过力的强度进行阻尼,从而使同时发生的重叠的处理可以融合在一起,以找到稳定的解决方案。
🌐 Overlapping nodes are resolved through iterative relaxation. For each node, the other nodes that are anticipated to overlap at the next tick (using the anticipated positions ⟨x + vx,y + vy⟩) are determined; the node’s velocity is then modified to push the node out of each overlapping node. The change in velocity is dampened by the force’s strength such that the resolution of simultaneous overlaps can be blended together to find a stable solution.
碰撞.迭代(iterations)
🌐 collide.iterations(iterations)
来源 · 如果指定了 iterations,则将每次应用的迭代次数设置为指定的数字,并返回该力。如果未指定 iterations,则返回当前的迭代次数,默认值为 1。增加迭代次数会大大增加约束的刚性,并避免节点的部分重叠,但也会增加评估该力的运行时间成本。