none
How to find the maximize & minimum points from List<Point> ?

    Question

  • I hold some List of point ( List ) and i need to find 2 point from this list

    1. Point 1 - with the minimum X, Y value
    2. Point 2 - with the maximum X, Y value

    What is the best way to do it ?

    Thanks.

    Sunday, August 22, 2010 9:09 AM

Answers

  • List

     

    <Point> points = new List<Point>();

    points.Add(

    new Point(10, 10));

    points.Add(

    new Point(20, 10));

    points.Add(

    new Point(30, 10));

     

    // Simple extension methods on a list

     

    int maxX = points.Max(e => e.X); // returns 30

     

    int minX = points.Min(e => e.X); // returns 10

     

    int maxXY = points.Max(e => (e.X + e.Y)); // returns 40

     

    // Or with LINQ to return the point

     

    var r = (from t in points select t).Min(e => e.X);

    Hope this helps!

     


    AFGADFG
    Sunday, August 22, 2010 9:58 AM
  • Hi ronili2,

     

    Welcome to MSDN forums!

    I am interested in helping.

    Every point has X and Y, the minimum/maximum of X and Y may not in a point.

    So you may give us the rule of min/max.

    As Stuart answer, Linq is an easy and good way to deal with it.

    System.Collections Namespace

    http://msdn.microsoft.com/en-us/library/system.collections(VS.71).aspx

    There are many ways comparing two objects.

     

    Hope these helps, if you have any problems, please feel free to let me know.

     

    Best Regards,

    Alan Chen

    ________________________________________

    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Friday, August 27, 2010 3:44 AM

All replies

  • List

     

    <Point> points = new List<Point>();

    points.Add(

    new Point(10, 10));

    points.Add(

    new Point(20, 10));

    points.Add(

    new Point(30, 10));

     

    // Simple extension methods on a list

     

    int maxX = points.Max(e => e.X); // returns 30

     

    int minX = points.Min(e => e.X); // returns 10

     

    int maxXY = points.Max(e => (e.X + e.Y)); // returns 40

     

    // Or with LINQ to return the point

     

    var r = (from t in points select t).Min(e => e.X);

    Hope this helps!

     


    AFGADFG
    Sunday, August 22, 2010 9:58 AM
  • You need to define what is the minimum/maximum X,Y.

    Between point X=4, Y=4 and point X=5, Y=3, which is the minimum, which is the maximum?

    Sunday, August 22, 2010 9:06 PM
  • Hi ronili2,

     

    Welcome to MSDN forums!

    I am interested in helping.

    Every point has X and Y, the minimum/maximum of X and Y may not in a point.

    So you may give us the rule of min/max.

    As Stuart answer, Linq is an easy and good way to deal with it.

    System.Collections Namespace

    http://msdn.microsoft.com/en-us/library/system.collections(VS.71).aspx

    There are many ways comparing two objects.

     

    Hope these helps, if you have any problems, please feel free to let me know.

     

    Best Regards,

    Alan Chen

    ________________________________________

    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Friday, August 27, 2010 3:44 AM