Skip to content

Instantly share code, notes, and snippets.

@posaunehm
Created April 11, 2013 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save posaunehm/5359607 to your computer and use it in GitHub Desktop.
Save posaunehm/5359607 to your computer and use it in GitHub Desktop.
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