简体   繁体   中英

Minimal values of X and Y coordinates in PointCollection (C#)

let's assume I have got a collection of points ( PointCollection ). What I want to do is to find the minimal value of X and Y coordinates among these points. Obviously one could iterate over the collection and check the coordinates step by step.

I wonder if there is a quicker and more efficient solution.

Do you have any ideas?

Thanks

Quicker to type? Perhaps:

var xMin = points.Min(p => p.X);
var yMin = points.Min(p => p.Y);

But that will execute slower than a single foreach loop:

bool first = true;
foreach(var point in points) {
    if(first) {
        xMin = point.X;
        yMin = point.Y;
        first = false;
    } else {
        if(point.X < xMin) xMin = point.X;
        if(point.Y < yMin) yMin = point.Y;
    }
}

To get the lowest x and y positions seperately, use

var lowestX = pointCollection.Min( p => p.X );
var lowestY = pointCollection.Min( p => p.Y );

If you want the one with the lowest combined X and Y position, use

var lowest = pointCollection.Min( p => p.X + p.Y );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM