多体力
¥Many-body force
多体(或 n 体)力在所有 nodes 之间相互作用。如果 strength 为正,则可用于模拟重力(吸引力);如果 strength 为负,则可用于模拟静电荷(排斥力)。此实现使用四叉树和 Barnes-Hut 近似 来显著提高性能;可以使用 theta 参数自定义精度。
¥The many-body (or n-body) force applies mutually amongst all nodes. It can be used to simulate gravity (attraction) if the strength is positive, or electrostatic charge (repulsion) if the strength is negative. This implementation uses a quadtree and the Barnes–Hut approximation to greatly improve performance; the accuracy can be customized using the theta parameter.
Unlike the 链接力, which only affect two linked nodes, the charge force is global:每个节点都会影响其他所有节点,即使它们位于不相连的子图上。
¥Unlike the link force, which only affect two linked nodes, the charge force is global: every node affects every other node, even if they are on disconnected subgraphs.
forceManyBody()
源代码 · 使用默认参数创建一个新的多体力。
¥Source · Creates a new many-body force with the default parameters.
const manyBody = d3.forceManyBody().strength(-100);
manyBody.strength(strength) {#manyBody_strength}
源代码 · 如果指定了 strength,则将 strength 访问器设置为指定的数字或函数,重新评估每个节点的 strength 访问器,并返回此力。正值使节点相互吸引,类似于重力;负值使节点相互排斥,类似于静电荷。如果未指定强度,则返回当前强度访问器,默认为:
¥Source · If strength is specified, sets the strength accessor to the specified number or function, re-evaluates the strength accessor for each node, and returns this force. A positive value causes nodes to attract each other, similar to gravity, while a negative value causes nodes to repel each other, similar to electrostatic charge. If strength is not specified, returns the current strength accessor, which defaults to:
function strength() {
return -30;
}
在模拟中,每个 node 都会调用强度访问器,并传递节点及其从零开始的索引。然后将结果数字存储在内部,以便仅在初始化力或使用新的力强度调用此方法时重新计算每个节点的强度,而不是每次施加力时都重新计算。
¥The strength 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 strength of each node is only recomputed when the force is initialized or when this method is called with a new strength, and not on every application of the force.
manyBody.theta(theta) {#manyBody_theta}
源代码 · 如果指定了 theta,则将 Barnes-Hut 近似标准设置为指定的数字并返回此 force。如果未指定 theta,则返回当前值,默认为 0.9。
¥Source · If theta is specified, sets the Barnes–Hut approximation criterion to the specified number and returns this force. If theta is not specified, returns the current value, which defaults to 0.9.
为了加速计算,本强制实现了 Barnes-Hut 近似,每个应用的复杂度为 O(n log n),其中 n 是 nodes 的数量。对于每个应用,quadtree 都会存储当前节点位置;然后,对于每个节点,计算所有其他节点作用于给定节点的力的和。对于较远的节点簇,电荷力可以通过将簇视为单个较大的节点来近似计算。theta 参数决定近似值的精度:如果四叉树单元的宽度 w 与节点到单元质心的距离 l 之比 w / l 小于 theta,则给定单元中的所有节点将被视为单个节点,而不是单独处理。
¥To accelerate computation, this force implements the Barnes–Hut approximation which takes O(n log n) per application where n is the number of nodes. For each application, a quadtree stores the current node positions; then for each node, the combined force of all other nodes on the given node is computed. For a cluster of nodes that is far away, the charge force can be approximated by treating the cluster as a single, larger node. The theta parameter determines the accuracy of the approximation: if the ratio w / l of the width w of the quadtree cell to the distance l from the node to the cell’s center of mass is less than theta, all nodes in the given cell are treated as a single node rather than individually.
manyBody.distanceMin(distance) {#manyBody_distanceMin}
源代码 · 如果指定了 distance,则设置节点间最小距离,在此距离内会触发此力。如果未指定距离,则返回当前最小距离,默认为 1。最小距离确定了两个相邻节点之间力的上限,从而避免了不稳定性。特别是,如果两个节点完全重合,它可以避免产生无限强的力;在这种情况下,力的方向是随机的。
¥Source · If distance is specified, sets the minimum distance between nodes over which this force is considered. If distance is not specified, returns the current minimum distance, which defaults to 1. A minimum distance establishes an upper bound on the strength of the force between two nearby nodes, avoiding instability. In particular, it avoids an infinitely-strong force if two nodes are exactly coincident; in this case, the direction of the force is random.
manyBody.distanceMax(distance) {#manyBody_distanceMax}
源代码 · 如果指定了 distance,则设置节点间的最大距离,在此距离内会触发此力。如果未指定距离,则返回当前最大距离,默认为无穷大。指定有限的最大距离可以提高性能并生成更本地化的布局。
¥Source · If distance is specified, sets the maximum distance between nodes over which this force is considered. If distance is not specified, returns the current maximum distance, which defaults to infinity. Specifying a finite maximum distance improves performance and produces a more localized layout.