using System; using System.Collections.Generic; using System.Linq; namespace grapher.Models.Calculations { public class AccelChartData { #region Constructors public AccelChartData() { AccelPoints = new SortedDictionary(); VelocityPoints = new SortedDictionary(); GainPoints = new SortedDictionary(); OrderedVelocityPointsList = new List(); OutVelocityToPoints = new Dictionary(); } #endregion Constructors #region Properties public SortedDictionary AccelPoints { get; } public SortedDictionary VelocityPoints { get; } public SortedDictionary GainPoints { get; } public List OrderedVelocityPointsList { get; } public Dictionary OutVelocityToPoints { get; } #endregion Properties #region Methods public void Clear() { AccelPoints.Clear(); VelocityPoints.Clear(); GainPoints.Clear(); OrderedVelocityPointsList.Clear(); OutVelocityToPoints.Clear(); } public (double, double, double) FindPointValuesFromOut(double outVelocityValue) { if (OutVelocityToPoints.TryGetValue(outVelocityValue, out var values)) { return values; } else { var velIdx = GetVelocityIndex(outVelocityValue); velIdx = Math.Min(velIdx, VelocityPoints.Count - 1); values = (VelocityPoints.ElementAt(velIdx).Key, AccelPoints.ElementAt(velIdx).Value, GainPoints.ElementAt(velIdx).Value); OutVelocityToPoints.Add(outVelocityValue, values); return values; } } public (double, double, double) ValuesAtIndex(int index) { return (AccelPoints.ElementAt(index).Value, VelocityPoints.ElementAt(index).Value, GainPoints.ElementAt(index).Value); } public (double, double, double) ValuesAtInVelocity(double inVelocity) { return (AccelPoints[inVelocity], VelocityPoints[inVelocity], GainPoints[inVelocity]); } public int GetVelocityIndex(double outVelocityValue) { var velIdx = OrderedVelocityPointsList.BinarySearch(outVelocityValue); if (velIdx < 0) { velIdx = ~velIdx; } velIdx = Math.Min(velIdx, VelocityPoints.Count - 1); return velIdx; } #endregion Methods } }