How to calculate the final point coordinates with an offset?

39 views
Skip to first unread message

Gregoor van der Eyken

unread,
Jul 8, 2017, 7:42:52 AM7/8/17
to JavaScript InfoVis Toolkit

I have to subtract a distance from a destination point, to give the final destination on the same line.

So, first I have calculated the distance between the source and target points, with the Pythagorean theorem, but my memories of Thales's theorem are too faulty to find the final point (on same line), with the right x and y attributes.

function getDistance (from, to){
    return Math.hypot(to.x - from.x, to.y - from.y);
}
function getFinalTo (from, to, distanceToSubstract){

    //with Pythagore we obtain the distance between the 2 points
    var originalDistance = getDistance(from, to);
    var finalDistance = originalDistance - distanceToSubstract;

    //Now, I was thinking about Thales but all my tries are wrong
    //Here some of ones, I need to get finalTo properties to draw an arrow to a node without

    var finalTo = new Object;
    finalTo.x = ((1 - finalDistance) * from.x) + (finalDistance * to.x);
    finalTo.y = ((1 - finalDistance) * from.y) + (finalDistance * to.y);

    return finalTo;
}

Indeed, the arrowhead be hidden by the round node that can be about 100 pixels of radius, so I try to get the final point.

Thanks a lot. 

Regards,

Reply all
Reply to author
Forward
0 new messages