【发布时间】:2015-07-10 17:29:27
【问题描述】:
我想确定是否在点之间以及在连接这两个点的线段上进行了点击。
我的问题与How can you determine a point is between two other points on a line segment? 类似,但有两点不同:
- 我在 javascript 中工作,坐标是整数(像素)
- 该点不必正好在线段上。需要容忍度。
以下代码改编自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