math - parallel links between nodes -
i've webcola&d3 svg graph nodes , links between them. till today, between nodes links 1 way, if b connected a, 1 way link.
today told need support having 2 way links, meaning can send link b , b can send link a.
now i'm stuck math , how accomplish it, used algorithm found draw links till today guess draw links center of node, need show 2 way links in parallel :
here algorithm use calculate links position:
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;
this part of function result goes 'd' attr of path this:
.attr('d', `m${x1} ${y1} l ${x2} ${y2}`)
the result of 2 way links right override each other, can me improve algorithm make 2 way links parallel?
update: position of links need calculated new radian considering offset, like:
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 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;
Comments
Post a Comment