public CalculationResult Calculate(AccelerationPolarity accelerationPolarity, double timeToNode, double distanceToNode, double currentSpeed) { var result = new CalculationResult(); CorrectAccelerationToPolarity(accelerationPolarity, AccelerationMagnitude); var argsCheckResult = ValidateArguments(distanceToNode, timeToNode, currentSpeed, ReactionTime, AccelerationMagnitude); if (argsCheckResult != CalculationErrors.NoErrors) { // Error detected. result.AdvisorySpeed = null; } else { var advisorySpeed = currentSpeed + AccelerationMagnitude * (timeToNode - ReactionTime - Math.Sqrt(Math.Pow(timeToNode - ReactionTime, 2) - (2 * (distanceToNode - timeToNode * currentSpeed)) / AccelerationMagnitude) ); result.AdvisorySpeed = advisorySpeed; } result.Errors = argsCheckResult; return(result); }
private void CorrectAccelerationToPolarity(AccelerationPolarity accelerationPolarity, double accelerationMagnitude) { if ((accelerationPolarity == AccelerationPolarity.Accelerate) && (accelerationMagnitude <= 0)) { accelerationMagnitude *= -1; } if ((accelerationPolarity == AccelerationPolarity.Decelerate) && (accelerationMagnitude >= 0)) { accelerationMagnitude *= -1; } }