TheSpixxyQ
TheSpixxyQ

Reputation: 1035

C# Point3DCollection select point by min or max coordinate

I have Point3DCollection of hundreds of points like Point3D(2, 5, 8), Point3D(8, 6, 9), Point3D(5, 8, 12)... and I need to get coordinates of those which have lowest and highest X. I know how to find lowest value (using LINQ Min), but I don't know, how to find lowest value and get Y and Z of it. Can you please help me?

Upvotes: 0

Views: 1238

Answers (1)

Amit
Amit

Reputation: 1857

suppose your Point3DCollection is list of Point3D class's object. and your Point3D class looks like this.

public class Point3D
{
  public int X;
  public int Y;
  public int Z;

  public Point3D(int x, int y, int z)
  {
    X = x;
    Y = y;
    Z = z;
  }
}

your desired Linq will be

for lowest X

Point3D p = Point3DCollection.OrderBy(x => x.X).FirstOrDefault();

for highest x

if(Point3DCollection.Count > 0)
   Point3D p = Point3DCollection.OrderBy(x => x.X).Last();

you should better check of emptiness of Point3DCollection first.

here is working sample

Upvotes: 2

Related Questions