Java examples for java.lang:Math Vector
Calculates the absolute orientation of a given vector.
//package com.java2s; import java.awt.geom.Point2D; public class Main { /** A quarter pi. */ public static final double M_PI_4 = Math.PI * 0.25; /** A double pi. */ public static final double M_2_PI = 2.0 * Math.PI; /**/*from w ww . ja va 2 s . com*/ * Calculates the absolute orientation of a given vector. * * @param vec The vector. * @return The angle from this vector to the x-axis in counter-clockwise or * negative y-axis order. The range is from * {@code 0.0 - 2.0 * Math.PI}. The vector (0, 0) results in an angle * of <code>0</code>. Note that positive y direction points downwards. */ public static double getOrientation(final Point2D vec) { final double x = vec.getX(); final double y = -vec.getY(); if (x == 0.0) return Math.PI * (y > 0.0 ? 0.5 : 1.5); return (x < 0 ? Math.PI : (y < 0 ? M_2_PI : 0)) + fastArcTan(y / x); } /** * A fast implementation of {@link Math#atan(double)}. The maximum error is * <code>0.0015</code> radians. The behavior is the same as the library * function. The algorithm comes from: * <em>"Efficient approximations for the arctangent function", * Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006</em> * * @param a The value whose arc tangent is to be returned. * @return The arc tangent of the argument. * @see Math#atan(double) */ public static double fastArcTan(final double a) { if (a < -1 || a > 1) return Math.atan(a); return M_PI_4 * a - a * (Math.abs(a) - 1) * (0.2447 + 0.0663 * Math.abs(a)); } }