D3.js D3求直线的角度

D3.js D3求直线的角度,d3.js,D3.js,我有x1,y1,x2,y2,怎么计算这条线的角度 我必须使用纯数学吗?或者d3里有什么东西可以帮我 edgeContainer.append("g") .selectAll("path") .data(edges) .enter() .append("path") .attr("class", function (

我有x1,y1,x2,y2,怎么计算这条线的角度

我必须使用纯数学吗?或者d3里有什么东西可以帮我

            edgeContainer.append("g")
                .selectAll("path")
                .data(edges)
                .enter()
                .append("path")
                .attr("class", function (d) {
                    return "nodeLine animate nodeLine_" + NodeUtils.createEdgeClass(d) + " node_" + d.outV + " node_" + d.inV;
                })
                .style("fill", "none")
                .style(edgeType == 'circular' ? "marker-start" : "marker-end", "url(#arrow)")
                .style("stroke", function (d) {
                    return options.edgeStroke ? options.edgeStroke : ReferenceData.getByRelationshipType(d.label).stroke;
                })
                .style("stroke-dasharray", function (d) {
                    return options.edgeStrokeDasharray ? (options.edgeStrokeDasharray == 'none' ? false : options.edgeStrokeDasharray) : ReferenceData.getByRelationshipType(d.label)['stroke-dasharray'];
                })
                .attr("d", function (d, i) {
                    var x1 = d.targetNode.node.x;
                    var y1 = d.targetNode.node.y;
                    var x2 = d.sourceNode.node.x;
                    var y2 = d.sourceNode.node.y;
                })

            ;

角度的计算很容易:

 var angle = Math.atan2(y2 - y1, x2 - x1));
如果需要度,可以通过将其乘以180/pi来轻松转换

方法返回一个介于-π和π之间的数值,表示(x,y)点的角度θ。这是正X轴和点(X,y)之间的逆时针角度,以弧度为单位。请注意,此函数的参数首先传递y坐标,然后传递x坐标

文件: