请阅读以了解代码
我们正在比较三个海拔点:
我们举个例子,用户启动时的海拔是100m
例如:700m,1000m,800 中的三个标高。
以上三个高程表示用户到达1000m后进行了下降。所以对于仰角增益是1000-100m = 900m,这是真正的仰角增益。其中减去的 100m 是行程开始时记录的海拔高度。
现在让我们考虑接下来的三个海拔是 800m,600m,700m 这表明在下降到 600m 后用户再次开始攀登。
所以现在真正的下降将计算用户从 1000m 下降了多少,因此真正的下降将是 1000m-600m =400m
代码块中的 cmets 解释了每个步骤在做什么。
// This jut stimulates elevations in a small trip
// instead of this list call the functions whenever you record a elevation.
// and on the end of the trip call the [onStopTrip] function.
List<double> listOfDemoElevations = [
200.0, 300.0,400.0,1000.0,800.0,700.0,600.0,500.0,600.0,800.0,300.0,200.0];
///This Code Snippet compares Elevation of three points to see if the person has started ascending or is descending.
/// Then the code only stores the point after which user asecded or descended.
/// Then Calulates the total Elevation Gain or Elevation Loss.
double elevationGain = 0.0;
double elevationLoss = 0.0;
double startTripElevation = 100.0; //Record the StartTripElevation;
double previousElevation = startTripElevation;
double beforePreviousElevation = startTripElevation;
double adjustment =startTripElevation ; // this to get the truw ascend or descend
// This function check the three elevation then adds to elevation gain or loss accordinly with adjustment so true elevation gain or loss is recorded.
void calculateElevationGainLoss(double currentElevation) {
if (beforePreviousElevation < previousElevation &&
previousElevation > currentElevation) {
elevationGain = elevationGain + (previousElevation - adjustment);
adjustment = previousElevation;
} else if (beforePreviousElevation > previousElevation &&
currentElevation > previousElevation) {
elevationLoss = elevationLoss + (adjustment - previousElevation);
adjustment = previousElevation;
}
beforePreviousElevation = previousElevation;
previousElevation = currentElevation;
}
/// this function check if the elevation at which the user ended the trip to be added to elevation gain or loss
void onStopTrip(double currentElevation) {
if (adjustment > currentElevation) {
elevationLoss = elevationLoss + (adjustment - currentElevation);
} else if (adjustment < currentElevation) {
elevationGain = elevationGain + (currentElevation - adjustment);
}
}
listOfDemoElevations.forEach((element) {
calculateElevationGainLoss(element);
});
// Call this function when user hits end trip
onStopTrip(listOfDemoElevations.last);
print('Elevation gain=$elevationGain');
print('Elevation loss=$elevationLoss');
您可以将整个代码放入 dartpad 中的void main{} 中并进行测试: