首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节点间并行链路

节点间并行链路
EN

Stack Overflow用户
提问于 2017-09-12 08:15:12
回答 1查看 62关注 0票数 0

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

今天我被告知我需要支持双向链接,这意味着A可以发送链接到B,B可以发送链接到A。

现在我被困在数学和如何完成它,我使用了一些我找到的算法,直到今天,我想从节点的中心画出链接,我需要并行地显示两个链接,如下所示:

下面是计算链接位置的算法:

代码语言:javascript
复制
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,如下所示:

代码语言:javascript
复制
.attr('d', `M${x1} ${y1} L ${x2} ${y2}`)

现在双向链接的结果是它们相互覆盖,谁能帮我改进这个算法,使双向链接并行吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-12 08:55:45

Update:链接的位置需要由考虑偏移量的新弧度计算,如:

代码语言:javascript
复制
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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46171262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档