I have an array of PointF’s that I want to use it for drawing a curve with Graphics.DrawCurve method.
For doing this, I need to now max and min of both X and Y so I can scale my bitmap imagebox correctly.
How is it possible to find the max and min for X & Y in an array of PointF’s ?
I came up with this idea but I am not sure if this is the best way!
//Find the max value on X axis (Time) and Y axis (Current)
float xMax = 0;
float yMax = 0;
foreach (PointF point in points)
{
if (point.X > xMax)
{
xMax = point.X;
}
if (point.Y > yMax)
{
yMax = point.Y;
}
}
You need to iterate over all the elements in the array and test each one against a bounding box, increasing the bounding box when the current item is outside it. Like this:
EDIT: You’ve got the right idea, but there is a .Net framework API to do the min/max test: Math.Min and Math.Max. Unless there’s some other information about the array of points that can be used to reduce the number of tests, you are going to have to test every point in the array. No short cuts there unfortunately. I wonder if the JIT compiler is smart enough to use SIMD for this?
Also, initialising with the value 0 could cause an error if all the points in the array are less than zero.