O
O
Oleg Ulyanov2017-01-11 11:09:57
3D
Oleg Ulyanov, 2017-01-11 11:09:57

How to rotate the hexagon by a smaller angle so that the longest chord (connecting opposite points) is parallel to the axis?

There is a hexagon (it has a thickness, that is, not a flat figure), you need to somehow get its vertices, determine the long chord and rotate it parallel to 0X (global) by a smaller angle.
64df97cb35864f06b6ed39f4601f7a34.pngfc2bd0dfeb0245b39bd0024089b8f9c4.png
I do all this mechanics in Babylon.js, but the TREE.js examples should help too.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Ulyanov, 2017-01-11
@gelosoft

const vertices = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
let vectorExtreme = null;
let length = 0;
for(let i = 0; i < vertices.length; i += 3) {
    let vertex = BABYLON.Vector3.FromArray(vertices, i);
    let vector = BABYLON.Vector3.TransformCoordinates(vertex, this.hexagon.mesh.getWorldMatrix());
    if(!i) vectorExtreme = vector;
    vector.z = 0;
    if(vector.length() > length && vector.x > 0) {
        length = vector.length();
        vectorExtreme = vector;
    }
}

const vector1 = new BABYLON.Vector3(vectorExtreme.x, vectorExtreme.y, 0);
const vector2 = new BABYLON.Vector3(1,0,0);

let angle = Math.acos(BABYLON.Vector3.Dot(vector1, vector2) /
    (vector1.length() * vector2.length()));
if(vectorExtreme.y > 0) {
    angle = -angle;
}

mesh.rotate(BABYLON.Axis.Z, angle, BABYLON.Space.WORLD);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question