我有 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 轻松转换。
The Math.atan2() method returns a numeric value between -π and π representing the angle theta of an (x, y) point. This is the counterclockwise angle, measured in radians, between the positive X axis, and the point (x, y). Note that the arguments to this function pass the y-coordinate first and the x-coordinate second.
关于d3.js - D3 求线的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34293488/