我有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;
})
;
Run Code Online (Sandbox Code Playgroud)
角度的计算很容易:
var angle = Math.atan2(y2 - y1, x2 - x1));
Run Code Online (Sandbox Code Playgroud)
如果你需要度数,你可以通过乘以180/pi轻松转换它.
Math.atan2()方法返回-π和π之间的数值,表示(x,y)点的角度θ.这是在正X轴和点(x,y)之间以弧度为单位的逆时针角度.请注意,此函数的参数首先传递y坐标,然后传递x坐标.
文档:developer.mozilla.org - Math.atan2()