THREE JS changes the length of the vector in absolute value

I have two points Vector3 A and B.

I want the vector C to put the path path from A to B, but add 100 pint to its length.

How can I calculate this vector?

enter image description here

+5
source share
2 answers

You can increase the vector of nonzero length by three .js along the len length as follows:

 var oldLength = vector.length(); if ( oldLength !== 0 ) { vector.multiplyScalar( 1 + ( len / oldLength ) ); } 

So, here is how you calculate point C

 var A = new THREE.Vector3( 10, 20, 30 ); var B = new THREE.Vector3( 20, 30, 40 ); var C = new THREE.Vector3(); var len = 10; C.subVectors( B, A ).multiplyScalar( 1 + ( len / C.length() ) ).add( A ); 

three.js r.69

+6
source

In a two-dimensional context, this will give you the C coordinates:

  var oldLength = A.distanceTo(B); var newLength = oldLength + 100; if(oldLength > 0) { Cx = Ax + (Bx - Ax) * newLength / oldLength; Cy = Ay + (By - Ay) * newLength / oldLength; } 
+1
source

Source: https://habr.com/ru/post/1209116/


All Articles