我有一个 PointF 数组,我想使用它通过 Graphics.DrawCurve 方法绘制曲线。
为此,我现在需要 X 和 Y 的最大值和最小值,以便正确缩放位图图像框。
如何在 PointF 的数组中找到 X 和 Y 的最大值和最小值?
我想出了这个主意,但我不确定这是否是最好的方法!
//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;
}
}
请您参考如下方法:
您需要遍历数组中的所有元素并针对边界框测试每个元素,当当前项目在边界框之外时增加边界框。像这样:
Point
min = first item in array,
max = first item in array;
foreach (item in array of points)
{
min.x = Math.Min (min.x, item.x)
min.y = Math.Min (min.y, item.y)
max.x = Math.Max (max.x, item.x)
max.y = Math.Max (max.y, item.y)
}
(min,max) are now the opposite corners of an axis aligned bounding box
编辑:您的想法是正确的,但是有一个 .Net 框架 API 可以进行最小/最大测试:Math.Min 和 Math.Max。除非有一些关于点数组的其他信息可用于减少测试次数,否则您将不得不测试数组中的每个点。不幸的是,那里没有捷径。我想知道 JIT 编译器是否足够聪明,可以为此使用 SIMD?
此外,如果数组中的所有点都小于零,则使用值 0 初始化可能会导致错误。
