Calculate the normal of a surface defined by points v1, v2 and v3 and store it in dest. - Java java.lang

Java examples for java.lang:Math Geometry

Description

Calculate the normal of a surface defined by points v1, v2 and v3 and store it in dest.

Demo Code

/*//from   w w w  .  ja  v a2s .co 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{
    /**
     * Calculate the normal of a surface defined by points <code>v1</code>, <code>v2</code> and <code>v3</code> and store it in <code>dest</code>.
     * 
     * @param v0
     *            the first position
     * @param v1
     *            the second position
     * @param v2
     *            the third position
     * @param dest
     *            will hold the result
     */
    public static void normal(Vector3f v0, Vector3f v1, Vector3f v2,
            Vector3f dest) {
        normal(v0.x, v0.y, v0.z, v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, dest);
    }
    /**
     * Calculate the normal of a surface defined by points <tt>(v1X, v1Y, v1Z)</tt>, <tt>(v2X, v2Y, v2Z)</tt> and <tt>(v3X, v3Y, v3Z)</tt>
     * and store it in <code>dest</code>.
     * 
     * @param v0X
     *            the x coordinate of the first position
     * @param v0Y
     *            the y coordinate of the first position
     * @param v0Z
     *            the z coordinate of the first position
     * @param v1X
     *            the x coordinate of the second position
     * @param v1Y
     *            the y coordinate of the second position
     * @param v1Z
     *            the z coordinate of the second position
     * @param v2X
     *            the x coordinate of the third position
     * @param v2Y
     *            the y coordinate of the third position
     * @param v2Z
     *            the z coordinate of the third position
     * @param dest
     *            will hold the result
     */
    public static void normal(float v0X, float v0Y, float v0Z, float v1X,
            float v1Y, float v1Z, float v2X, float v2Y, float v2Z,
            Vector3f dest) {
        dest.x = ((v1Y - v0Y) * (v2Z - v0Z)) - ((v1Z - v0Z) * (v2Y - v0Y));
        dest.y = ((v1Z - v0Z) * (v2X - v0X)) - ((v1X - v0X) * (v2Z - v0Z));
        dest.z = ((v1X - v0X) * (v2Y - v0Y)) - ((v1Y - v0Y) * (v2X - v0X));
        dest.normalize();
    }
}

Related Tutorials