# How to determine wether a point is between two other points?

I made a little function that doesn’t work quite right…

function isPointbetweenTwoOthers (pA, pB, pToCheck) {

``````    var pApB = new THREE.Vector3();
pApB.subVectors(pA,pB).normalize();

var pBpA = new THREE.Vector3();
pBpA.subVectors(pB,pA).normalize();

var pA_pToCheck = new THREE.Vector3();
pA_pToCheck.subVectors(pA,pToCheck).normalize();

var pB_pToCheck = new THREE.Vector3();
pB_pToCheck.subVectors(pB,pToCheck).normalize();

if(pA_pToCheck.dot(pApB) <0 || pB_pToCheck.dot(pBpA) <0)
return false
else return true
}
``````

What am I doing wrong? According to the output of my function this left above point is inside the two on the right

SOLUTION:

`````` function isPointbetweenTwoOthers (pA, pB, pToCheck) {
var nvAtoB = new THREE.Vector3();
nvAtoB.subVectors(pB, pA).normalize();

var nvAtoC = new THREE.Vector3();
nvAtoC.subVectors(pToCheck, pA).normalize();

var nvBtoC = new THREE.Vector3();
nvBtoC.subVectors(pToCheck, pB).normalize();

let epsilon = 0.0016;
let cos90epsi = 1.0 - epsilon;
return nvAtoB.dot(nvAtoC) > cos90epsi && nvAtoB.dot(nvBtoC) < -cos90epsi;
}
``````

As it is right now, your function is checking if pToCheck is inside the space formed by the two planes defined with pA, pB, and the line between them as their plane normals.

The problem is what you want is not well specified: “wether a point is between two other points” should be true only if pToCheck is on the line segment between pA and pB, as I see it. Which is impossible to determine without a threshold, due to floating point precision.

Oh well you solved it, putting in a threshold as I said.

1 Like