This snippet of code will rotate all vectors in the tree by 90 degrees around the specified axis defined as a vertex [-42, 51, 11]
. The rotation axis is determined by creating a line from the origin [0,0,0]
to the vertex [-42, 51, 11]
, ensuring that point A
remains unchanged after the rotation.
const tree = [
[ 0, 0, 0], // A
[ 2, 151, 2], // B
[ -62, 283, 63], // C
[ 62, 296, -58], // D
[-104, 334, 74], // E
[ -58, 338, 45], // F
[ 67, 403, -55], // G
[ 105, 365, -86], // H
]
// Define the rotation axis:
let axis = new THREE.Vector3(-42, 51, 11)
// Normalize the axis:
axis.normalize()
// Create the matrix:
let matrix = new THREE.Matrix4()
// Set the rotation angle in radians:
let radians = 90 * Math.PI / 180
// Apply the rotation to the matrix:
matrix.makeRotationAxis(axis, radians)
// Rotate all vectors in the tree using the new matrix
let newTree = []
for(const vector of tree) {
// Define the vector3 object:
let vec = new THREE.Vector3(vector[0], vector[1], vector[2])
vec.applyMatrix4(matrix)
newTree.push(vec.toArray()) // You can choose to keep the original Vector3 object without converting it to array
}
// The rotated vectors are now stored in newTree
console.log(newTree)
Check out the live version at https://codepen.io/mtveerman/pen/PoZZpvw for a demonstration