Utilizing the cross product operation of two vectors can provide a solution to this particular problem.
function verifyPointAlignment (point1, point2, pointToVerify) {
var check = new THREE.Vector3();
check.crossVectors(point1.clone().sub(pointToVerify), point2.clone().sub(pointToVerify));
return !check.length();
}
THREE.verifyPointOnLineAndBetweenPoints = function (point1, point2, pointToVerify) {
if (!verifyPointAlignment(point1, point2, pointToVerify)) {
return false;
}
var dx = point2.x - point1.x;
var dy = point2.y - point1.y;
// determining if the line is more horizontal or vertical:
if (Math.abs(dx) >= Math.abs(dy)) {
if (dx > 0) {
return point1.x <= pointToVerify.x && pointToVerify.x <= point2.x;
} else {
return point2.x <= pointToVerify.x && pointToVerify.x <= point1.x;
}
} else {
if (dy > 0 ) {
return point1.y <= pointToVerify.y && pointToVerify.y <= point2.y;
} else {
return point2.y <= pointToVerify.y && pointToVerify.y <= point1.y;
}
}
}
A sample call:
THREE.verifyPointOnLineAndBetweenPoints(new THREE.Vector3(1, 0, 0), new THREE.Vector3(2, 0, 0), new THREE.Vector3(2, 0, 0));
If you only need to determine if a point lies on a line, use the following function:
verifyPointAlignment(new THREE.Vector3(1, 0, 0), new THREE.Vector3(2, 0, 0), new THREE.Vector3(2, 0, 0));