math - d3.js how to calculate new endpoints for each line with a distance of X from node center -


i have force directed graph , i'd adjust endpoints of lines 30px node center. believe need modify tick function calculate new coordinates x1,y1 , x2,y2 haven't touched geometry in long time , can't seem figure out right equation accomplish this.

any or guidance appreciated.

update...

so managed using following (thanks friend). slight caveat, has bug line vertical. if has more efficient way i'd love help.

    function distance(x1,y1,x2,y2) {       y = y2 - y1;       x = x2 - x1;       return math.sqrt(x*x + y*y);     }      function calc_slope(d) {       x1 = d.source.x;       y1 = d.source.y;       x2 = d.target.x;       y2 = d.target.y;        return (y2-y1)/parsefloat(x2-x1);     }      function calc_constant(slope, x, y) {       return (y - (slope * x));     }      function calc_y(slope, constant, x){       return (slope * x) + constant;     }      function adjusted_source_x(d, adjustment_size) {       x1 = d.source.x;       y1 = d.source.y;       x2 = d.target.x;       y2 = d.target.y;        if (distance(x1,y1,x2,y2) < (2 * adjustment_size)){         return x1;       }        if (x1 == x2){         return x1;       }        slope = calc_slope(d);       constant = calc_constant(slope, x1, y1);        if (x1 < x2){         x1_prime = x1 + adjustment_size;         y1_prime = calc_y(slope, constant, x1_prime);          while (distance(x1,y1,x1_prime,y1_prime) > adjustment_size){           x1_prime = x1_prime - 1;           y1_prime = calc_y(slope, constant, x1_prime);         }         return x1_prime;       }       else{         x1_prime = x1 - adjustment_size;         y1_prime = calc_y(slope, constant, x1_prime);          while (distance(x1,y1,x1_prime,y1_prime) > adjustment_size){           x1_prime = x1_prime + 1;           y1_prime = calc_y(slope, constant, x1_prime);         }         return x1_prime;        }     }      function adjusted_source_y(d, adjustment_size) {       x1 = d.source.x;       y1 = d.source.y;       x2 = d.target.x;       y2 = d.target.y;        if (distance(x1,y1,x2,y2) < (2 * adjustment_size)){         return y1;       }        if (x1 == x2){         if(y1 < y2){           return (y1 + adjustment_size)         }         else {           return (y1 - adjustment_size)         }       }        slope = calc_slope(d);       constant = calc_constant(slope, x1, y1);        if (x1 < x2){         x1_prime = x1 + adjustment_size;         y1_prime = calc_y(slope, constant, x1_prime);          while (distance(x1,y1,x1_prime,y1_prime) > adjustment_size){           x1_prime = x1_prime - 1;           y1_prime = calc_y(slope, constant, x1_prime);         }         return y1_prime;       }       else{         x1_prime = x1 - adjustment_size;         y1_prime = calc_y(slope, constant, x1_prime);          while (distance(x1,y1,x1_prime,y1_prime) > adjustment_size){           x1_prime = x1_prime + 1;           y1_prime = calc_y(slope, constant, x1_prime);         }         return y1_prime;        }     }      function adjusted_target_x(d, adjustment_size) {       x1 = d.source.x;       y1 = d.source.y;       x2 = d.target.x;       y2 = d.target.y;        if (distance(x1,y1,x2,y2) < (2 * adjustment_size)){         return x2;       }        if (x1 == x2){         return x2;       }        slope = calc_slope(d);       constant = calc_constant(slope, x1, y1);        if (x2 < x1){         x2_prime = x2 + adjustment_size;         y2_prime = calc_y(slope, constant, x2_prime);          while (distance(x2,y2,x2_prime,y2_prime) > adjustment_size){           x2_prime = x2_prime - 1;           y2_prime = calc_y(slope, constant, x2_prime);         }         return x2_prime;       }       else{         x2_prime = x2 - adjustment_size;         y2_prime = calc_y(slope, constant, x2_prime);          while (distance(x2,y2,x2_prime,y2_prime) > adjustment_size){           x2_prime = x2_prime + 1;           y2_prime = calc_y(slope, constant, x2_prime);         }         return x2_prime;        }     }      function adjusted_target_y(d, adjustment_size) {       x1 = d.source.x;       y1 = d.source.y;       x2 = d.target.x;       y2 = d.target.y;        if (distance(x1,y1,x2,y2) < (2 * adjustment_size)){         return y2;       }        if (x1 == x2){         if(y2 < y1){           return (y2 + adjustment_size)         }         else {           return (y2 - adjustment_size)         }       }        slope = calc_slope(d);       constant = calc_constant(slope, x1, y1);        if (x2 < x1){         x2_prime = x2 + adjustment_size;         y2_prime = calc_y(slope, constant, x2_prime);          while (distance(x2,y2,x2_prime,y2_prime) > adjustment_size){           x2_prime = x2_prime - 1;           y2_prime = calc_y(slope, constant, x2_prime);         }         return y2_prime;       }       else{         x2_prime = x2 - adjustment_size;         y2_prime = calc_y(slope, constant, x2_prime);          while (distance(x2,y2,x2_prime,y2_prime) > adjustment_size){           x2_prime = x2_prime + 1;           y2_prime = calc_y(slope, constant, x2_prime);         }         return y2_prime;        }     }      function tick() {       node.attr("cx", function(d) { return d.x; })         .attr("cy", function(d) { return d.y; })         .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")";});       link.attr("x1", function(d) { return adjusted_source_x(d, 45); })         .attr("y1", function(d) { return adjusted_source_y(d, 45); })         .attr("x2", function(d) { return adjusted_target_x(d, 45); })         .attr("y2", function(d) { return adjusted_target_y(d, 45); });     } 


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -