Compute two arbitrary vectors perpendicular to the given normalized vector (x, y, z), and store them in dest1 and dest2, respectively. - Java java.lang

Java examples for java.lang:Math Vector

Description

Compute two arbitrary vectors perpendicular to the given normalized vector (x, y, z), and store them in dest1 and dest2, respectively.

Demo Code

/*//  w w  w  . j  av  a  2 s .  c  o m
 * (C) Copyright 2015-2016 Kai Burjack

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.

 */
import joml.Vector2f;
import joml.Vector3f;

public class Main{
    /**
     * Compute two arbitrary vectors perpendicular to the given normalized vector <tt>(x, y, z)</tt>, and store them in <code>dest1</code> and <code>dest2</code>,
     * respectively.
     * <p>
     * The computed vectors will themselves be perpendicular to each another and normalized. So the tree vectors <tt>(x, y, z)</tt>, <code>dest1</code> and
     * <code>dest2</code> form an orthonormal basis.
     * 
     * @param x
     *            the x coordinate of the normalized input vector
     * @param y
     *            the y coordinate of the normalized input vector
     * @param z
     *            the z coordinate of the normalized input vector
     * @param dest1
     *            will hold the first perpendicular vector
     * @param dest2
     *            will hold the second perpendicular vector
     */
    public static void perpendicular(float x, float y, float z,
            Vector3f dest1, Vector3f dest2) {
        float magX = z * z + y * y;
        float magY = z * z + x * x;
        float magZ = y * y + x * x;
        float mag;
        if (magX > magY && magX > magZ) {
            dest1.x = 0;
            dest1.y = z;
            dest1.z = -y;
            mag = magX;
        } else if (magY > magZ) {
            dest1.x = z;
            dest1.y = 0;
            dest1.z = x;
            mag = magY;
        } else {
            dest1.x = y;
            dest1.y = -x;
            dest1.z = 0;
            mag = magZ;
        }
        float len = 1.0f / (float) Math.sqrt(mag);
        dest1.x *= len;
        dest1.y *= len;
        dest1.z *= len;
        dest2.x = y * dest1.z - z * dest1.y;
        dest2.y = z * dest1.x - x * dest1.z;
        dest2.z = x * dest1.y - y * dest1.x;
    }
    /**
     * Compute two arbitrary vectors perpendicular to the given normalized vector <code>v</code>, and store them in <code>dest1</code> and <code>dest2</code>,
     * respectively.
     * <p>
     * The computed vectors will themselves be perpendicular to each another and normalized. So the tree vectors <code>v</code>, <code>dest1</code> and
     * <code>dest2</code> form an orthonormal basis.
     * 
     * @param v
     *            the {@link Vector3f#normalize() normalized} input vector
     * @param dest1
     *            will hold the first perpendicular vector
     * @param dest2
     *            will hold the second perpendicular vector
     */
    public static void perpendicular(Vector3f v, Vector3f dest1,
            Vector3f dest2) {
        perpendicular(v.x, v.y, v.z, dest1, dest2);
    }
}

Related Tutorials