def contract_positions(XY, edges, stepsize):
"""Perturb vertex positions by an L1-minimizing attractive force.
This is used to slightly adjust vertex positions to provide a visual
hint to their grouping.
Args:
XY: A [V, 2]-shaped numpy array of the current positions.
edges: An [E, 2]-shaped numpy array of edges as (vertex,vertex) pairs.
"""
E = edges.shape[0]
V = E + 1
assert edges.shape == (E, 2)
assert XY.shape == (V, 2)
old = XY
new = old.copy()
heads = edges[:, 0]
tails = edges[:, 1]
diff = old[heads] - old[tails]
distances = (diff**2).sum(axis=1)**0.5
spacing = distances.min()
assert spacing > 0
diff /= distances[:, np.newaxis]
diff *= spacing
new[tails] += stepsize * diff
new[heads] -= stepsize * diff
return new
评论列表
文章目录