Finds the closest value in quad that is closest in distance to angle. - Java java.lang

Java examples for java.lang:Math Geometry Distance

Description

Finds the closest value in quad that is closest in distance to angle.

Demo Code

/*//w  w  w.j  a v a 2 s  .  com
 * Copyright (C) 2011 apurv
 *
 * 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 3 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/>.
 */
import static java.lang.System.out;
import java.util.Arrays;
import java.util.Vector;

public class Main{
    public static void main(String[] argv) throws Exception{
        double angle = 2.45678;
        double[] quad = new double[]{34.45,35.45,36.67,37.78,37.0000,37.1234,67.2344,68.34534,69.87700};
        System.out.println(findClosestAngle(angle,quad));
    }
    /**
     * Finds the closest value in quad that is closest in distance to angle.
     * @param angle
     * @param quad
     * @return 
     */
    private static double findClosestAngle(double angle, Double[] quad) {
        double closestAngle = 0;
        int l = 0;
        int u = quad.length - 1;
        int mid = (l + u) / 2;
        while (l <= u) {

            mid = (l + u) / 2;
            if (angle > quad[mid]) {
                l = mid + 1;

            } else if (angle < quad[mid]) {
                u = mid - 1;

            } else {
                break;
            }
        }
        mid = (l + u) / 2;
        closestAngle = quad[mid];

        //IMP: The closestAngle obtained above is the angle just smaller than angle in the array quad.
        //     As an additional correction we need to check which among quad[mid] and quad[mid+1] is closer to angle.
        //     Also the case when size of quad is 1 is also handled.
        //Begin Correction
        if (mid + 1 < quad.length) {
            Double closestAngle1 = quad[mid];
            Double closestAngle2 = quad[mid + 1];
            Double distance1 = findMinAngularDistance(closestAngle1, angle);
            Double distance2 = findMinAngularDistance(closestAngle2, angle);
            if (distance1 < distance2) {
                closestAngle = closestAngle1;

            } else {
                closestAngle = closestAngle2;

            }
        }
        //End Correction

        return closestAngle;
    }
    /**
     * Finds the minimum angular distance between two angles.
     * @param angle1
     * @param angle2
     * @return
     */
    private static double findMinAngularDistance(double angle1,
            double angle2) {
        double angDistance = 0.0;
        if (Math.signum(angle2) == Math.signum(angle1)) {
            angDistance = Math.abs(angle2 - angle1);
        } else {
            double sumAngle = Math.abs(angle1) + Math.abs(angle2);
            angDistance = Math.min(sumAngle, 360 - sumAngle);
        }
        return angDistance;
    }
}

Related Tutorials