Java Distance Calculate calcDistanceIfAccel(double startVelocity, double accel, double targetVelocity, double duration)

Here you can find the source of calcDistanceIfAccel(double startVelocity, double accel, double targetVelocity, double duration)

Description

Determine how far the Vehicle will go in the given duration, if it starts at the given starting velocity and accelerates at the given acceleration toward the provided target velocity.

License

Open Source License

Parameter

Parameter Description
startVelocity the initial velocity of the Vehicle
accel the acceleration of the Vehicle during this time
targetVelocity the velocity at which the Vehicle will stop accelerating
duration the duration for which this all takes place

Return

how far the Vehicle will travel in this time

Declaration

public static double calcDistanceIfAccel(double startVelocity,
        double accel, double targetVelocity, double duration) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w w  w .  ja  v a 2 s  .co  m
     * Determine how far the Vehicle will go in the given duration, if
     * it starts at the given starting velocity and accelerates at the given
     * acceleration toward the provided target velocity.
     *
     * @param startVelocity  the initial velocity of the Vehicle
     * @param accel          the acceleration of the Vehicle during this
     *                       time
     * @param targetVelocity the velocity at which the Vehicle will stop
     *                       accelerating
     * @param duration       the duration for which this all takes place
     * @return               how far the Vehicle will travel in this time
     */
    public static double calcDistanceIfAccel(double startVelocity,
            double accel, double targetVelocity, double duration) {
        // If we're speeding up
        if (accel >= 0.0) {
            // If we're already at or above the target velocity
            if (startVelocity >= targetVelocity) {
                // We won't setMaxAccelWithMaxTargetVelocity, so just use current velocity
                return startVelocity * duration;
            }
            // Otherwise we need to figure out how much accelerating will be done
            double maxChange = accel * duration;
            double requestedChange = targetVelocity - startVelocity;
            // If our requested change is at least our maxChange, we'll be
            // accelerating the whole time
            if (requestedChange >= maxChange) {
                return (startVelocity + (maxChange / 2.0)) * duration;
            } else {
                // Otherwise, we will setMaxAccelWithMaxTargetVelocity for part of it
                double accelDuration = (targetVelocity - startVelocity)
                        / accel;
                // Find our average velocity during this time
                double avgAccelVelocity = (targetVelocity + startVelocity) / 2;
                // The distance is how far we go during acceleration plus how far
                // we go after that
                return avgAccelVelocity * accelDuration + targetVelocity
                        * (duration - accelDuration);
            }
        } else { // If we're decelerating
            // If we're already at or below the target velocity
            if (startVelocity <= targetVelocity) {
                // We won't decelerate, so just use current velocity
                return startVelocity * duration;
            }
            // Otherwise we need to figure out how much decelerating will be done
            double maxChange = accel * duration;
            double requestedChange = targetVelocity - startVelocity;
            // If our requested change is at least (as negative as) our maxChange,
            // we'll be decelerating the whole time
            if (requestedChange <= maxChange) {
                return (startVelocity + (maxChange / 2.0)) * duration;
            } else {
                // Otherwise, we will decelerate for part of it
                double decelDuration = (targetVelocity - startVelocity)
                        / accel;
                // Find our average velocity during this time
                double avgDecelVelocity = (targetVelocity + startVelocity) / 2;
                // The distance is how far we go during deceleration plus how far
                // we go after that
                return avgDecelVelocity * decelDuration + targetVelocity
                        * (duration - decelDuration);
            }
        }
    }
}

Related

  1. calcDistance(final int coordinate1, final int coordinate2)
  2. calcDistance(int ax, int ay, int bx, int by)
  3. calcDistance(int x1, int y1, int x2, int y2)
  4. calcDistanceBetweenCoords(double startLat, double startLon, double endLat, double endLon)
  5. calcDistanceHubery(double lat1, double lng1, double lat2, double lng2, int type)
  6. calcDistanceToStop(double startingVelocity, double maxDeceleration)
  7. calculateDistance(double lat1, double lng1, double lat2, double lng2)
  8. calculateDistance(Double lng1, Double lat1, Double lng2, Double lat2)
  9. calculateDistance(Double prevLat, Double prevLon, Double currentLat, Double currentLon)