碰撞力
¥Collide force
碰撞力将节点视为具有给定 radius 的圆,而不是点,并防止节点重叠。更正式的说法是,两个节点 a 和 b 彼此分离,使得 a 和 b 之间的距离至少为 radius(a) + radius(b)。为了减少抖动,默认情况下这是一个“软”约束,具有可配置的 strength 和 迭代计数。
¥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)
源代码 · 使用指定的 radius 创建一个新的圆形碰撞力。如果未指定半径,则默认为所有节点的常量值。
¥Source · Creates a new circle collide force with the specified radius. If radius is not specified, it defaults to the constant one for all nodes.
const collide = d3.forceCollide((d) => d.r);
collide.radius(radius) {#collide_radius}
源代码 · 如果指定了半径,则将半径访问器设置为指定的数字或函数,重新计算每个节点的半径访问器,并返回此力。如果未指定半径,则返回当前半径访问器,默认为:
¥Source · If radius is specified, sets the radius accessor to the specified number or function, re-evaluates the radius accessor for each node, and returns this force. If radius is not specified, returns the current radius accessor, which defaults to:
function radius() {
return 1;
}
在模拟过程中,每个 node 都会调用半径访问器,并传递节点及其从零开始的索引。生成的数字随后存储在内部,这样,只有在初始化力或以新的半径调用此方法时,才会重新计算每个节点的半径,而不是每次施加力时都重新计算。
¥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) {#collide_strength}
源代码 · 如果指定了 strength,则将力的强度设置为 [0,1] 范围内的指定数字,并返回该力。如果未指定强度,则返回当前强度,默认为 1。
¥Source · If strength is specified, sets the force strength to the specified number in the range [0,1] and returns this force. If strength is not specified, returns the current strength which defaults to 1.
重叠节点通过迭代松弛解决。对于每个节点,确定预计在下一个 tick 重叠的其他节点(使用预期位置 ⟨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.
collide.iterations(iterations) {#collide_iterations}
源代码 · 如果指定了 iterations,则将每次应用的迭代次数设置为指定次数并返回此力。如果未指定迭代次数,则返回当前迭代次数,默认为 1。增加迭代次数可以大大提高约束的刚性,避免节点部分重叠,但也会增加评估力的运行时成本。
¥Source · If iterations is specified, sets the number of iterations per application to the specified number and returns this force. If iterations is not specified, returns the current iteration count which defaults to 1. Increasing the number of iterations greatly increases the rigidity of the constraint and avoids partial overlap of nodes, but also increases the runtime cost to evaluate the force.