Vahid
Vahid

Reputation: 5444

Get the inner points which are on a straight line

The question I'm about to ask might seem a Geometry question at the first glance, but it actually can be solved by using LINQ, at least I hope so!

I have 5 points on a straight line two of them are at the ends of the line. How can I select the ones that are in the interior of the line (not at the end points) using LINQ?

public class Point
{
    public double X;
    public double Y;
}

var listOfPointsOnALine = new List<Point>
{
    new Point { X = 2500, Y = 50 },
    new Point { X = 2540, Y = 112.5 },
    new Point { X = 2580, Y = 175 },
    new Point { X = 2620, Y = 237.5 },
    new Point { X = 2660, Y = 300 },
}

So using some LINQ on the list above should give me the list below:

innerPointsOnALine: {(2540, 112.5), (2580, 175), (2620, 237.5)}
  1. The points are ordered in the original list
  2. The points should be in the same order they appear in the original list (listOfPointsOnALine)

enter image description here

Upvotes: 0

Views: 73

Answers (1)

Habib
Habib

Reputation: 223267

If I understand it correctly then I think you are looking for:

 var newList = listOfPointsOnALine
                          .Skip(1)
                          .Take(listOfPointsOnALine.Count - 2)
                          .ToList();

You may have to check for length of list before doing this.

Upvotes: 2

Related Questions