【问题标题】:Test if a point is approximately on a line segment formed by two other points测试一个点是否大约在由另外两个点形成的线段上
【发布时间】:2015-07-10 17:29:27
【问题描述】:

我想确定是否在点之间以及在连接这两个点的线段上进行了点击。

我的问题与How can you determine a point is between two other points on a line segment? 类似,但有两点不同:

  1. 我在 javascript 中工作,坐标是整数(像素)
  2. 该点不必正好在线段上。需要容忍度。

以下代码改编自this answer,但我不知道如何在线段周围插入公差

a和b是线段的端点,c是点击的点。我想检查c 是否在a 和b 之间,并且大致在连接a 和b 的段上

PencilTool.prototype._isBetween = function(a,b,c){
    //test if a, b and c are aligned
    var crossproduct = (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y);
    if(Math.abs(crossproduct) !== 0){ return false; }

    var dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y)*(b.y - a.y)
    if(dotproduct < 0){ return false }

    var squaredlengthba = (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y);
    if(dotproduct > squaredlengthba){ return false; }

    return true;
}

【问题讨论】:

  • 可爱的变量名:D

标签: javascript linear-algebra algebra


【解决方案1】:

可能是你想要的公式。

这是该公式的维基百科页面的link。

此来源非常详尽,但可能不是最容易阅读的。您可以阅读 wiki 上的说明,但我将在此处以另一种方式进行说明,希望能帮助您和其他读者形象化。

x0 和 y0 是点击点的坐标。 x1 和 y1 是您的第一条线端点的坐标。 x2 和 y2 是同一条线上第二个端点的坐标。

此公式将三个点的三组坐标作为参数。
前两组坐标形成直线。
第三个参数是您的点击点。 它返回一个距离。

好的,现在让我们试着想象一下这个公式在做什么。 所以你取点击点和线的两个端点,你想象一个三角形。我们得到了三个点,这就是我们制作三角形所需的全部内容。

所以,要找到三角形的高度,你有一个公式,它是熟悉的 A = (1/2)bh 的重新排列

因此,当您找到这样的三角形的高度时,您就是在找到点击点和直线之间的距离。 (准确地说是点击点到直线的最短距离)

上面的较大距离公式基本上就是这样做的。这里的区别,以及为什么它看起来更复杂,是显式显示了计算 A 的部分。

关于您提到的容差,只需为容差设置一个变量,并将距离与该容差进行比较。如果您想要对在线附近的点击的容忍度更“模糊”一些,您需要做更多的数学运算,但我假设您只想知道点击是否距离在线一定距离。

当您编写此函数时,请确保您做好簿记并在正确的位置设置正确的坐标,否则您将返回一段距离,但不是您想要的。 既然你提到你使用的是整数,你可能无法从距离公式中得到一个完美的 int,我的意思是,看看那个平方根,所以如果你没有得到一个完美的 int,不用担心,只需向上或向下取整,

【讨论】:

  • 点赞!只是好奇,如果您的所有点都在 x 轴上,这将如何工作?
  • 只需将所有 Y 值设置为零 :) 或者,abs(x2 - x1) 是一条线上两点之间距离的简单公式。
【解决方案2】:

您可以在两点之间覆盖div,根据需要旋转,根据需要加厚。将hover CSS 或点击事件附加到 div。

使用距离公式确定div的宽度:

使用这个公式来确定旋转角度:

Math.atan2((y1-y2),(x1-x2))*(180/Math.PI)

为简单起见,我在我的代码片段中使用了 jQuery,但我可以用原生 JavaScript 快速重写:

var x1= 10+Math.random()*500/4,
    y1= 10+Math.random()*300/4,
    x2= Math.random()*500/2 + 500/4,
    y2= Math.random()*300/2 + 300/4,
    pt= $('.a').width()/2,
    dx= (x2-x1),
    dy= (y2-y1),
    angle= Math.atan2((y1-y2),(x1-x2))*(180/Math.PI),
    tolerance= 30;

$('.a').css({left: x1, top : y1});
$('.b').css({left: x2, top : y2});

$('div.c').css({
  width: Math.sqrt(dx*dx + dy*dy),
  height: tolerance,
  left: x2+pt,
  top: y2+pt,
  transformOrigin: '0px '+tolerance/4+'px',
  transform: 'rotate('+angle+'deg)'
});

$('div.c')
  .click(function() {
    alert('clicked!');
  });
body {
  margin: 0;
  padding: 0;
}

div.a, div.b, div.c {
  position: absolute;
  border-radius: 50%;
  height: 1.2em;
  width: 1.2em;
  text-align: center;
  background: orange;
}

div.c {
  border: 1px solid #eee;
  background: transparent;
  border-radius: 0;
}

div.c:hover ~ div.a, div.c:hover ~ div.b {
  background: lightgreen;
}

div.c hr {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  height: 1.2em;
  border: none;
  border-top: 1px solid orange;
}

div.c:hover hr {
  border-top: 1px solid green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="c"><hr></div>
<div class="a">A</div>
<div class="b">B</div>

【讨论】:

  • 哇,令人印象深刻!
【解决方案3】:

这是我现在使用的最后一个 javascript 函数。

第一部分使用Ashley Tharp's answer 中解释的距离公式,我们首先测试c 是否在通过a 和b 的线的预定义距离(tolerance)内。

第二部分取自Cyrille Ka' answer from a similar question。

然后,要知道c 是否在a 和b 之间,您还必须检查 (b-a) 和 (c-a) 的点积为正且小于 比a 和b 之间距离的平方。

// a and b are vertices of the segment AB and c is the tested point (here from a mouseclick event (e))
var a={}, b={}, c={};
a.x = 100;
a.y = 100;
b.x = 200;
b.y = 200;
c.x = e.screenX
c.y = e.screeny

console.log(isBetween(a, b, c, 5));

function isBetween(a, b, c, tolerance) {

    //test if the point c is inside a pre-defined distance (tolerance) from the line
    var distance = Math.abs((c.y - b.y)*a.x - (c.x - b.x)*a.y + c.x*b.y - c.y*b.x) / Math.sqrt(Math.pow((c.y-b.y),2) + Math.pow((c.x-b.x),2));
    if (distance > tolerance) { return false; }

    //test if the point c is between a and b
    var dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y)*(b.y - a.y);
    if (dotproduct < 0) { return false; }

    var squaredlengthba = (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y);
    if (dotproduct > squaredlengthba) { return false; }

    return true;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多