Guest User

Untitled

a guest
May 27th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. public static class Ext
  2. {
  3. private static IEnumerable<decimal> LinearInterpolate(decimal lowerBound, decimal upperBound, int pointCount)
  4. {
  5. var gradient = (upperBound - lowerBound) / (pointCount + 1);
  6. for(var i = 1; i <= pointCount; i++)
  7. {
  8. yield return lowerBound + gradient * i;
  9. }
  10. }
  11.  
  12. public static IEnumerable<decimal> Interpolate(this IEnumerable<decimal> original)
  13. {
  14. var zeroCount = 0;
  15. var previousValue = 0m;
  16. var enumerator = original.GetEnumerator();
  17.  
  18. // Strip leading 0s
  19. while(enumerator.MoveNext())
  20. {
  21. if(enumerator.Current == 0)
  22. {
  23. yield return 0;
  24. }
  25. else
  26. {
  27. previousValue = enumerator.Current;
  28. yield return enumerator.Current;
  29. break;
  30. }
  31. }
  32.  
  33. // Interpolate 0s
  34. while(enumerator.MoveNext())
  35. {
  36. if(enumerator.Current == 0)
  37. {
  38. zeroCount++;
  39. }
  40. else
  41. {
  42. if(zeroCount > 0)
  43. {
  44. foreach(var item in LinearInterpolate(previousValue, enumerator.Current, zeroCount))
  45. {
  46. yield return item;
  47. }
  48. zeroCount = 0;
  49. }
  50. yield return enumerator.Current;
  51. previousValue = enumerator.Current;
  52. }
  53. }
  54.  
  55. // Strip trailing 0s
  56. for (var i = 0; i < zeroCount; i++)
  57. {
  58. yield return 0;
  59. }
  60. }
  61. }
Add Comment
Please, Sign In to add comment