开发者

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 col开发者_StackOverflow社区lection 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 );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜