我有一个Webcola&D3 svg图,其中有节点和它们之间的链接。直到今天,节点之间的链接可以是单向的,如果B连接到A,它只是单向链接。

今天我被告知我需要支持双向链接,这意味着A可以发送链接到B,B可以发送链接到A。
现在我被困在数学和如何完成它,我使用了一些我找到的算法,直到今天,我想从节点的中心画出链接,我需要并行地显示两个链接,如下所示:

下面是计算链接位置的算法:
let parent = connection.parent;
const sx = parent.source.x;
const sy = parent.source.y;
const tx = parent.target.x;
const ty = parent.target.y;
let angle = Math.atan2(ty - sy, tx - sx);
const radiusSource = parent.source.radius;
const radiusTarget = parent.target.radius;
let x1 = sx + Math.cos(angle) * radiusSource;
let x2 = tx - Math.cos(angle) * radiusTarget;
let y1 = sy + Math.sin(angle) * radiusSource;
let y2 = ty - Math.sin(angle) * radiusTarget;
angle = angle * 180 / Math.PI;
let opposite = Math.abs(angle) > 90;
if (opposite)
angle -= 180;
connection.coords = [x1, y1, x2, y2, angle, opposite];
return connection.coords;这是一个函数的一部分,其结果进入路径的'd‘attr,如下所示:
.attr('d', `M${x1} ${y1} L ${x2} ${y2}`)现在双向链接的结果是它们相互覆盖,谁能帮我改进这个算法,使双向链接并行吗?
发布于 2017-09-12 08:55:45
Update:链接的位置需要由考虑偏移量的新弧度计算,如:
let parent = connection.parent;
const sx = parent.source.x;
const sy = parent.source.y;
const tx = parent.target.x;
const ty = parent.target.y;
const radiusSource = parent.source.radius;
const radiusTarget = parent.target.radius;
let radian = Math.atan2(ty - sy, tx - sx);
let offset = 0.1 // Offset ratio of radian, can be adjusted
let offsetRadian;
let angle = radian * 180 / Math.PI;
let opposite = Math.abs(angle) > 90;
if (opposite) {
angle -= 180;
offsetRadian = radian * (1 + offset);
} else {
offsetRadian = radian * (1 - offset);
}
let x1 = sx + Math.cos(offsetRadian) * radiusSource;
let y1 = sy + Math.sin(offsetRadian) * radiusSource;
let x2 = tx - Math.sin(offsetRadian) * radiusTarget;
let y2 = ty - Math.cos(offsetRadian) * radiusTarget;
connection.coords = [x1, y1, x2, y2, angle, opposite];
return connection.coords;https://stackoverflow.com/questions/46171262
复制相似问题