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();

    var pBpA = new THREE.Vector3();

    var pA_pToCheck = new THREE.Vector3();

    var pB_pToCheck = new THREE.Vector3();

    if( <0 || <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


 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 > cos90epsi && < -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