Android Distance Calculate fetchRawEte(boolean useBearing, double distance, double speed, double bearing, double heading)

Here you can find the source of fetchRawEte(boolean useBearing, double distance, double speed, double bearing, double heading)

Description

Fetch the raw estimated time enroute given the input parameters

License

Open Source License

Parameter

Parameter Description
distance - how far to the target
speed - how fast we are moving
bearing - direction to target
heading - direction of movement

Return

int value of HR * 100 + MIN for the ete, -1 if not applicable

Declaration

private static Time fetchRawEte(boolean useBearing, double distance,
        double speed, double bearing, double heading) 

Method Source Code

//package com.java2s;

import android.text.format.Time;

public class Main {
    /***/*from www  .  j  a va 2s  . c  o  m*/
     * Fetch the raw estimated time enroute given the input parameters
     * @param distance - how far to the target
     * @param speed - how fast we are moving
     * @param bearing - direction to target
     * @param heading - direction of movement
     * @return int value of HR * 100 + MIN for the ete, -1 if not applicable
     */
    private static Time fetchRawEte(boolean useBearing, double distance,
            double speed, double bearing, double heading) {
        double xFactor = 1;
        if (useBearing) {
            // We can't assume that we are heading DIRECTLY for the destination, so 
            // we need to figure out the multiply factor by taking the COS of the difference
            // between the bearing and the heading.
            double angDif = angularDifference(heading, bearing);

            // If the difference is 90 or greater, then ETE means nothing as we are not
            // closing on the target
            if (angDif >= 90)
                return null;

            // Calculate the actual relative speed closing on the target
            xFactor = Math.cos(angDif * Math.PI / 180);
        }

        // Calculate the travel time in seconds
        double eteTotal = (distance / (speed * xFactor)) * 3600;

        // Allocate an empty time object
        Time ete = new Time();

        // Extract the hours
        ete.hour = (int) (eteTotal / 3600); // take whole int value as the hours
        eteTotal -= (ete.hour * 3600); // Remove the hours that we extracted

        // Convert what's left to fractional minutes
        ete.minute = (int) (eteTotal / 60); // Get the int value as the minutes now
        eteTotal -= (ete.minute * 60); // remove the minutes we just extracted

        // What's left is the remaining seconds 
        ete.second = Math.round((int) eteTotal); // round as appropriate

        // Account for the seconds being 60
        if (ete.second >= 60) {
            ete.minute++;
            ete.second -= 60;
        }

        // account for the minutes being 60
        if (ete.minute >= 60) {
            ete.hour++;
            ete.minute -= 60;
        }

        // Time object is good to go now
        return ete;
    }

    /** Calculate the absolute angular difference between the two headings
     * 
     * @param hdg angle 1 in degrees (typically the heading)
     * @param brg angle 2 in degrees (typically the bearing)
     * @return difference between hdg and brg in degrees
     */
    public static double angularDifference(double hdg, double brg) {
        double absDiff = Math.abs(hdg - brg);
        if (absDiff > 180) {
            return 360 - absDiff;
        }
        return absDiff;
    }
}

Related

  1. getTempsTrajetBus(int distance)
  2. getTempsTrajetPied(int distance)
  3. hammingDistance(String sourceHashCode, String hashCode)
  4. getMessageForDistance(long distance)
  5. calculateEte(boolean useBearing, double distance, double speed, double bearing, double heading)
  6. distance(double lat1, double lon1, double lat2, double lon2, char unit)
  7. computeSpeed(int distance, long duration)
  8. getClosestStore(ArrayList storeIds, SparseArray distanceMap)
  9. toDistanceSearchUri(Uri contentUri, Location location, double distance)