简体   繁体   English

PointCollection(C#)中X和Y坐标的最小值

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

let's assume I have got a collection of points ( PointCollection ). 假设我有一个点集合( PointCollection )。 What I want to do is to find the minimal value of X and Y coordinates among these points. 我想做的是找到这些点之间的X和Y坐标的最小值。 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: 但这将比单个foreach循环执行得慢:

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 要分别获得最低的x和y位置,请使用

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 如果您希望X和Y的组合位置最低,请使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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