Created
April 11, 2013 00:17
-
-
Save posaunehm/5359607 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ReducePoints | |
{ | |
public class PointReducer | |
{ | |
private readonly List<XYPoint> _input; | |
public PointReducer(IEnumerable<XYPoint> input) | |
{ | |
_input = input.ToList(); | |
} | |
public IEnumerable<XYPoint> Reduce(double start, double end, int restrict) | |
{ | |
var counter = 0; | |
foreach (var range in EnumrateRestrictedRangeCollection(start, end, restrict)) | |
{ | |
var tempList = new List<XYPoint>(); | |
while ( counter < _input.Count && _input[counter].X < range) | |
{ | |
tempList.Add( _input[counter]); | |
counter++; | |
} | |
foreach (var xyPoint in EnumrateReducedList(tempList)) | |
{ | |
yield return xyPoint; | |
} | |
} | |
} | |
private static IEnumerable<XYPoint> EnumrateReducedList(List<XYPoint> tempList) | |
{ | |
if (tempList.Count <= 0) | |
{ | |
yield break; | |
} | |
var orderedTempList = tempList.OrderBy(point => point.Y); | |
var returnArray = new[] | |
{ | |
tempList.First(), | |
tempList.Last(), | |
orderedTempList.First(), | |
orderedTempList.Last(), | |
}.Distinct().OrderBy(point => point.X); | |
foreach (var xyPoint in returnArray) | |
{ | |
yield return xyPoint; | |
} | |
} | |
IEnumerable<double> EnumrateRestrictedRangeCollection(double start, double end, int restrict) | |
{ | |
var distance = (end - start) / restrict; | |
for (var i = 1; i < restrict; i++) | |
{ | |
yield return start + distance * i; | |
} | |
yield return end; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment