Obtains the z-coordinate of the cross product of the 2D vectors p2-p1 and p3-p2, useful for determining whether the curve p1->p2->p3 is curving to the right or left. - Java java.lang

Java examples for java.lang:Math Vector

Description

Obtains the z-coordinate of the cross product of the 2D vectors p2-p1 and p3-p2, useful for determining whether the curve p1->p2->p3 is curving to the right or left.

Demo Code

/*/*  w  w  w  .  jav  a  2 s.c o  m*/
 * #%L
 * VisBio application for visualization of multidimensional biological
 * image data.
 * %%
 * Copyright (C) 2002 - 2014 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */
//package com.java2s;

public class Main {
    /**
     * Obtains the z-coordinate of the cross product of the 2D vectors p2-p1 and
     * p3-p2, useful for determining whether the curve p1->p2->p3 is curving to
     * the right or left.
     */
    public static float orient2D(final float[] p1, final float[] p2,
            final float[] p3) {
        final float x1 = p1[0];
        final float y1 = p1[1];
        final float x2 = p2[0];
        final float y2 = p2[1];
        final float x3 = p3[0];
        final float y3 = p3[1];
        // z coord. of cross product of p2-(minus)p1 and p3-p2
        final float z = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
        return z;
    }
}

Related Tutorials