如何在2点之间创建中点?

时间:2016-12-05 16:28:07

标签: javascript d3.js svg

我试图在三角形的任意两点之间创建点。我如何创建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];

1 个答案:

答案 0 :(得分:1)

由于你有一个points数据,三角形中有一个冗余的第四个点(与第一个点相同),我们可以简单地删除数据的第四个对象:

.data(points.slice(0, points.length-1))

甚至不介意最后数据的范围错误:

.attr("cx", (d,i)=>(d.x + points[i+1].x)/2)
.attr("cy", (d,i)=>(d.y + points[i+1].y)/2);

这是一个演示。蓝点是数据数组的点,红点是中点。

var svg = d3.select("body")
	.append("svg")
	.attr("width", 400)
	.attr("height", 300);
	
var points = [
      {"x": 150, "y": 10},
       {"x": 10, "y": 200},
       {"x": 300, "y": 200},  
       {"x": 150, "y": 10}
      ];
			
var circles = svg.selectAll(".circles")
	.data(points)
	.enter()
	.append("circle")
	.attr("r", 5)
	.attr("fill", "blue")
	.attr("cx", d=>d.x)
	.attr("cy", d=>d.y);
	
var midPoints = svg.selectAll(".midPoints")
	.data(points.slice(0, points.length-1))
	.enter()
	.append("circle")
	.attr("r", 5)
	.attr("fill", "brown")
	.attr("cx", (d,i)=>(d.x + points[i+1].x)/2)
	.attr("cy", (d,i)=>(d.y + points[i+1].y)/2);
<script src="https://d3js.org/d3.v4.min.js"></script>

相同的代码段,每个三角形都有一行:

var svg = d3.select("body")
	.append("svg")
	.attr("width", 400)
	.attr("height", 300);
	
var points = [
      {"x": 150, "y": 10},
       {"x": 10, "y": 200},
       {"x": 300, "y": 200},  
       {"x": 150, "y": 10}
      ];

var line = d3.line()
  .x(d=>d.x)
  .y(d=>d.y);

var lineMid = d3.line()
  .x((d,i)=>(d.x + points[i+1].x)/2)
  .y((d,i)=>(d.y + points[i+1].y)/2)
  .curve(d3.curveLinearClosed);

var lineCircles = svg.append("path")
  .attr("d", line(points))
  .attr("fill", "none")
  .attr("stroke", "blue")
  .attr("opacity", 0.4);

var lineMidPoints = svg.append("path")
  .attr("d", lineMid(points.slice(0, points.length-1)))
  .attr("fill", "none")
  .attr("stroke", "brown")
  .attr("opacity", 0.4);
			
var circles = svg.selectAll(".circles")
	.data(points)
	.enter()
	.append("circle")
	.attr("r", 5)
	.attr("fill", "blue")
	.attr("cx", d=>d.x)
	.attr("cy", d=>d.y);
	
var midPoints = svg.selectAll(".midPoints")
	.data(points.slice(0, points.length-1))
	.enter()
	.append("circle")
	.attr("r", 5)
	.attr("fill", "brown")
	.attr("cx", (d,i)=>(d.x + points[i+1].x)/2)
	.attr("cy", (d,i)=>(d.y + points[i+1].y)/2);
<script src="https://d3js.org/d3.v4.min.js"></script>