Returns the difference of both orientations. - Java java.lang

Java examples for java.lang:Math Geometry

Description

Returns the difference of both orientations.

Demo Code


//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  w  w. ja  v  a  2  s . co  m
     * Returns the difference of both orientations.
     * 
     * @param o1 The first orientation which ranges from
     *          {@code 0.0 - 2.0 * Math.PI}.
     * @param o2 The second orientation which ranges from
     *          {@code 0.0 - 2.0 * Math.PI}.
     * @return The absolute difference between those orientations which ranges
     *         from {@code 0.0 - Math.PI}.
     */
    public static double getOrientationDifference(final double o1,
            final double o2) {
        final double diff = Math.abs(o1 - o2);
        return diff > Math.PI ? M_2_PI - diff : diff;
    }

    /**
     * Returns the difference of the orientations of the given vectors.
     * 
     * @param v1 The first vector.
     * @param v2 The second vector.
     * @return The absolute difference between the orientations of the vectors
     *         which ranges from {@code 0.0 - Math.PI}.
     */
    public static double getOrientationDifference(final Point2D v1,
            final Point2D v2) {
        return getOrientationDifference(getOrientation(v1),
                getOrientation(v2));
    }

    /**
     * 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));
    }
}

Related Tutorials