2017-08-30 102 views
2

嗨,大家好,我已經成功使用d3.js繪製網格。網格由10行5列,我應該如何使用mouseOnclick那個路口應該像我標記的行和列的交叉點指示點相交如何點svg和d3.js中的線的交點

var svg = d3.select("body").append("svg").attr("width", 500).attr("height", 500); 



var inputs = [ 
{ "x1": 100, "x2": 500, "y1": 50, "y2": 50}, 
{ "x1": 100, "x2": 500, "y1": 90, "y2": 90}, 
{ "x1": 100, "x2": 500, "y1": 130, "y2": 130}, 
{ "x1": 100, "x2": 500, "y1": 170, "y2": 170}, 
{ "x1": 100, "x2": 500, "y1": 210, "y2": 210}, 
{ "x1": 100, "x2": 500, "y1": 250, "y2": 250}, 
{ "x1": 100, "x2": 500, "y1": 290, "y2": 290}, 
{ "x1": 100, "x2": 500, "y1": 330, "y2": 330}, 
{ "x1": 100, "x2": 500, "y1": 370, "y2": 370}, 
{ "x1": 100, "x2": 500, "y1": 400, "y2": 400}, 



//columns 
{ "x1": 100, "x2": 100, "y1": 50, "y2": 400}, 
{ "x1": 200, "x2": 200, "y1": 50, "y2": 400}, 
{ "x1": 300, "x2": 300, "y1": 50, "y2": 400}, 
{ "x1": 400, "x2": 400, "y1": 50, "y2": 400}, 
{ "x1": 500, "x2": 500, "y1": 50, "y2": 400}, 
] 

     svg.selectAll("line").data(inputs).enter().append("line") 

     .attr("x1", function(d) { 
      return d.x1; 
     }) 
     .attr("x2", function(d) { 
      return d.x2; 
     }) 
     .attr("y1", function(d) { 
      return d.y1; 
     }) 
     .attr("y2", function(d) { 
      return d.y2; 
     }) 
     .attr("stroke", "red") 

這裏是我的小提琴:https://jsfiddle.net/7mmgedax/

回答

0

你需要將你的數組拆分成行和列,然後找到每個點的交集。對於交點一個非常基本的算法是這樣的:

function calculateIntersectionPoint(line1StartX, line1StartY, line1EndX, line1EndY, line2StartX, line2StartY, line2EndX, line2EndY) { 
    // if the lines intersect, the result contains the x and y of the intersection (treating the lines as infinite) and booleans for whether line segment 1 or line segment 2 contain the point 
    var denominator, a, b, numerator1, numerator2, result = { 
     x: null, 
     y: null, 
     onLine1: false, 
     onLine2: false 
    }; 
    denominator = ((line2EndY - line2StartY) * (line1EndX - line1StartX)) - ((line2EndX - line2StartX) * (line1EndY - line1StartY)); 
    if (denominator == 0) { 
     return result; 
    } 
    a = line1StartY - line2StartY; 
    b = line1StartX - line2StartX; 
    numerator1 = ((line2EndX - line2StartX) * a) - ((line2EndY - line2StartY) * b); 
    numerator2 = ((line1EndX - line1StartX) * a) - ((line1EndY - line1StartY) * b); 
    a = numerator1/denominator; 
    b = numerator2/denominator; 

    // if we cast these lines infinitely in both directions, they intersect here: 
    result.x = line1StartX + (a * (line1EndX - line1StartX)); 
    result.y = line1StartY + (a * (line1EndY - line1StartY)); 
/* 
     // it is worth noting that this should be the same as: 
     x = line2StartX + (b * (line2EndX - line2StartX)); 
     y = line2StartX + (b * (line2EndY - line2StartY)); 
     */ 
    // if line1 is a segment and line2 is infinite, they intersect if: 
    if (a > 0 && a < 1) { 
     result.onLine1 = true; 
    } 
    // if line2 is a segment and line1 is infinite, they intersect if: 
    if (b > 0 && b < 1) { 
     result.onLine2 = true; 
    } 
    // if line1 and line2 are segments, they intersect if both of the above are true 

    return result; 
}; 

的工作演示可以在https://jsfiddle.net/8gguunnq/1/

找到其中對交點圓已經繪就。如果你想省略邊界條件,那麼改變回路的限制爲https://jsfiddle.net/8gguunnq/2/

+0

是否有可能將整個網格 –