【发布时间】:2016-12-06 09:47:40
【问题描述】:
我正在尝试在三角形的任意 2 个点之间创建点。如何在 2 点之间创建中点?例如,我将如何创建一个点 在坐标 (150, 0) 和 (0, 200) 之间?
var points = [
{"x": 150, "y": 0},
{"x": 0, "y": 200},
{"x": 300, "y": 200},
{"x": 150, "y": 0}
];
//创建 SVG 容器
var svg = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 500)
.style('fill', '#113F8C')
.style('padding-left', '20px')
.style('padding-top', '5px');
// 绘制路径
var path = svg.append("path")
.data([lineData])
.attr("stroke", "black")
.attr("stroke-width", 3)
.attr("d", d3.line());
//设置点
svg.selectAll("circle")
.data(points)
.enter().append("circle")
.attr("cx", function(d) {
console.log(d.x+ " hello");
for (var i = 0; i < points.length; i++) {
//console.log(points[i]);
x[i] = d.x;
console.log(d.x);
};
return d.x;
})
.attr("cy", function(d) {console.log(d.y+ " hello"); return d.y; })
.attr("r", 5);
// THE X TEST
x[0];
【问题讨论】:
-
你只是想要数学吗?两点的中点正好是
[ (x0 + x1) / 2, (y0 + y1) / 2 ] -
这很酷,但实际上正在寻找如何在上面的代码中使用 D3 执行此操作。
标签: javascript d3.js svg