Hello,everyone.I have a List<Line> which have been sorted by the StartPoint.X.The code is below:
public int sortByStartPointX(Line a, Line b)
{
if (Math.Abs(a.StartPoint.X - b.StartPoint.X) <= 1)
return a.StartPoint.Y.CompareTo(b.StartPoint.Y);
return a.StartPoint.X.CompareTo(b.StartPoint.X);
}
LineList.Sort(sortByStartPointX);
So,the lines'order should like the image.And now,my problem is that for each StartPoint.X, I just want a single line to be left in the LineList.Take the picture for example, I just want the 1,n+1,n+m+1 lines to be left in the LineList.All I can do is below.
List<Line> cesoredLineLst = new List<Line>();
private void censorLines(List<Line> sourceLineList,List<Line> destinationLineList)
{
destinationLineList.Clear(); //Make sure it is empty at the begining
while (sourceLineList.Count() > 0) //The loop can noly stop when the sourceLineList is empty
{
//Get the loop line,which is the fist element of the List
Line loopLine = sourceLineList.First();
//remove the line from the List
sourceLineList.Remove(loopLine);
//to determine whethe the StartPoint.X existed in the destinationLineList or not
bool existRepeatedX = destinationLineList.Exists(li =>
{
if (loopLine.StartPoint.X = li.StartPoint.X)
return true;
return false;
});
//if not,add the line to the destinationLineList
if (!existRepeatedX)
destinationLineList.Add(loopLine);
}
}
cesorLines(LineList,cesoredLineLst);
I know my algorithm is not good.I need to define a extra List<Line>,and every line in the LineList need to be iterated.When the number is huge,this algorithm gets worse!! So,is there any way to improve? The extra List<Line> can be spared?