Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find specific point between 2 points - three.js

How can I find a point ( C (x,y,z) ) between 2 points ( A(x,y,z) , B(x,y,z) ) in a thgree.js scene?

I know that with this: mid point I can find the middle point between them, but I don't want the middle point, I want to find the point which is between them and also has distance a from the A point?

in this picture you can see what I mean :

enter image description here

Thank you.


1 Answers

Basically you need to get the direction vector between the two points (D), normalize it, and you'll use it for getting the new point in the way: NewPoint = PointA + D*Length.

You could use length normalized (0..1) or as an absolute value from 0 to length of the direction vector.

Here you can see some examples using both methods:

Using absolute value:

function getPointInBetweenByLen(pointA, pointB, length) {
    
    const dir = pointB.clone().sub(pointA).normalize().multiplyScalar(length);
    return pointA.clone().add(dir);
       
}

And to use with percentage (0..1)

function getPointInBetweenByPerc(pointA, pointB, percentage) {
    
    let dir = pointB.clone().sub(pointA);
    const len = dir.length();
    dir = dir.normalize().multiplyScalar(len*percentage);
    return pointA.clone().add(dir);
       
}

See it in action: http://jsfiddle.net/8mnqjsge/

Hope it helps.

like image 150
fernandojsg Avatar answered Jan 23 '26 02:01

fernandojsg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!