Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Receives the Euler angles in radians and returns the components of the quaternion.
     * 
     * @param x The x or phi axis in radians.
     * @param y The y axis or theta in radians.
     * @param z The z axis or psi in radians
     * @return A 4 positions array which contains the x, y, z and w in this order.
     */
    public static final float[] eulerToQuaternion(float x, float y, float z) {
        float[] quaternion = new float[4];

        quaternion[0] = (float) ((Math.cos(x / 2) * Math.cos(y / 2) * Math.cos(z / 2))
                + (Math.sin(x / 2) * Math.sin(y / 2) * Math.sin(z / 2)));
        quaternion[1] = (float) ((Math.sin(x / 2) * Math.cos(y / 2) * Math.cos(z / 2))
                - (Math.cos(x / 2) * Math.sin(y / 2) * Math.sin(z / 2)));
        quaternion[2] = (float) ((Math.cos(x / 2) * Math.sin(y / 2) * Math.cos(z / 2))
                + (Math.sin(x / 2) * Math.cos(y / 2) * Math.sin(z / 2)));
        quaternion[3] = (float) ((Math.cos(x / 2) * Math.cos(y / 2) * Math.sin(z / 2))
                - (Math.sin(x / 2) * Math.sin(y / 2) * Math.cos(z / 2)));

        return quaternion;
    }
}