Java examples for java.lang:Math Vector
Gets a unit vector which bisects (p1 - p2) and (p3 - p2).
/*/*from w w w . ja v a2 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% */ import java.math.BigInteger; public class Main{ /** * Gets a unit vector which bisects (p1 - p2) and (p3 - p2). This vector * points to the 'right' side of the line defined by p1->p2->p3. */ public static float[] getRightBisectorVector2D(final float[] p1, final float[] p2, final float[] p3) { // System.out.println("entering getBisectorVector2D ..."); //TEMP // Always retrieves the bisector vector on the right (as opposed to left) // side of the angle made by the two vectors. // z coord. of cross product of p2-(minus)p1 and p3-p2 final float z = orient2D(p1, p2, p3); final float[] v1 = vector(p1, p2); final float[] v2 = vector(p3, p2); final float[] v1Hat = unit(v1); final float[] v2Hat = unit(v2); final float[] vAvg = { (v1Hat[0] + v2Hat[0]) / 2f, (v1Hat[1] + v2Hat[1]) / 2f }; float[] aBisector = null; // ... says what? if ((vAvg[0] == 0 && vAvg[1] == 0) || z == 0) { // Sometimes, z can have a very small nonzero value even when // the points have the same x=coordinate // (Apparently due to floating point arithmetic?) // To handle that case, test for parallel vectors without referring to z, // by comparing vAvg to <0, 0>. final float[] v = MathUtil.vector(p2, p1); aBisector = new float[] { v[1], -v[0] }; } else if (z < 0) { // the curve is curving to the right--vAvg points right aBisector = vAvg; } else if (z > 0) { // the curve is curving to the left--vAvg points left aBisector = new float[] { -vAvg[0], -vAvg[1] }; } final float[] bisector = unit(aBisector); return bisector; } /** * 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; } /** Creates the vector p2-(minus) p1. */ public static float[] vector(final float[] p2, final float[] p1) { // assumes p1, p2 have same lengths if (p2.length != p1.length) return null; final int len = p2.length; final float[] v = new float[len]; for (int i = 0; i < len; i++) { v[i] = p2[i] - p1[i]; } return v; } /** Computes the N-D unit vector in the direction of the vector supplied. */ public static float[] unit(final float[] v) { final float mag = mag(v); final float[] vHat = new float[v.length]; for (int i = 0; i < v.length; i++) { vHat[i] = v[i] / mag; } return vHat; } /** Computes the magnitude of an N-D vector. */ public static float mag(final float[] a) { return (float) Math.sqrt(dot(a, a)); } /** Computes the dot product of two N-D vectors. */ public static float dot(final float[] a, final float[] b) { float sum = 0f; for (int i = 0; i < a.length; i++) { sum += a[i] * b[i]; } return sum; } }