寻找虚点的距离

时间:2015-12-12 21:29:00

标签: javascript math fabricjs trigonometry

我有所有坐标但是Px,我需要找到Px和鼠标位置(红线)之间的距离。它将在javascript,任何想法上实现?

1 个答案:

答案 0 :(得分:1)

你去了:

var P1 = {x: 10, y: 5},
    P2 = {x: 8,  y: 4},
    PM = {x: 2,  y: 5};

function distance(source, target1, target2) {
    var zaehler = Math.abs((target2.x - target1.x)*(target1.y - source.y) - (target1.x - source.x)*(target2.y - target1.y));
    var nenner = Math.sqrt(Math.pow(target2.x - target1.x, 2) + Math.pow(target2.y - target1.y, 2));
    return zaehler/nenner;
}

console.log(distance(PM, P2, P1) === distance(PM, P1, P2));