Minimal values of X and Y coordinates in PointCollection (C#)
-
27-09-2019 - |
Question
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
Solution
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;
}
}
OTHER TIPS
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 );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow