gpt4 book ai didi

c# - PointF[] 数组中的最大值和最小值

转载 作者:太空宇宙 更新时间:2023-11-03 22:07:17 25 4
gpt4 key购买 nike

我有一个 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 初始化可能会导致错误。

关于c# - PointF[] 数组中的最大值和最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7964015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com