IlikedCPlusPlus
IlikedCPlusPlus

Reputation: 57

Determine intersection point between orthogonal line and two vectors

I'm having the following situation in my ThreeJS app: I have one observation point P and one line L. Now I want to project perform a projection from P on the vector V which represents the line L. I have included a graphic for better measurement.

Illustration

The baseline L (which I will project on) goes through A and B. The projected vector V only goes through A and C. Now, I have written a small function which the point on L which is part of the orthogonal projection. It goes as follows:

    import * as THREE from 'three'

    /**
     * Returns an orthogonal projection from the observation point to the base line
     * @param base base line consisting of the start and end point (start = index 0, end = index 1)
     * @param observationPoint observation point
     * @returns 
     */
    function getOrthogonalProjection(
    base: THREE.Vector3[],
    observationPoint: THREE.Vector3
    ): THREE.Vector3 {
    const result: THREE.Vector3 = new THREE.Vector3()

    // Setup the vectors
    const baseDirectionVector = base[1].clone().sub(base[0].clone())
    const connectionDirectionVector = observationPoint.clone().sub(base[0].clone()) 
    const baseDirectionVectorLength = baseDirectionVector.lengthSq()

    // Compute dot product and amount of the scalar 
    const dotProduct = baseDirectionVector.dot(connectionDirectionVector)
    const scalar = dotProduct/baseDirectionVectorLength;

    const projection = baseDirectionVector.multiplyScalar(scalar)

    return projection
    }

After checking the input parameters as well as the results on paper, I unfortunatly only see a result point which is slightly moved to the right. Furthermore, this point is not inside the orthogonal projection. Does somebody know what I might have done wrong?

Upvotes: 0

Views: 14

Answers (0)

Related Questions